Section 5.3. RDF in XML

5.3. RDF in XML

In preparation for the rest of this chapter, 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 isn't necessary in many RDF documents, but it is the way they appear in RSS 1.0. For the sake of clarity, I will leave them in here too. Therefore, for reasons we will discuss later, the root element of an RDF document is:

<rdf:RDF xmlns:rdf=""> ... </rdf:RDF>

As you will see further on, the root element can also contain the URIs of additional RDF vocabularies. The following examples 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=""          xmlns="" > <channel rdf:about=""> ... </channel> </rdf:RDF>

means the channel resource is identified by the URI Or, more to the point, everything within the channel element is referred to by

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

<rdf:RDF xmlns:rdf="">          xmlns="" > <channel rdf:about=""> <title>Sausages are tasty for breakfast</title> <channel> </rdf:RDF>

In this example, the resource channel identified by the URI has a PropertyType title whose value is Sausages are tasty for breakfast. Nothing to object to there, then.

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, you can't 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 as a string or a hyperlink. Instead, use the rdf:resource attribute:

<rdf:RDF xmlns:rdf="">          xmlns="" >     <channel rdf:about=""> <title>Sausages are tasty for breakfast</title> <image rdf:resource="" /> </channel>     </rdf:RDF>

In this example, the channel resource has a PropertyType image whose value is a resource,

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

<rdf:RDF xmlns:rdf="">          xmlns="">     <channel rdf:about=""> <title>Sausages are tasty for breakfast</title> <image rdf:resource="" /> </channel>     <image rdf:about=""> <title>A picture of some sausages</title> </image>     </rdf:RDF>

You can now 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-2 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-2.

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




Sausages are tasty for breakfast picture.jpg syntax-ns#type channel picture.jpg

A picture of some sausages picture.jpg rdf-syntax-ns#type 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 you need to list more than one resource? For this, you need RDF containers. There are three to choose from, each with its own purpose. rdf:Bag

rdf:Bag denotes 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. rdf:Seq

rdf:Seq denotes 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> rdf:Alt

rdf:Alt describes 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: you 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="">          xmlns=""> <channel rdf:about="">   <title>Sausages are tasty for breakfast</title>   <image rdf:resource="" />       <items>     <rdf:Seq>       <rdf:li rdf:resource=""/>       <rdf:li rdf:resource=""/>       <rdf:li rdf:resource=""/>     </rdf:Seq>   </items> </channel>     <image rdf:about="">   <title>A picture of some sausages</title> </image>     </rdf:RDF>

Example 5-3. A document with the detailed items themselves
<rdf:RDF xmlns:rdf="">          xmlns=""> <channel rdf:about="">   <title>Sausages are tasty for breakfast</title>   <image rdf:resource="" />       <items>     <rdf:Seq>       <rdf:li rdf:resource=""/>       <rdf:li rdf:resource=""/>       <rdf:li rdf:resource=""/>     </rdf:Seq>   </items> </channel>     <image rdf:about="">   <title>A picture of some sausages</title> </image>     <item rdf:about=""> <title>This is item 1</title> </item>     <item rdf:about=">" <title>This is item 2/title> </item>     <item rdf:about=">" <title>This is item 3/title> </item> </rdf:RDF>

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

    Developing Feeds with RSS and Atom
    Developing Feeds with Rss and Atom
    ISBN: 0596008813
    EAN: 2147483647
    Year: 2003
    Pages: 118 © 2008-2017.
    If you may any questions please contact us: