In this section, we'll look at RDF (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 also 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 file 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 among others so use 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 with the data in it. 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 menus. We'll see this in the next section.
Here, a brief overview of RDF will be provided. A good way to understand it is to look at some of the RDF files provided with Mozilla. They have the extension rdf.
RDF, like XUL, is an XML-based language. 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 template. 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.
A full description of RDF will not be given here. However, some parts will be described. 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 URL (actually, a URI).
For example, a selection of the fields in the Mozilla bookmark list are described by the URIs below:
| Name | http://home.netscape.com/NC-rdf#Name | The bookmark name |
| URL | http://home.netscape.com/NC-rdf#URL | The 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 we use these to fill in the field values automatically. Note that the last modified date has a slightly different namespace than the other three.
Below, a sample RDF 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-ficticious-zoo.com/rdf#">
<RDF:Seq about="urn:animals:data">
<RDF:li>
<RDF:Description about="urn:animals: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="urn:animals: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="urn:animals: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. With-in each record, three fields are described, name, species and type. 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. In this case, it was just made up so that it doesn't conflict with any other namespace that might be available. The name was selected because it has meaning in this case, but we could have selected something else. The namespace has been declared with a URL in the RDF tag. The namespace feature is useful since the class field would conflict with that used for styles. The URL was also made up here but you'd probably use something suitable.
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-ficticious-zoo.com/rdf#name |
| Species | http://www.some-ficticious-zoo.com/rdf#species |
| Class | http://www.some-ficticious-zoo.com/rdf#class |
(Next) Next, we'll see how we can use RDF to populate XUL elements.