Namespaces

Jean-François Perrot


  1. Principle

  2. Syntax
    1. Colon as separator
    2. Shorthand : prefixes
    3. Default namespace

  3. Examples

  4. Namespaces in DOM-Java
    1. Documents created by a DocumentBuilder (parser)
    2. Empty Documents created by a DOMImplementation
    3. DOM manipulation of namespace aware documents



  1. Principle

    1. Names of tags and of attributes do carry meaning, hence they are ambiguous
      • there is no such thing as a word having a single meaning...
      • the same problem appears in many places, e.g. in programming languages.

    2. Namespaces are a way to resove ambiguity.

      A (full) name is made up of 2 parts :
      • a local part, supposed to be unambiguous inside a certain domain called namespace :
        e.g. "title" or "head" or "body" in the domain "XHTML"
      • a name for the domain, supposed to absolutely unambiguous : is this possible ?


    3. URIs are supposed to be unambiguous : they will be used as namespace names.
      e.g. "http://www.w3.org/1999/xhtml" for the domain "XHTML"

      Note that in this function URIs are pure symbols, they do not point to any resource, they are non-dereferenceable.
      If they do point to some resource, this resource has no specific property : e.g. "http://www.w3.org/1999/xhtml"

  2. Syntax

    1. Colon as separator

      A full name (or qualified name) is written with (1)  the namespaee (2) the local name, separated by a colon.
      e.g. two different notions of title
      • http://www.w3.org/1999/xhtml:title (title in XHTML)
      • http://purl.org/dc/elements/1.1/:title (title according to the Dublin Core Metadata)

    2. Shorthand : prefixes

      1. Namespace URIs are seldom used directly (too long, making the document illegible).
        They are ususally abbreviated as prefixes
        Note that these prefixes exist only in the text that represents the document as a file,
        in the (DOM object) document itself only URIs are known.

      2. Prefixes are declared alongside attributes in an opening tag,
        in the form :  xmlns:prefix = URI

        They remain valid for all chidren of the tag.
        Very often the tag is the root tag of the document...
        e.g.
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

        Caution : namespace prefixes are NOT attributes, although they look the same and appear in the same places !

      3. The 'xml' prefix  is supposed to be implicitly defined in every XML document.
        The corresponding URL is http://www.w3.org/XML/1998/namespace.

      4. Example with 2 namespace prefixes rdf and epita :
        See here for background about the RDF formalism and here about the names & marks data set.
        File rNN2.rdf

        <?xml version="1.0" encoding="utf-8"?>
        <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                 xmlns:epita="http://epita/masters/international/"
                 xml:base="http://epita/masters/international/perso">
          <rdf:Description rdf:about="#Elisabeth"><epita:mark>07</epita:mark></rdf:Description>
          <rdf:Description rdf:about="#Luc"><epita:mark>12</epita:mark></rdf:Description>
          <rdf:Description rdf:about="#Maurice"><epita:mark>18</epita:mark></rdf:Description>
          <rdf:Description rdf:about="#Juliette"><epita:mark>07</epita:mark></rdf:Description>
        </rdf:RDF>



        Note that
        • xmlns:rdf and xmlns:epita are not attributes of the RDF tag.
        • xml:base is an attribute, the name of which belongs to the predefined xml namespace.
        • In this example, both tags and attributes names carry a namespace prefix.
          Usually, only tags carry namespaces.
          This point will reappear when we will specify this using XML Schemas.

    3. Default namespace

      • Empty prefix: Very often, most names in a document belong to the same namespace.
        As a further shorthand, this namespace may be represented by an empty prefix, without colon.
        It is then called the default namespace.
        Ex. XHTML
        <html xmlns="http://www.w3.org/1999/xhtml">
           <head>
              <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
              <title>M2-IM</title>
              <style type="text/css">....


      • But note that this is merely a convenience for making the text easier to read.
        From the point of view of the (DOM) document, the default namespace is a namespace like all others...
        Indeed, quite often using an empty prefix is prohibited !

        However, some pieces of software may think otherwise : most browsers won't interpret properly our basic example below
        it it is written with a non-empty prefix for html...

        To make this more precise, note that the defaut namespace applies to tags (elements) only,
        the namespace of attributes being determined by the element on which they appear [Ref.].
        However, some disagree with this interpretation : the JavaDoc on DOMNode:getNamespaceURI says:

        Note: Per the Namespaces in XML Specification [XML Namespaces] an attribute does not inherit its namespace from the element it is attached to. If an attribute is not explicitly given a namespace, it simply has no namespace.


      • Basic example

        <?xml version="1.0" encoding='ISO-8859-1'?>
        <html xmlns="http://www.w3.org/1999/xhtml"
              xmlns:xdc="http://www.xml.com/books">
         <head><title>Book Review</title></head>
         <body>
          <xdc:bookreview>
           <h1><xdc:title style="font-family: sans-serif;">XML: A Primer</xdc:title></h1>
           <table>
            <tr align="center">
             <td>Author</td><td>Price</td>
             <td>Pages</td><td>Date</td>
            </tr>
            <tr align="left">
             <td><xdc:author>Simon St. Laurent</xdc:author></td>
             <td><xdc:price>31.98</xdc:price></td>
             <td><xdc:pages>352</xdc:pages></td>
             <td><xdc:date>1998/01</xdc:date></td>
            </tr>
           </table>
          </xdc:bookreview>
         </body>
        </html>



  3. Examples

  4. Namespaces in DOM-Java

    By default DOM-Java ignores namespaces, i.e. DOM documents are not namespace aware.

    But if more complex manipulations are used (XSLT transforms, validity checking), then
    in order to properly manage documents with namespaces, some precautions are necessary.
    1. Documents created by a DocumentBuilder (parser)

      A DocumentBuilder p parses a file to create a Document doc : we want doc to be namespace aware.
      Then p must have been created by a DocumentBuilderFactory which was namespace aware.

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);  // default is "false"
      DocumentBuilder p = dbf.newDocumentBuilder();
      Document doc_xsl =  p.parse(myFile.xml);


    2. Empty Documents created by a DOMImplementation

      The DOMImplementation object was obtained from a DocumentBuilder,
      that must have been created by a DocumentBuilderFactory which was namespace aware.

      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      DOMImplementation domi = dbf.newDocumentBuilder().getDOMImplementation();
      Document doc = domi.createDocument(nmsURI, root, null);


      where
      • root is the qualified name of the root tag of the document
      • nmsURI is the corresponding namespace URI.
      • the third argument (if not null) is the DocumentType, i.e. a Java object representing the DTD.

      Note that the specification that root be a QName (= prefix:localName) means that a prefix for the namespace is implicitly declared. A document like our RDF example would have been created as

      String nmsURI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
      Document doc = domi.createDocument (nmsURI, "rdf:RDF", null);


      The namespace will implicitly be declared as default by writing root without a prefix.
      A document like our createElement(String tagName) Basic Example would have been created as

      String nmsURI = "http://www.w3.org/1999/xhtml";
      Document doc = domi.createDocument (nmsURI, "html", null);


    3. DOM manipulation of namespace aware documents

      A number of heavily used DOM methods that take a name argument have counterpart "with namespace", e.g.

      • in Document : createElement(String tagName)  --> createElementNS(String namespaceURI, String qualifiedName)
      • in Element : getAttribute(String name) --> getAttributeNS(String namespaceURI, String localName) 
      • id. setAttribute(String name, String value) --> setAttributeNS(String namespaceURI, String qualifiedName, String value)
      • id. getElementsByTagName(String name) --> getElementsByTagNameNS(String namespaceURI, String localName)

      These methods belong the the 2nd level of the DOM specification (DOM level 2).
      Example : BuildRfromText.
      We wiil soon meet situations where they must be used !