Comparing Namespaces for XML and RDF

Author: Karsten Tolle

Status: Working Draft

Date of last change: 10-Sep-01

Note before reading:

As you can read in the ‘RDF Issue Tracking’ the RDF WG decided on 25th May 2001 that ALL attributes must be namespace qualified. This decision of the RDF WG will change some results of this draft.

 

Introduction

In this short paper I want to illustrate some differences in using namespaces within XML and RDF. RDF uses the XML syntax to provide a standardized, interoperable way for encoding and transporting (meta-)data. We would think that the usage of namespaces is the same for XML and RDF, but this is not the case. Before illustrating the peculiarities in RDF the usage of namespaces in XML is shortly reviewed.

Namespaces in XML

We can think of a namespace as an area with unique names. In XML the namespace itself is identified by an Uniform Resource Identifier (URI) [9]. The namespace mechanism is used in XML to avoid the ambiguous usage of names for elements and attributes. For this purpose a namespace might exist at the conceptual level but there is no need that an XML namespace must exist physical. See Myth 1 at [4] for more details. Of course there might be something at the URI of the namespace. We can state:

In XML namespaces do not need to exist.

Since namespaces are just an abbreviated form of writing unique names I introduce the idea of universal names similar as done in [2] for this paper. The combination of the namespace URI followed by a ‘#’ and the element or the attribute name (local names) is called the universal name. For the following example:

<?xml version="1.0"?>
  <s:Creator xmlns:s="http://somthing/index.html" s:date=”2001-07-05”>
       Karsten
  </s:Creator>

we would have http://somthing/index.html#Creator as the universal name for s:Creator and http://somthing/index.html#date for s:date. The attribute xmlns:s is a special attribute to declare a namespace. This is focused in the next paragraph.

How do I declare a namespace?

Namespaces can be declared by using the attribute names xmlns or xmlns:<prefix> followed by the URI specifying the namespace name. Without the prefix we are talking about the default namespace. For example:

<rdf:RDF
       xmlns = “anyURI”          (default namespace)
       xmlns:pre = “anyURI”
       xmlns:ns = “anyotherURI”> …

An empty namespace name (the value of the xmlns attribute) can only be used with the default namespace [1].

       xmlns:bad = “”          (illegal, may not)
       xmlns = “”               (legal)

When we use the empty string as the value for the default namespace this has the effect of there being no default namespace [1]. To be more precise, in [1] we read at 5.2: “…If the URI reference in a default namespace declaration is empty, then unprefixed elements in the scope of the declaration are not considered to be in any namespace. …”. This means that elements with no prefix in this case are not bound to any namespace. This is the same effect as if there is no definition of an default namespace. The following two examples are equal and the universal names for the element in both cases is just the local name Creator.

<?xml version="1.0"?>
  <Creator xmlns="" date=”2001-07-05”>
       Karsten
  </Creator>
 
<?xml version="1.0"?>
  <Creator date=”2001-07-05”>
       Karsten
  </Creator>

In [1] we can further read: ”… Note that default namespaces do not apply directly to attributes. …”. So, the default namespace only affect elements. Let us assume the following example without having an default namespace:

  <?xml version="1.0"?>
  <who forename=”Astrid” surname=”Tolle”/>

In this case the element who and the two attributes forename and surname do not belong to any namespace in the sense of the XML namespace specification. This means the universal names for this example would be who for the element and forename and surname for the attributes.

If we continue the example and say the default namespace is given and its value is not an empty string this will only affect the element itself and not the attributes.

  <?xml version="1.0"?>
  <foo xmlns="http://somthing/index.html">
       <who forename=”Astrid” surname=”Tolle”/>
  </foo>

The universal names in the example above would be http://somthing/index.html#foo, http://somthing/index.html#who, forename and surname.

All together we have seen that there exist elements and attributes in XML that do not belong to any namespace. See also Myth 4 of [4].

In XML elements and attributes do not need to be in any namespace.

How are they used?

For the scope of namespace declarations let us have a look at the original text and example from the specification:

The namespace declaration is considered to apply to the element where it is specified and to all elements within the content of that element, unless overridden by another namespace declaration with the same NSAttName part:

<?xml version="1.0"?>
<!-- all elements here are explicitly in the HTML namespace -->
<html:html xmlns:html='http://www.w3.org/TR/REC-html40'>
  <html:head><html:title>Frobnostication</html:title></html:head>
  <html:body><html:p>Moved to 
    <html:a href='http://frob.com'>here.</html:a></html:p></html:body>
</html:html>

All universal names in this example would belong to the namespace of  'http://www.w3.org/TR/REC-html40', only the attribute href wouldn’t.

So, the specification simply says that the prefix declaration is valid inside the element it was declared and all child elements of this element. The following would not be legal because the html:html element is not in the scope of the html namespace declaration:

<?xml version="1.0"?>
<html:html>
  <html:head xmlns:html='http://www.w3.org/TR/REC-html40'>
       <html:title>Frobnostication</html:title>
  </html:head>
  

When declaring the prefixes in one element they should be unique. The following would be illegal.

  <?xml version="1.0"?>
  <foo     xmlns:pre = “anyURI” xmlns:pre = “anyotherURI” …

It is not very nice because the human reader probably will get confused, but you are free to declare the same prefix in different elements. You just need to take into account that if the elements are in a father child relation you will overwrite the declaration of the father inside the content of the child. For more details in overriding of namespace see section 4 of [3].

New developments

In technologies being developed today like XML Schema we can find namespaces also inside of values, e.g.,

  <xs:complexType name="paraType" mixed="true">
       
      <xs:attribute name="version" type="xs:number"/>
  </xs:complexType>

In this case the values of the attribute type is restricted to be of QName. To use the namespace prefix in this manner is not stated in the namespace specification. But it would be very convenient to do it this way. The problem is that existing applications probably do not recognize it as an qualified name and will just treat it as a string value which will cause errors. See [3] section 10 for more details.

Namespaces in RDF

RDF uses the XML syntax to provide a standardized, interoperable way for encoding and transporting (meta-)data. We would think that the usage of namespaces is the same for XML and RDF, but this is not the case. Therefore I discuss in the following paragraphs the peculiarities of RDF and the effect on the usage of namespaces.

Existence of the namespaces in RDF

In XML the namespace URI you give can be anything corresponding to the definition of a URI [9]. You do not need to find anything behind it to have a valid XML file. In RDF this differs. Using a namespace in RDF means that you use either a class or a property out of this namespace. To make a full validation and to be sure of using this resources in the right sense the schema definition of the corresponding resource must be found at the URI of the namespace. We need to know about super/sub classes (for classes) or range and domain definitions (for properties). This can be compared with the existence of an DTD or XML Schema for an XML file to validate it against it.

In RDF namespaces must exist.

Note: If you just refer to a resource by RDF like a description about something, this has nothing to do with namespaces. Therefore the URI for resources does not need to exist.

Attributes in RDF

There are some (real) attributes in RDF having to do with the interpretation of the element and can not directly be found in other representations of RDF (triple, DLG). These attributes are: about, aboutEach, aboutEachPrefix, ID, bagID, resource, parseType and special xml attributes like xmlns, xml:lang.

·      about, aboutEach and aboutEachPrefix can only be used within a description or a typedNode specifying which resource(s) is(are) the subject of the following triples.

·      ID and bagID generating new resources.

·      parseType specifies the way the following content should be treated by a parser.

·      resource specifies that the object of the generated triple is a resource and not a literal.

·      xmlns for declaring a namespace

·      xml:lang for declaring a language (no effect on other representations)

Beside these attributes in RDF only exist properties written as attributes (propAttr). In RDF we have properties that can (in simple cases) be expressed either by elements or by attributes. In these cases the value of the property must be a literal. In XML the DTD or the XML Schema specifies whether something is an attribute or an element but you are not free to choose. One problem is that if we look at namespaces especially the default namespace we see that elements and attributes are differently treated in XML.

Further we can state that in XML attributes can belong to no namespace. For properties this should not be the case. They need to be defined somewhere in a RDF schema. See also Existence of namespaces in RDF above.

This results in the question in contradiction to the specification in XML: “Should every attribute in RDF belong to a namespace?” Since properties must belong to a namespace the question can be reduced to: “Do the (real) attributes in RDF also belong to a namespace?”.

To answer this question I will leave out special XML attributes like xmlns, xml:lang or any attribute starting with the letters x, m, l as stated in the “Namespace Constraint: Leading “XML”” [1]. For more consistency we could think of them as if they would belong to a special XML namespace.

In practice we can find in the RDF Schema specification the usage of the attributes about, ID and resource as if they would belong to the rdf namespace (http://www.w3.org/1999/02/22-rdf-syntax-ns). Below we can see a part of the XML serialization of RDF Schema [6] using the attributes ID, about and resource with the rdf namespace.

<rdf:RDF
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdfs:ConstraintProperty rdf:ID="range">
  <rdfs:label xml:lang="en">range</rdfs:label>
  <rdfs:label xml:lang="fr">étendue</rdfs:label>
  <rdfs:comment>Properties that can be used in a
  schema to provide constraints</rdfs:comment>
  <rdfs:range rdf:resource="#Class"/>
  <rdfs:domain rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"/>
</rdfs:ConstraintProperty>
 
<rdfs:Class rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#Property">
  <rdfs:label xml:lang="en">Property</rdfs:label>
  

For the bold printed attributes we would generate the following universal names:

QName

universal name

rdf:ID

http://www.w3.org/1999/02/22-rdf-syntax-ns#ID

rdf:resource

http://www.w3.org/1999/02/22-rdf-syntax-ns#resource

rdf:about

http://www.w3.org/1999/02/22-rdf-syntax-ns#about

Using rdf:resource and rdf:parseType can also be found in examples of the RDF Model and Syntax Specification [7] (page 37). This results in the acceptance that the attributes about, aboutEach, aboutEachPrefix, ID, bagID, resource and parseType belong to the rdf namespace. Even if we can not find them in the schema description itself. This means:

Attributes in RDF always belong to a namespace (xmlns, xml:lang excluded).

If we accept this the question arises: “What happens with attributes without prefix (unqualified), which namespace do they belong to?”

To answer this question let us view an example of the RDF Schema specification.

<rdf:RDF xml:lang="en"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
 
<rdf:Description ID="registeredTo">
 <rdf:type resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Property"/>
 <rdfs:domain rdf:resource="#MotorVehicle"/>
 <rdfs:range rdf:resource="#Person"/>
</rdf:Description>

In this example we can see both usages of the attribute resource qualified (rdf:resource) and unqualified (resource). Of course in both cases we are talking about the same attribute. Which means they should belong to the same namespace (rdf) and therefore have the same universal name. The idea here is that the attribute inherits the namespace from the element. This results in the universal name http://www.w3.org/1999/02/22-rdf-syntax-ns#resource for both attributes. If we allow this inheritance of namespaces from the element to its attributes it makes even more sense to say “Default namespaces do not apply directly to attributes.” as the specification for namespaces in XML [1] does. For the example above we could enter a default namespace without the need to change anything the universal names would stay the same. This inheritance mechanism differs from XML but seems to be quite useful for RDF. This means:

In RDF the unqualified attributes inherit the namespace of the element they belong to.

Note: A similar inheritance system can be found in XML Schema Part 0: Primer [8].

Elements in RDF

What about elements in RDF. Elements in RDF are either from the rdf namespace, a propertyElt or we have a typedNode. A propertyElt starts with a property and therefore must belong to a namespace as stated earlier. A typedNode is just an abbreviated form of saying something being of a special type, which also could be expressed with the rdf:type property. The typedNode just begins with value of the type property expressed by a QName. For example the property registeredTo (see above) could also be written as:

<rdf:RDF xml:lang="en"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
 
<rdf:Property ID="registeredTo">
  <rdfs:domain rdf:resource="#MotorVehicle"/>
 <rdfs:range rdf:resource="#Person"/>
</rdf:Property>

Since the value of the rdf:type attribute is restricted to be a class and each class must belong to a namespace, we can be sure that any element in RDF must belong to a namespace.

In RDF every element must belong to a specific namespace.

So, what happens if we have unqualified elements without having a default namespace? An example is given in the RDF Schema specification [6]:

<rdf:RDF xml:lang="en"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
 
<rdfs:Class rdf:ID="Person">
  <rdfs:comment>The class of people.</rdfs:comment>
  <rdfs:subClassOf rdf:resource="http://www.w3.org/2000/03/example/classes#Animal"/>
</rdfs:Class>
<rdfs:Class rdf:ID="MaritalStatus"/>
 
<MaritalStatus rdf:ID="Married"/>
<MaritalStatus rdf:ID="Divorced"/>
<MaritalStatus rdf:ID="Single"/>
<MaritalStatus rdf:ID="Widowed"/>
 
</rdf:RDF>

The four elements at the end of the example declaring a new resource of the type MarialStatus. This is the class declared in the element above the four elements. So, in this case unqualified elements without the existence of a default namespace belong to the namespace generated by the current file. This is something we should note:

Each RDF file represents a namespace.

This is because in RDF you are free to mix metadata and RDF Schema as done in the example above. The universal name for the class MaritalStatus in this case depends on the way the file was currently opened. If the file lets name it status.rdf is inside your file system the universal name could be file:/<path>/status.rdf#MaritalStatus. If the file would have been called via the web the universal name could be http://www.<domain>/<path>/status.rdf#MaritalStatus.

Having no namespace declared is in XML the same as using the default namespace with an empty value. To be consequent this means entering the default namespace with empty value in the example above would not change the universal names. So, the empty value of a default namespace in RDF would refer to the current file.

To refer to the current file in RDF is done quite often. For a better reading it would be useful in these cases to declare a prefix for the current file. In XML it is not allowed to use an empty value for declaring a non default namespace. For RDF this constraint should be rethought. For example the schema of the Dublin Core Element Set (Appendix B in the old version of [6]) used xmlns:DC=””

A non default namespace declared with an empty value should be allowed.

Conclusion

To bring it all together we have the following differences in using namespaces in XML and RDF:

XML

RDF

Namespaces do not need to exist.

Namespaces must exist.

Elements and attributes do not need to belong to a namespace.

Elements and attributes must belong to a namespace.

Unqualified attributes do not inherit the namespaces of the element they belong to.

Unqualified attributes inherit the namespace of the element they belong to.

An XML file does not represent a namespace.

Each RDF file represents a namespace.

A non default namespace declared with an empty value is not allowed.

A non default namespace declared with an empty value should be allowed.

 

Existing parsers like VRP [10] and SIRPAC [11] already implemented the differences in using namespaces as described above.

 


Appendix A: Formal Grammar for RDF (BNF)

  [6.1] RDF            ::= ['<rdf:RDF>'] obj* ['</rdf:RDF>']

  [6.2] obj            ::= description | container

  [6.3] description    ::= '<rdf:Description' idAboutAttr? bagIdAttr? propAttr* '/>'

                         | '<rdf:Description' idAboutAttr? bagIdAttr? propAttr* '>'

                                propertyElt* '</rdf:Description>'

                         | typedNode

  [6.4] container      ::= sequence | bag | alternative

  [6.5] idAboutAttr    ::= idAttr | aboutAttr | aboutEachAttr

  [6.6] idAttr         ::= ' ID="' IDsymbol '"'

  [6.7] aboutAttr      ::= ' about="' URI-reference '"'

  [6.8] aboutEachAttr  ::= ' aboutEach="' URI-reference '"'

                         | ' aboutEachPrefix="' string '"'

  [6.9] bagIdAttr      ::= ' bagID="' IDsymbol '"'

 [6.10] propAttr       ::= typeAttr

                         | propName '="' string '"' (with embedded quotes escaped)

 [6.11] typeAttr       ::= ' type="' URI-reference '"'

 [6.12] propertyElt    ::= '<' propName idAttr? '>' value '</' propName '>'

                         | '<' propName idAttr? parseLiteral '>'

                               literal '</' propName '>'

                         | '<' propName idAttr? parseResource '>'

                               propertyElt* '</' propName '>'

                         | '<' propName idRefAttr? bagIdAttr? propAttr* '/>'

 [6.13] typedNode      ::= '<' typeName idAboutAttr? bagIdAttr? propAttr* '/>'

                         | '<' typeName idAboutAttr? bagIdAttr? propAttr* '>'

                               propertyElt* '</' typeName '>'

 [6.14] propName       ::= Qname

 [6.15] typeName       ::= Qname

 [6.16] idRefAttr      ::= idAttr | resourceAttr

 [6.17] value          ::= obj | string

 [6.18] resourceAttr   ::= ' resource="' URI-reference '"'

 [6.19] Qname          ::= [ NSprefix ':' ] name

 [6.20] URI-reference  ::= string, interpreted per [URI]

 [6.21] IDsymbol       ::= (any legal XML name symbol)

 [6.22] name           ::= (any legal XML name symbol)

 [6.23] NSprefix       ::= (any legal XML namespace prefix)

 [6.24] string         ::= (any XML text, with "<", ">", and "&" escaped)

 [6.25] sequence       ::= '<rdf:Seq' idAttr? '>' member* '</rdf:Seq>'

                         | '<rdf:Seq' idAttr? memberAttr* '/>'

 [6.26] bag            ::= '<rdf:Bag' idAttr? '>' member* '</rdf:Bag>'

                         | '<rdf:Bag' idAttr? memberAttr* '/>'

 [6.27] alternative    ::= '<rdf:Alt' idAttr? '>' member+ '</rdf:Alt>'

                         | '<rdf:Alt' idAttr? memberAttr? '/>'

 [6.28] member         ::= referencedItem | inlineItem

 [6.29] referencedItem ::= '<rdf:li' resourceAttr '/>'

 [6.30] inlineItem     ::= '<rdf:li' '>' value </rdf:li>'

                         | '<rdf:li' parseLiteral '>' literal </rdf:li>'

                         | '<rdf:li' parseResource '>' propertyElt* </rdf:li>'

 [6.31] memberAttr     ::= ' rdf:_n="' string '"' (where n is an integer)

 [6.32] parseLiteral   ::= ' parseType="Literal"'

 [6.33] parseResource  ::= ' parseType="Resource"'

 [6.34] literal        ::= (any well-formed XML)

 

 


Appendix B: References

[1]             Namespaces in XML, W3C Recommendation, 14 January 1999, www.w3.org/TR/REC-xml-names

[2]        XML Namespaces, James Clark, last update 4 February 1999, www.jclark.com/xml/xmlns.htm

[3]        XML Namespaces FAQ, Ronald Bourret, last update March 2001, www.rpbourret.com/xml/NamespacesFAQ.htm

[4]             Namespace Myths Exploded, Ronald Bourret, 8 March 2000, www.xml.com/pub/a/2000/03/08/namespaces/index.html

[5]        XML Coverpages, http://xml.coverpages.org/namespaces.html

[6]            Resource Description Framework (RDF) Schema Specification 1.0, W3C Candidate Recommendation, 27 March 2000, www.w3.org/TR/2000/CR-rdf-schema-20000327

[7]             Resource Description Framework (RDF) Model and Syntax, W3C Recommendation, 22 February 1999, www.w3.org/TR/1999/REC-rdf-syntax-19990222

[8]        XML Schema Part 0: Primer, W3C Recommendation, 2 May 2001, www.w3.org/TR/2001/REC-xmlschema-0-20010502/

[9]            Uniform Resource Identifiers (URI): Generic Syntax, T. Berners-Lee, R. Fielding, U.C. Irvine and L. Masinter, August 1998, RFC2396, www.ietf.org/rfc/rfc2396.txt

[10]            Validating RDF Parser (VRP), www.ics.forth.gr/proj/isst/RDF/

[11]            SIRPAC - Simple RDF Parser & Compiler, www.w3.org/RDF/Implementations/SiRPAC/