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="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> ... </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="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. 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="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 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="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>

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

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 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.

5.3.4.1 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.

5.3.4.2 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>

5.3.4.3 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="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 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

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net