Search This Blog

21 October 2008

Further Semantic Antics: XML and all that

This is the second part of a short series on RDF and the Semantic Web. The first part can be found here.

RDF is essentially a form of XML, but it isn't quite the same as other XML formats such as SVG or RSS 2.0. Let's take a look at a snippet of RDF code (I will assume you have a working knowledge of XML):

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:example="http://example.com/vocab/">
   <example:company rdf:about="http://acmeinc.com/ourcompany#us">
      <example:companyName>Acme Inc.</example:companyName>
      <example:hasEmployee>
         <example:person>
            <example:personName>John Smith</example:personName>
            <example:employeeID>385740</example:employeeID>
         </example:person>
      </example:hasEmployee>
   </example:company>
</rdf:RDF>

Confused yet? Let's go through it step by step:

  1. The first thing to notice about this document is the abundance of colons. This is thanks to XML Namespaces, which allow tags from different vocabularies to be used in the same document. (A vocabulary is a collection of tags used for a specific purpose.) The namespace is a URI with the last portion missing - otherwise known as the base URI - and when the name of the tag is appended a full URI is created that references that tag specifically. Instead of having to write out the base URI over and over again whenever you want to use a tag in a particular namespace, you can assign it a name. Now when you use a tag, you can write its URI as nameOfBaseURI:tagName. For example, if the base URI http://exampletwo.org/vocab/ has been assigned the name exampletwo, then the tag name exampletwo:thing represents the URI http://exampletwo.org/vocab/thing. If you were to visit that URI, it would probably give you a description of what the thing tag meant. Base URIs are assigned names in the outermost tag of the document, in this case the rdf:RDF tag, using the attribute xmlns:assignedName, where assignedName is substituted with whatever name you want to give to the base URI in question. In RDF, all tags must have a namespace.
  2. The second thing to point out is the way in which triples are represented. In the example above, the tag example:companyName is nested inside the example:company tag, meaning that company is the subject of that particular triple whilst companyName is the predicate. Inside the example:companyName tag is a 'string literal', in other words a plain bit of text rather than a URI, and this is the object of the triple. Therefore, this triple means: "There is a company with the name 'Acme Inc.'." The second triple is much the same, but this time the object is not a string literal but an example:person tag, so the triple means: "There is a company that employs an example:person." The example:person tag is itself the subject of a third and fourth triple, which give us the name and ID number of the employee respectively.

If you wanted to express this RDF document in English, you would get something along the lines of:

There is a company with the name 'Acme Inc.' and with an employee with the name 'John Smith' and the employee ID 385740.

And so now you know the way RDF works. In the next post I'll talk about some popular RDF vocabularies.

No comments:

Post a Comment