Tip: Use rdf:about and rdf:ID effectively in RDF/XML

Minimize confusion in specifying resources

The combination of RDF and XML allows for several different approaches to specifying resources, and sometimes the rules for interpreting the syntax can be troublesome.In this tip, Uche Ogbuji uses examples to illustrate the various behaviors of the rdf:ID and rdf:about attributes, and shows how to use XML Base to control these behaviors.

Uche Ogbuji, Principal Consultant, Fourthought, Inc.

Photo of Uche OgbujiUche Ogbuji is a consultant and co-founder of Fourthought Inc., a software vendor and consultancy specializing in XML solutions for enterprise knowledge management. Fourthought develops 4Suite, an open source platform for XML, RDF, and knowledge-management applications. Mr. Ogbuji is a computer engineer and writer born in Nigeria, living and working in Boulder, Colorado, USA. You can contact Mr. Ogbuji at uche@ogbuji.net.



01 February 2003

Also available in Japanese

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:about and 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 rdf:Description as http://spam.com/eggs/listing.rdf#local-resource.


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>

Conclusion

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:ID and 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.

Resources

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into XML on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12217
ArticleTitle=Tip: Use rdf:about and rdf:ID effectively in RDF/XML
publish-date=02012003