« PreviousNext »

In this section, we'll look at RDF (Resource Description Framework).

Resource Description Framework

We can use the  tree  elements to display a set of data, such as bookmarks or mail messages. However, it would be inconvenient to do so by entering the data directly into the XUL file. It would make it very difficult to modify the bookmarks if they were directly in the XUL file. The way to solve this is by using RDF datasources.

RDF (Resource Description Framework) is a format that can be used to store resources such as bookmarks or mail. Alternatively, data in other formats can be used and code written that will read the file and create RDF data from it. This is how Mozilla works when it reads data such as bookmarks, the history or mail messages. Mozilla provides datasources for this common data so that you can easily use them.

You can use any of the provided RDF datasources to populate trees with data or you can point to an RDF file stored in XML which contains the data. This makes it very convenient to display trees with lots of rows in them. RDF can also populate other XUL elements as well such as listboxes and menus. We'll see this in the next section.

A very brief overview of RDF will be provided here.  For more information about RDF, see the RDF specification.

RDF/XML

RDF consists of a model, which is a graph representation of data. RDF/XML is an XML language which can be used to represent RDF data. It contains a fairly simple set of elements. The sample below shows a simple RDF template.

<?xml version="1.0"?>
<RDF:RDF
  xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  ...
</RDF:RDF>

This has some similarities to the XUL header. Instead of the window element, the RDF element is used. You can see the namespace for RDF was declared so that the RDF elements are recognized properly. Inside, the RDF element, you will place the data. To see some example RDF/XML files, look at those provided with Mozilla. They have the extension rdf.

RDF database

Let's take the example of a bookmarks list generated from RDF. A bookmarks list contains a set of records, each with a set of data associated with it, such as a URL, a bookmark title and a visited date.

Think of the bookmarks as a database, which is stored as a large table with numerous fields. In the case of RDF however, the lists may be hierarchical as well. This is necessary so that we can have folders or categories of bookmarks. Each of the fields in an RDF database is a resource, each with a name associated with it. The name is described by a URI.

For example, a selection of the fields in the Mozilla bookmark list is described by the URIs below:

Name http://home.netscape.com/NC-rdf#Name Bookmark name
URL http://home.netscape.com/NC-rdf#URL URL to link to
Description http://home.netscape.com/NC-rdf#Description Bookmark description
Last Visited http://home.netscape.com/WEB-rdf#LastVisitDate Date of last visit

These are generated by taking a namespace name and appending the field name. In the next section, we'll look at how we can use these to fill in the field values automatically. Note that the last visited date has a slightly different namespace than the other three.

RDF/XML file example

Below, a sample RDF/XML file is shown, listing a table with three records and three fields.

<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:ANIMALS="http://www.some-fictitious-zoo.com/rdf#">

  <RDF:Seq about="http://www.some-fictitious-zoo.com/all-animals">
    <RDF:li>
       <RDF:Description about="http://www.some-fictitious-zoo.com/mammals/lion">
         <ANIMALS:name>Lion</ANIMALS:name>
         <ANIMALS:species>Panthera leo</ANIMALS:species>
         <ANIMALS:class>Mammal</ANIMALS:class>
       </RDF:Description>
    </RDF:li>
    <RDF:li>
       <RDF:Description about="http://www.some-fictitious-zoo.com/arachnids/tarantula">
         <ANIMALS:name>Tarantula</ANIMALS:name>
         <ANIMALS:species>Avicularia avicularia</ANIMALS:species>
         <ANIMALS:class>Arachnid</ANIMALS:class>
       </RDF:Description>
    </RDF:li>
    <RDF:li>
       <RDF:Description about="http://www.some-fictitious-zoo.com/mammals/hippopotamus">
         <ANIMALS:name>Hippopotamus</ANIMALS:name>
         <ANIMALS:species>Hippopotamus amphibius</ANIMALS:species>
         <ANIMALS:class>Mammal</ANIMALS:class>
       </RDF:Description>
    </RDF:li>
  </RDF:Seq>
</RDF:RDF>

Here, three records have been described, one for each animal. Each RDF:Description tag describes a single record. Within each record, three fields are described, name, species and class. It isn't necessary for all records to have the same fields but it makes more sense to have them all the same.

Each of three fields have been given a namespace of ANIMALS, the URL of which has been declared on the RDF tag. The name was selected because it has meaning in this case, but we could have selected something else. The namespace feature is useful because the class field might conflict with that used for styles.

The Seq and li elements are used to specify that the records are in a list. This is much like how HTML lists are declared. The Seq element is used to indicate that the elements are ordered, or in sequence. Instead of the Seq element, you can also use Bag to indicate unordered data, and Alt to indicate data where each record specifies alternative values (such as mirror URLs).

The resources can be referred to in a XUL file by combining the namespace URL followed by the field name. In the example above, the following URIs are generated which can be used to refer to the specific fields:

Name http://www.some-fictitious-zoo.com/rdf#name
Species http://www.some-fictitious-zoo.com/rdf#species
Class http://www.some-fictitious-zoo.com/rdf#class

Next, we'll see how we can use RDF to populate XUL elements.

« PreviousNext »