XML technologies / XML / XML in a Nutshell / Namespaces / Syntax for namespaces

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

Namespaces distinguish elements with identical names by assigning URIs to elements and attributes. Generally, all elements of one XML application are assigned one URI, while all elements and attributes of another application are assigned another. These URIs are called namespace names. They divide elements and attributes into disjoint sets. Elements with identical names but different URIs have different types; those with identical names and URIs are the same. Usually namespaces and XML applications have a one-to-one correspondence, although some applications use several namespaces to distinguish their parts. XSL, for instance, uses separate namespaces for XSL Transformations (XSLT) and XSL Formatting Objects (XSL-FO).

Qualified names, prefixes and local parts

Because URI names often contain characters such as /, % and ~ that are not permitted in XML names, short prefixes such as rdf and xsl are used instead in element and attribute names. Each prefix is associated with a URI. Names whose prefixes map to the same URI belong to the same namespace; names whose prefixes map to different URIs belong to different namespaces. Prefixed elements and attributes in namespaces have names containing exactly one colon:

rdf:description
xlink:type
xsl:template

Everything before the colon is the prefix ; everything after it is the local part. The complete name, including the colon, is called a qualified name, QName or Raw Name. The prefix identifies the element’s or attribute’s namespace. The local part identifies the particular element or attribute within it.

In a document containing both SVG and MathML elements named set, one might be a svg:set element and the other a mathml:set element, eliminating confusion. In an XSLT stylesheet transforming documents into XSL formatting objects, the XSLT processor would recognize elements prefixed xsl as XSLT instructions and those prefixed fo as actual result elements.

Prefixes may contain any character allowed in XML names except colon. The three-letter prefix xml, used for the standard XML attributes xml:space, xml:lang and xml:base, is always bound to the URI "http://www.w3.org/XML/1998/namespace" and need not be explicitly declared. Other prefixes beginning with the three letters xml in any case combination are reserved for XML and its own specifications. Otherwise, name your prefixes as you wish. Namespaces impose another XML restriction: the local part may not contain colons. Colons in XML are therefore used exclusively to separate the namespace prefix from the local part in a qualified name.

Binding prefixes to URIs

Every prefix in a qualified name must be associated with a URI. For example, all XSLT elements are associated with the URI "http://www.w3.org/1999/XSL/Transform". The conventional prefix xsl replaces this long URI.

Note: You cannot use the URI directly in the name. In particular, the slashes present in most URIs are illegal in XML names. Sometimes, however, using the full name instead of a particular prefix is useful. A convention found on many XML mailing lists and in documentation places the URI in braces before the name. For example, the qualified name xsl:template could also be written as {http://www.w3.org/1999/XSL/Transform}template.
Another possibility appends the local name after a hash (#) to the URI, creating a URI fragment identifier such as "http://www.w3.org/1999/XSL/Transform#template". Both forms merely facilitate communication between human readers when the URI matters but the prefix does not. Neither an XML parser nor an XSLT processor accepts or understands this long form.

Bind prefixes to namespace URIs by adding an xmlns:präfix attribute to the element concerned or one of its ancestors. (The präfix is only a placeholder and must be replaced with the actual prefix.) For instance, the attribute xmlns:rdf of this rdf:RDF element binds the prefix rdf to the namespace URI "http://www.w3.org/TR/REC-rdf-syntax#":

<rdf:RDF xmlns:rdf="http://www.w3.org/TR/REC-rdf-syntax#">
   <rdf:Description about="http://www.cafeconleche.org/examples/impressionists.xml">
      <title>Impressionistische Gemälde</title>
      <creator>Elliotte Rusty Harold</creator>
      <description>Eine Liste berühmter impressionistischer Gemälde, geordnet nach Maler und Erstellungsdatum</description>
      <date>2000-08-22</date>
   </rdf:Description>
</rdf:RDF>

Bindings apply within the declaring element and its content. The attribute xmlns:rdf declares the prefix rdf for the element rdf:RDF and its descendant elements. An RDF processor recognizes rdf:RDF and rdf:Description as RDF elements because both have prefixes bound to the particular URI specified by RDF. It does not consider the elements title, creator, description and date to be RDF elements, since their prefixes are not bound to the URI "http://www.w3.org/TR/REC-rdf-syntax#".

Declare a prefix on the highest element using it or on an ancestor. This may be the document’s root element or an element lower down. For example, the Dublin Core elements could be assigned to the namespace "http://purl.org/dc/" by adding to the element rdf:Description the attribute xmlns:dc as the next example shows, since all Dublin Core elements in this document occur inside one rdf:Description element. In documents with more widely distributed elements, declaring the namespace on the root may be more convenient. If necessary, one element can hold several namespace declarations for different prefixes.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<catalog>
   <rdf:RDF xmlns:rdf="http://www.w3.org/TR/REC-rdf-syntax#">
      <rdf:Description xmlns:dc="http://purl.org/dc/" about="http://www.cafeconleche.org/examples/impressionists.xml">
         <dc:title>Impressionistische Gemälde</dc:title>
         <dc:creator>Elliotte Rusty Harold</dc:creator>
         <dc:description>Eine Liste berühmter impressionistischer Gemälde, geordnet nach Maler und Erstellungsdatum</dc:description>
         <dc:date>2000-08-22</dc:date>
      </rdf:Description>
   </rdf:RDF>   
   <painting>
      <title>Memory of the Garden at Etten</title>
      <artist>Vincent Van Gogh</artist>
      <date>November 1888</date>
      <description>Zwei Frauen schauen nach links. Eine dritte arbeitet in ihrem Garten.</description>
   </painting>
   <painting>
      <title>The Swing</title>
      <artist>Pierre-Auguste Renoir</artist>
      <date>1876</date>
      <description>Ein junges Mädchen auf einer Schaukel. Zwei Männer und ein kleines Kind schauen zu.</description>
   </painting>
   <!-- Viele weitere Gemälde... -->
</catalog>

Code example: A document containing both RDF and Dublin Core

A DTD for this document may specify different content models for the elements dc:description and description. A stylesheet can assign different styles to dc:title and title. Software sorting the catalog by date can consider the date elements and ignore the dc:date elements.

In this example, unprefixed elements such as catalog, painting, description, artist and title belong to no particular namespace. Unprefixed attributes, such as the attribute about of rdf:Description in the preceding example, likewise never belong to a namespace. Being an attribute of an element in the namespace "http://www.w3.org/TR/REC-rdf-syntax#" does not make it an attribute in the namespace "http://www.w3.org/TR/REC-rdf-syntax#". An attribute belongs to a namespace only if it has a declared prefix, as in rdf:about.

In XML 1.1, there is an exception to the rule that unprefixed attributes never belong to a namespace. XML 1.1/Namespaces 1.1 specifies that the attribute xmlns belongs to the namespace "http://www.w3.org/2000/xmlns/". In XML 1.0/Namespaces 1.0, the attribute xmlns belongs to no namespace.

A prefix may be redefined within a document to refer to one namespace URI in one element and another URI in another. The nearest ancestor defining the prefix then takes precedence. Redefining prefixes is usually a bad idea, however, since it causes confusion; you should avoid it.

Note: In XML 1.1 you can also undeclare a namespace by defining it with an empty value ("").

Namespace URIs

Many XML applications have conventional prefixes. SVG elements, for example, often use svg, while RDF elements often use rdf. These are merely conventions and may change according to need or preference. Before use, a prefix must be bound to a URI such as "http://www.w3.org/2000/svg" or "http://www.w3.org/1999/02/22-rdf-syntax-ns#". URIs, rather than prefixes, are standardized. The prefix may change as long as the URI stays the same. An RDF processor looks for the RDF URI, not a particular prefix. Provided nobody outside the w3.org domain uses namespace URIs in the w3.org domain, and the W3C oversees its members’ namespace choices, all conflicts can be avoided.

Namespace URIs need not point to a real document or page. They need not even use the http scheme. They could use another protocol, such as mailto, whose URIs do not even refer to documents. If you define a namespace yourself using an http URI, however, placing documentation for the XML application at that URI is a good idea. Tired of reports about incorrect or missing namespace links in its specifications, the W3C added simple pages at its namespace URIs. For a more formal approach, you can place a RDDL URI there, allowing automatic resolution and other techniques. This possibility is discussed further under Resource Directory Description Language (RDDL). None of this is required, however. Many namespace URIs yield the familiar 404 “Page not found” error when opened in a browser. Namespace URIs are purely formal identifiers. They are not page addresses and are not intended to be treated or used as links.

Parsers compare namespace URIs character by character. Any difference, even at one normally insignificant position, makes them different. For instance, the following URLs all point to the same page:

http://www.w3.org/1999/02/22-rdf-syntax-ns#
http://WWW.W3.ORG/1999/02/22-rdf-syntax-ns#
http://www.w3.org/1999/02/22-rdf-syntax-ns/
http://www.w3.org/1999/02/22-rdf-syntax-ns/index.rdf

Only the first, however, is the correct namespace name for RDF. These four URLs identify four distinct namespaces.

Specifying a default namespace with xmlns

Often you know that all content of an element comes from one XML application. Inside an SVG svg element, for example, you will probably find only other SVG elements. To indicate that an unprefixed element and all its unprefixed children belong to a namespace, add the unprefixed attribute xmlns to the topmost element, for example:

<svg xmlns="http://www.w3.org/2000/svg" width="12cm" height="10cm">
   <ellipse rx="110" ry="130" />
   <rect x="4cm" y="1cm" width="3cm" height="6cm" />
</svg>

Although none of the elements has a prefix, svg, ellipse and rect belong to the namespace "http://www.w3.org/2000/svg".

Attributes are a different matter. Default namespaces apply only to elements, not attributes. In this example, therefore, the attributes width, height, rx, ry, x and y belong to no namespace.

You can change the default namespace inside an element by adding the attribute xmlns. The following XML document initially sets the default namespace for all XHTML elements to "http://www.w3.org/1999/xhtml". This declaration applies throughout most of the document. However, the element svg has an attribute xmlns resetting the default namespace for itself and its content to "http://www.w3.org/2000/svg". The XLink information is held in attributes, however, so these must use explicit prefixes to enter the XLink namespace.

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink">
   <head><title>Drei Namensräume</title></head>
   <body>
      <h1 align="center">Eine Ellipse und ein Rechteck</h1>
      <svg xmlns="http://www.w3.org/2000/svg" width="12cm" height="10cm">
         <ellipse rx="110" ry="130" />
         <rect x="4cm" y="1cm" width="3cm" height="6cm" />
      </svg>
      <p xlink:type="simple" xlink:href="ellipses.html">Mehr über Ellipsen</p>
      <p xlink:type="simple" xlink:href="rectangles.html">Mehr über Rechtecke</p>
      <hr/>
      <p>Letzte Änderung am 13. Mai 2000</p>
   </body>
</html>

The default namespace does not apply to prefixed elements or attributes: they remain in the namespace bound to their prefix. An unprefixed child of a prefixed element, however, belongs to the default namespace.

  

 

 

 

back to top