This tip covers the RDF/XML syntax specification of 23 January 2003. You may want to review the latest W3C RDF primer if you are not familiar with recent RDF specifications (see Resources). In RDF/XML, the subjects of statements are organized into node elements, which use attributes such as
rdf:ID to set the subject for a collection of statements about that subject. Rules govern how the actual RDF subject URIs are constructed from these attributes, but there is plenty of room for confusion, and even instability, in the parsing results from environment to environment, if you're not careful. This tip offers some practices that can help minimize such errors and confusion.
Name that base
rdf:ID is frequently used in the manner shown in Listing 1.
Listing 1. A simple use of rdf:ID (listing1.rdf)
<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" > <rdf:Description rdf:ID="local-record"> <dc:title>Local Record</dc:title> </rdf:Description> </rdf:RDF>
In this case, RDF/XML prescribes that the subject of the statement is a hash followed by the ID name (
#local-resource) which is then appended to the in-scope base URI. This latter part is where confusion can arise. In Listing 1, the in-scope base URI is not explicitly stated. That URI is usually provided by the parser based on the location of the file in which it was parsed. For example, if Listing 1 is hosted at the URI
http://rdfhost/listing1.rdf, then the actual subject URI is likely to be
http://rdfhost/listing1.rdf#local-resource. If instead the listing is hosted at the local file system path
/code/rdf/listing1.rdf, then the resolved URI is likely to be
file:///code/rdf/listing1.rdf#local-resource. The parser could also perform resolution in other, less straightforward ways. RDF/XML does not mandate any base URI determination process for parsed RDF documents.
As long as the parser's behavior is reasonable, this shift in subject URI might be what you want, depending on the location of the parsed file. The RDF statements you intend the file to assert may depend on the context of the file itself. However, in many cases you have an absolute URI in mind for the resources described in the file, so this variation is problematic.
One solution is to always use
rdf:about and an absolute URI -- I shall discuss this a bit more later. Another solution is to use XML Base, a specification that allows one to assert a base URI attached to an element in an XML file. By placing the
xml:base attribute in the right spots in the XML file, you can control which base is used to resolve the
rdf:ID. Listing 2 offers an example of this.
Listing 2. Using rdf:ID with XML Base (listing2.rdf)
<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:base="http://spam.com/eggs/listing.rdf" > <rdf:Description rdf:ID="local-record"> <dc:title>Local Record</dc:title> </rdf:Description> </rdf:RDF>
Notice the added
xml:base attribute. I added it to the top-level element, which is often what you want to do to control the base URI for the entire file, since in general elements inherit the base URI of their parent elements. Regardless of whether Listing 2 is hosted at a URI such as
http://rdfhost/listing.rdf or on the local file system, the parser must generate the subject in the
How rdf:about fits into the picture
RDF node elements using
rdf:about rather than
rdf:ID have a different resolution mechanism. Most simply, if the attribute value is an absolute URI, then that URI is taken verbatim as the subject. Therefore, Listing 3 is equivalent to Listing 2 in terms of the RDF model it expresses.
Listing 3. Using rdf:about with an absolute URI (listing3.rdf)
<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" > <rdf:Description rdf:about="http://spam.com/eggs/listing.rdf#local-record"> <dc:title>Local Record</dc:title> </rdf:Description> </rdf:RDF>
You can, however, use a relative URI in the
rdf:about, in which case it is resolved against the base URI. This means that you may again find the
xml:base handy for ensuring the precise URI that is used for the subject, regardless of context. Listing 4 is equivalent to Listing 2 in terms of the RDF model it expresses.
Listing 4. Using rdf:about with a relative URI (listing4.rdf)
<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:base="http://spam.com/eggs/" > <rdf:Description rdf:about="listing.rdf#local-record"> <dc:title>Local Record</dc:title> </rdf:Description> </rdf:RDF>
The behavior of
rdf:about with respect to relative URIs also applies to similar attributes such as
rdf:resource. The behavior of
rdf:ID is similar to that of
rdf:bagID and the like. Throughout your RDF/XML files, you should be acutely aware of any base URIs that are in effect so you can be sure you understand the process by which the RDF model is generated.
As for choosing between
rdf:about, you will most likely want to use the former if you are describing a resource that doesn't really have a meaningful location outside the RDF file that describes it. Perhaps it is a local or convenience record, or even a proxy for an abstraction or real-world object (although I recommend you take great care describing such things in RDF as it leads to all sorts of metaphysical confusion; I have a practice of only using RDF to describe records that are meaningful to a computer).
rdf:about is usually the way to go when you are referring to a resource with a globally well-known identifier or location.
- For gentle introductions to RDF, see "An introduction to RDF" (developerWorks, December 2000) and "The Languages of the Semantic Web."
- The W3C RDFCore working group has been very busy of late. If you are not familiar with current specifications, review the RDF Primer, and if, on the opposite end of the spectrum, you want to get your hands into all the gory RDF/XML details, check out the RDF/XML Syntax Specification.
- See XML Base for details on base URIs in XML nodes and the
- Check out my Thinking XML columns, which cover RDF and other technologies in greater depth.
- I tested all the examples in this tip using the RDF component of the 4Suite platform.
- Find more XML resources on the developerWorks XML zone. For a complete list of XML tips to date, check out the tips summary page.
- IBM trial software: Build your next development project with trial software available for download directly from developerWorks.
- Find out how you can become an IBM Certified Developer in XML and related technologies.
Dig deeper into XML on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Experiment with new directions in software development.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.