RDF, Resource Description Framework, how to use

According to W3C specifications and Mozilla documentation
Summary

What is RDF?

The RDF format allows to mix together two XML documents into a single one, describing the relations in the data, and may br used by web apps to describe in the data file itself how may be displayed by the graphical interface of the program.

When use RDF?

This format is convenient when the data must have a structure similar to that of the tool that uses it, usually a graphical interface.
When we want also to aggregate separated kinds of data from separated documents, also. For example, local and remote documents.
We are mostly concerned here by combining the graphical interface and the data to fill the widgets.
RDF is used by Firefox to create bookmarks and other dynamic displays, and by XULRunner to fill tables and trees.
It may be used for localization in foreign languages, as texts and code are separated.

XML or RDF?

RDF is complicated. Can we use directly XML to fill lists and trees in XUL? There is a detailed article about that on the Mozilla website.
We can load the XML document and use DOM's methods, to add elements one by one. But there is a lot of code to write while that is implemented natively in XULRunner when using RDF...
RDF on the other hand, is quite simple inside XUL code, just a rule for all items in the list, and change and saving are automatically handled.
RDF allows to use same data in different views thanks to relations in the document, and this avoid you to write the corresponding code.
I hope in the future that tools to generate RDF file more easily will become available.
To conclude, use RDF and keep the XUL code simple or use XML and overload XUL with all the code to process it.

Getting started with RDF

Resource

A resource may be:

A resource is denoted by an URI (a local or remote address of a document).

Property and declaration

A property is an attribute of a resource or a relation involving a resource.
Features of properties are defined by an RDF schema.
A declaration is a triple:

  1. a resource,
  2. a property of this resource,
  3. and a value for this property.

This value may be another URI, or a literal, a string for example.
This model comes from the logic, it is an application of the triple: subject, predicate, object or value.

Example of declaration

Here is a simple statement:

 Wells is the author of the book: The Invisible Man.

This is converted to RDF components:

Subject / Resource: https://www.xul.fr/Wells
Predicat / Property: Author 
Object / Literal: "The Invisible Man"

The formal RDF writing becomes:

<rdf>
  <Description about="https://www.xul.fr/Wells">
<s:author>The Invisible Man</s:author> </Description> </rdf>

This syntax is described below.

Name spaces

A name space is a field where an RDF schema is applied.
An RDF document makes use of two name spaces at least, the RDF one, and the name of the domain of the data.
The name space is designated by the xmlns attribute.

Example of namespace attribute, here RDF:

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

Once the RDF name space is defined, the "rdf:" prefix becomes useless providing the xmlns attribute is associated to the container of the rule.

Example of global namespace:

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

The complete example

Including the triple and name space definitions:

<?xml version="1.0 ?>
<rdf xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:s="http://description.org/schema/" > 
  <Description about="https://www.xul.fr/Wells">
     <s:author>The Invisible Man<s:author>
  </Description>
</rdf>

Vocabulary and symbols

Resource: This is a set of statements and this is an RDF document.
Statement: A statement associates a data (a literal or a resource) to the element that uses it.
Literal: This is a text.
Triple: A complex statement with a subject, a predicate, an object.

Symbol *: the next element, while one exists.
Symbol #: an element inside an RDF document.
- the start of the document if the symbol is not followed by a word.
- the current document if the symbol is not preceded by an address.
Symbol ?: is a XUL symbol, when using RDF. This is the prefix of a variable. (The whole string between quotes). The content of the variable will be used.

More on the RDF syntax

Datasource and URI

A template is filled from a data source, that is a file in order.
The address is given by an attribute of the higher container of the template in the format: datasources="..."

An URI is either an URL, remote or local, or a local component.

datasources=rdf:null
This data source is empty. In this case, data are dynamically added to.

RDF statements, or triples

A triple is a tag with two attributes. The subject is the value of the about attribute. The predicate is the second attribute and the object is the value of this second attribute.
For example, the subject is X, the predicate is the name of X, the object is Sandra.

<rdf:rdf
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
   xmlns:people="people/" >
   <description 
   about="X"
   name="Sandra" />
</rdf>

It is possible to add more predicates to the statement, with more attributes.  

<description
   about="X"
   people:name="Sandra"
   people:genre="Female"
/> 

We can also create a description tag for each new attribute with the same subject.

<description
  about="X"
  people:genre="Female" /> 

The predicate may be a sub-tag. The two forms, sub-tag or attributes are equivalent.

<description about="X" >
    <people:name> "Sandra" </people:name>
</description> 

Attributes and sub-elements may be combined into one element.
A triple may create a relation between two resources rather than literals, in this case the resource attribute is used instead.
The value of the type predicate, that denotes the type of an object may be exceptionally used in tag form.

<rdf 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"                      
   xmlns:world="world"    
   xmlns:people="people/">
   <world:person about="http://www.xulplanet.com/rdf/people/Sandra">
   </world:person> 
</rdf> 

The type "person" in the "world" name space, is now a sub-element and replaces "description" with a "type" attribute and the value "person".

RDF elements

Any element defined inside RDF are resources. Resources are specialized as classes, containers, property, etc.

Resources

resource: Object.

Classes

datatype: a kind of data.
literal: a literal value.

Elements

description Tag to describe a relation between a subject and an object.
bag: list of unordered elements.
seq: (sequence) list or ordered elements.
alt: (alternate) list of element to choose.
li: element of a list.

Attributes

about:the subject of the relation.
label
: label of a resource.
type: type of an object.
comment: to describe a resource.

Attributes of elements

xml:lang: The human language for the document. Must be at top of the hierarchy.
rdf: about: designates a subject. May be a url.
   Example rdf:about="mydocument.html"
   Example rdf:about="https://www.xul.fr/en/document.html"
parseType: Type of processing for the content of the tag. The value is "literal" for an XML content or "resource" for an RDF content.

Example

The first XML document holds a list of names of animals.

<animals>
  <name>Tarantula </name>
  <name>Emu </name>
  <name>Barn Owl</name>
</animals >
An XML document

The second XML document holds a list of species.

<animals> 
  <species>Avicularia avicularia</species>
  <species>Dromaius novaehollandiae</species> 
  <species>Tyto alba</species>
</animals>
Another XML document

These two documents are now joined. The about attribute designates the animal by a code, a tag links it with the name and another one with the species. The XUL display should reflect these relations.

<rdf:description rdf:about="animals"> 
  <animals:name>animals</animals:name>
</rdf:description>

<rdf:description rdf:about="arachnids/tarantula" > <animals:name>Tarantula</animals:name> <animals:species>Avicularia avicularia</animals:species> </rdf:description>
<rdf:description rdf:about="birds/emu" > <animals:name>Emu</animals:name> <animals:species>Dromaius novaehollandiae</animals:species> </rdf:description>
<rdf:description rdf:about="birds/barnowl" > <animals:name>Barn Owl</animals:name> <animals:species>Tyto alba</animals:species> </rdf:description>
Associating the two document in the RDF syntax

The structure of data.
The about attribute gives an identifier to the item in the list, while the resource attribute links to the corresponding description.
Now the "animals" item previously described separately, encloses other items.

<rdf:seq rdf:about="animals" > 

  <rdf:seq rdf:about="animals/arachnids/tarantula"> 
    <rdf:li rdf:resource="arachnids/tarantula"/>
  </rdf:seq>

  <rdf:seq rdf:about="animals/birds/emu"> 
    <rdf:li rdf:resource="arachnids/tarantula"/>
</rdf:seq> <rdf:seq rdf:about="animals/arachnids/tarantula"> <rdf:li rdf:resource="arachnids/tarantula"/>
</rdf:seq> </rdf:seq>
Creating a structure usable by XUL

Now, the XUL source using this RDF document.
For each non-RDF attribute, and which are in the "animals" name space, name and species, exists a cell in the table.

<tree datasource="animals.rdf" ref="animals">
  <template> 
    <rule>
      <treechildren>
         <treeitem uri="rdf:*">
           <treerow>
             <treecell label="rdf:rdf#name"/> 
             <treecell label="rdf:rdf#species"/>
           </treerow>
         </treeitem>
      </treechildren> 
     </rule> 
  </template> 
</tree>
XUL file displaying the RDF document

Describing the XUL format for an RDF document...
A template tag encloses one unit used as model, and each unit in the document will be generated according to this model.
Its content may be repeated n times, for n element in the RDF file.
The RDF document is designated by the datasource attribute.
A rule, is a container for an RDF block:
- conditions are rules with an attribute the value of which must be true.
- action are rules that actually associate RDF data to XUL widgets.
The uri attribute, uri="rdf:*" in the example, is a maker that points out the start of the association XUL and RDF. A set of assignments start here.

<tag datasource="animals.rdf" >
  <template>
    <rule>
      <conditions>
         ...
      </conditions>
      <action>
         ...
      </action>
    </rule>
    <rule> 
      .....etc...
</template> </tag>
General XUL format to use an RDF file

Full example: animals

Converting XML to RDF

To use an XML document into a XUL application, a script is required to convert XML to RDF.
The script has to use an RDF element as model, and will generate all other ones for each element in the XML document.

RDF applications

XUL and RDF

By using RDF, you can create complete lists and trees, with just the first element defined. For each entry in the RDF file, one element will be displayed.
To make the interface so dynamic you have just to enclose the widget between these tags:

<template>
     ...
template>

The RDF document is given by the datasource attribute. It is an attribute of the whole table or tree. The uri attribute marks the XUL element as being a line to display and the # sign followed by an RDF tag or attribute denotes the data to insert.

<tree datasource="animals.rdf">
  <template> 
    <treechildren>
      <treeitem uri="rdf:*">
        <treerow>
          <treecell label="rdf:rdf#name"/> 
          <treecell label="rdf:rdf#species"/>
        </treerow>
      </treeitem>
    </treechildren> 
  </template> 
</tree>

The RDF name space is added for data tags, as here tags are XUL's tag by default.

RSS

RDF is one of the formats used to create RSS feeds: "RDF Site Summary". The RSS 1.0 Tutorial describes an exemple of use of the RDF format for creating a syndication feed.

Dublin Core

It is a standard name space to describe oeuvres or documents.
The short name is dc and elements are: author, title, subject, etc.

<dc:author> Shakespeare </dc:author>

JSON-LD

RDF is derived from XML and for those who prefer the C-Like syntaxe, another format, JSON-LD (JSON for Linked Data) has been defined by the W3C as an alternative to RDF. Like RDF, it allows to describe in the documents how data are connected.

See: JSON-LD. Documentation and specification ot this alternative format.

Links

© 2006-2021 Xul.fr