5.3 RDF in XML

In preparation for Chapter 6, we need to look at how RDF is written in XML.

5.3.1 The Root Element

In all the examples in this book, I have given the RDF attributes a prefix of rdf:. This is not necessary in many RDF documents, but it is the way they appear in RSS 1.0. For the sake of clarity, we will leave them in here too. Therefore, for reasons we will discuss in Chapter 6, the root element of an RDF document is:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
. . .
</rdf:RDF>

As we will see in Chapter 6, the root element can also contain the URIs of additional RDF vocabularies. In the following examples, we will use elements from the RSS 1.0 vocabulary.

5.3.2 <element rdf:about="URI OF ELEMENT">

The rdf:about attribute defines the URI for the element that contains it. Remember, it is like the subject in a sentence?everything else refers to it. For example:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns="http://purl.org/rss/1.0/"
>
<channel rdf:about="http://www.example.org/">
. . .
</channel>
</rdf:RDF>

means the channel resource is identified by the URI http://www.example.org/. Or, more to the point, everything within the channel element is referred to by http://www.example.org.

The contents of the element then describe the object referred to by the URI:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
         xmlns="http://purl.org/rss/1.0/" >
<channel rdf:about="http://www.example.org">
<title>Sausages are tasty for breakfast</title>
<channel>
</rdf:RDF>

In this example, the resource channel identified by the URI http://www.example.org has a PropertyType title whose value is Sausages are tasty for breakfast.

Remember, RDF describes the relationship between resources, their attributes, and other resources. You have to define all the resources, and the relationship PropertyTypes, before the RDF is valid and meaningful. The different objects are distinguished by unique URIs. So, every resource must have an rdf:about attribute when it is described.

5.3.3 <element rdf:resource="URI" />

Sometimes, the value of a property is another resource. To describe this, we cannot just use the URI of the resource as the value of the element describing the PropertyType, because nothing identifies it as a URI and not just a string or a hyperlink. Instead, we must use the rdf:resource attribute:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
         xmlns="http://purl.org/rss/1.0/" >
   
<channel rdf:about="http://www.example.org">
<title>Sausages are tasty for breakfast</title>
<image rdf:resource="http://www.example.org/picture.jpg" />
</channel>
   
</rdf:RDF>

In this example, the channel resource has a PropertyType image whose value is a resource, http://www.example.org/picture.jpg.

If we then want to describe the image itself, we need to create a description using the rdf:about attribute, as follows:

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
         xmlns="http://purl.org/rss/1.0/">
   
<channel rdf:about="http://www.example.org">
<title>Sausages are tasty for breakfast</title>
<image rdf:resource="http://www.example.org/picture.jpg" />
</channel>
   
<image rdf:about="http://www.example.org/picture.jpg">
<title>A picture of some sausages</title>
</image>
   
</rdf:RDF>

So now we begin to see the way RDF documents are structured. In this example, every concept, object, or thing is defined with reference to a URI. Figure 5-3 shows this example as an RDF graph, using the data model. Table 5-1 shows the relationships between subjects, predicates, and objects in Figure 5-3.

Figure 5-3. An RDF graph of the continuing example
figs/csr_0503.gif

Table 5-1. The relationship illustrated in Figure 5-3

Subject

Predicate

Object

http://www.example.org

http://purl.org/rss/1.0/title

Sausages are tasty for breakfast

http://www.example.org

http://purl.org/rss/1.0/image

http://www.example.org/picture.jpg

http://www.example.org

http://www.w3.org/1999/02/22-rdf-syntax-ns#type

http://purl.org/rss/1.0/channel

http://www.example.org/picture.jpg

http://purl.org/rss/1.0/title

A picture of some sausages

http://www.example.org/picture.jpg

http://www.w3.org/1999/02/22-rdf-syntax-ns#type

http://purl.org/rss/1.0/image

5.3.4 RDF Containers

We've seen that RDF resources can also be used as properties with the use of the rdf:resource attribute. But what if we need to list more than one resource? For this, we need to introduce RDF containers. There are three to choose from, each with its own purpose.

5.3.4.1 rdf:Bag

rdf:Bag is used to denote an unordered list of resources. It is used like this:

<rdf:Bag>
  <rdf:li rdf:resource="URI" />
  <rdf:li rdf:resource="URI" />
  <rdf:li rdf:resource="URI" />
</rdf:Bag>

As you can see, each list item within the rdf:Bag is denoted with an rdf:li element, which takes the rdf:resource attribute. The order of the list items is unimportant and is ignored.

5.3.4.2 rdf:Seq

rdf:Seq is used to denote an ordered list of resources. The syntax is similar to rdf:Bag, but the order of the list is considered important:

<rdf:Seq>
  <rdf:li rdf:resource="URI Number 1" />
  <rdf:li rdf:resource="URI Number 2" />
  <rdf:li rdf:resource="URI Number 3" />
</rdf:Seq>
5.3.4.3 rdf:Alt

rdf:Alt is used to describe a list of alternatives. The order is unimportant, except that the first list item is considered the default. The list items can contain other attributes to differentiate between them. For example, the xml:lang attribute denotes the language of the resource:

<rdf:Alt>
  <rdf:li xml:lang="en" rdf:resource="URI of English Version" />
  <rdf:li xml:lang="fr" rdf:resource="URI of French Version" />
  <rdf:li xml:lang="de" rdf:resource="URI of German Version" />
</rdf:Alt>

So, to continue our example, let's give the channel some items. Example 5-2 shows the first stage: we declare the items as resources connected to the channel.

Example 5-3 includes the items themselves. Note how the URIs match correctly, and pay attention to the position of the items and item elements with respect to the channel.

Example 5-2. A document with references to items
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
         xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="http://www.example.org">
  <title>Sausages are tasty for breakfast</title>
  <image rdf:resource="http://www.example.org/picture.jpg" />
   
  <items>
    <rdf:Seq>
      <rdf:li rdf:resource="http://www.example.org/item1"/>
      <rdf:li rdf:resource="http://www.example.org/item2"/>
      <rdf:li rdf:resource="http://www.example.org/item3"/>
    </rdf:Seq>
  </items>
</channel>
   
<image rdf:about="http://www.example.org/picture.jpg">
  <title>A picture of some sausages</title>
</image>
   
</rdf:RDF>
Example 5-3. A document with the detailed items themselves
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
         xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="http://www.example.org">
  <title>Sausages are tasty for breakfast</title>
  <image rdf:resource="http://www.example.org/picture.jpg" />
   
  <items>
    <rdf:Seq>
      <rdf:li rdf:resource="http://www.example.org/item1"/>
      <rdf:li rdf:resource="http://www.example.org/item2"/>
      <rdf:li rdf:resource="http://www.example.org/item3"/>
    </rdf:Seq>
  </items>
</channel>
   
<image rdf:about="http://www.example.org/picture.jpg">
  <title>A picture of some sausages</title>
</image>
   
<item rdf:about=" http://www.example.org/item1">
<title>This is item 1</title>
</item>
   
<item rdf:about=" http://www.example.org/item2>
<title>This is item 2/title>
</item>
   
<item rdf:about=" http://www.example.org/item3>
<title>This is item 3/title>
</item>
   
</rdf:RDF>

Do you see the resemblance between Example 5-3 and a RSS 0.92 document? In this section, we have made something very close to RSS depicted using RDF. This leads us to Chapter 6, where we will meet RSS 1.0, which is exactly that: RSS written as an RDF document.