XML technologies / XML / Perl & XML / Introduction to XML / Namespaces

(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)

Sometimes it is useful to group elements and attributes using Namespaces (Namespaces). A namespace is rather like a person’s surname: you may know three boys called Mike with different surnames. The following document illustrates the concept:

Example: A document with namespaces

<?xml version="1.0" encoding="iso-8859-1"?>
<report>
  <title>Fisch und Fahrräder: Gibt es einen Zusammenhang?</title>
  <para>Ich habe eine überraschende Beziehung zwischen Fisch und Fahrrädern
    gefunden, die man grob durch die Gleichung <equation>f = kb+n</equation>
    ausdrücken kann. Die Ergebnisse meines Experiments werden im folgenden
    Graphen dargestellt:</para>
  <chart xmlns:graph="http://mathstuff.com/dtds/chartml/">
    <graph:dimension>
      <graph:axis>fish</graph:axis>
      <graph:start>80</graph:start>
      <graph:end>99</graph:end>
      <graph:interval>1</graph:interval>
    </graph:dimension>
    <graph:dimension>
      <graph:axis>bicycle</graph:axis>
      <graph:start>0</graph:start>
      <graph:end>1000</graph:end>
      <graph:interval>50</graph:interval>
    </graph:dimension>
    <graph:equation>fish=0.01*bicycle+81.4</graph:equation>
    </graph:chart>
</report>

The example uses two namespaces. The first is the default namespace, with no colons in element and attribute names. Elements whose names begin with graph: belong to the “chartml” namespace we just invented. graph: is the prefix of a namespace. An element or attribute name with this prefix is a qualified name. The example contains two <equation> elements with completely different roles. The one in the default namespace reproduces an equation verbatim. The other, in the namespace"http://mathstuff.com/dtds/charml/" is used by the graphics program to generate a curve.

A namespace must be declared on an enclosing element, using an attribute of the form xmlns:präfix=URI. Here, präfix is the prefix used in the document, in this example graph:. The URI is a unique identifier, usually a URL, though that is not required. URLs make uniqueness easy to ensure. Outside the declaring element, the namespace does not apply unless declared again. That declaration then establishes its namespace and possibly different prefix within the second element.

Namespaces distinguish similar element and attribute types. They also play an important processing role: a namespace change may signal replacing the default formatter with one suited to the specific data, such as the graph renderer in our example. Alternatively, a namespace can identify instructions serving as meta-markup, as in XSLT.

Namespaces are increasingly important XML tools. Unfortunately, they present a problem with DTDs, discussed later. For now, a DTD contains declarations constraining and structuring valid elements and attributes, but no namespace declaration or assignment of elements and attributes to namespaces, apart from the default. This may seem puzzling, but DTDs date back to the early days of SGML, while namespaces are very new. They are therefore incompatible with DTDs. Despite efforts by standards bodies, this problem remains unsolved.

Programmer strategies covers practical issues when working with namespaces.

  

  

 

 

 

back to top