In preparation for the rest of this chapter, we need to look at how RDF is written in XML.
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.
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.
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.
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
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
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
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 item
s. Example 5-2 shows the first stage: you declare the items
as resources connected to the channel
. Example 5-3 includes the item
s
themselves. Note how the URIs match correctly and pay attention to
the position of the items
and
item
elements with respect to the
channel
.
<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>
<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.
3.145.107.94