Namespace (XML)

from Wikipedia, the free encyclopedia

XML namespaces are used to uniquely identify elements and attributes in an XML document and to be able to mix several XML languages ​​in a single document. How they work can be compared to area codes for telephone numbers.

For example, the <p> element in XHTML describes a paragraph; in an XML language for a person database, <p> could represent an element for a person. Namespaces allow these elements to be clearly distinguished.

The namespace mechanism for XML data was developed by the W3 consortium and has been available in its third edition since December 8, 2009 for both XML 1.0 and XML 1.1. The first version of the specification is dated January 14, 1999, about a year after the XML specification was adopted. For this reason one finds z. E.g. in the recommendation for MathML 1.0 no namespace is specified. The second version was released on August 16, 2006.

Structure of XML namespaces

Namespaces are represented by URIs , mostly normal web addresses. It should be noted that the corresponding address does not have to exist . It can be freely defined. It is also important that the namespace information is case-sensitive, including in the host part, as well as any URL coding , e.g. B.% C3% A4 instead of ä, must be observed.

If a URL is used as a namespace, however, it usually makes sense to offer additional information about the XML language at this address, e.g. B. a document type definition (DTD) or an XML schema .

Use namespaces

For namespaces the attribute xmlns(for English. : XML N ame s pace) is used:

<html xmlns="http://www.w3.org/1999/xhtml">
  ... restliche HTML-Datei
</html>

The <html> element and all its child elements, i.e. elements within <html>... </html>, belong to the namespace here W3C XHTML namespace. Namespaces can also be nested:

<html xmlns="http://www.w3.org/1999/xhtml">
  ... XHTML-Elemente
  <math xmlns="http://www.w3.org/1998/Math/MathML">
    ... MathML-Elemente
  </math>
  ... XHTML-Elemente
</html>

Prefixes

In addition to the method described above, there is a prefix mechanism: elements can be set in any namespace using a character string separated from the element name by a colon. The prefix does not have to have any relation to the namespace, but must first be "bound" to the namespace:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:m="http://www.w3.org/1998/Math/MathML">
  ... XHTML-Elemente
  <m:math>
    ... MathML-Elemente mit m:-Präfix
  </m:math>
  ... XHTML-Elemente
</html>

In this example, the prefix m is bound to the MathML namespace in the <html> element with the specification xmlns: m = "..." . After that, elements can be specified by specifying

<m:Elementname>...</m:Elementname>

be set in the appropriate namespace.

The actual element name, i.e. the part without a leading prefix and colon, is called the local name of the element. The full or qualified name of the element ( QName for short ) consists of a namespace URI and local name, so the prefix can be chosen as desired.

The prefix xmlns

From a technical point of view, a construct of the type is xmlns:man attribute with a namespace prefix. In the case of using prefixes, there is an additional namespace

 xmlns:xmlns="http://www.w3.org/2000/xmlns/"

in the document. However, this is not specified explicitly, but is assumed by XML-processing programs. Like the following xml , this prefix is ​​also permanently linked to the namespace specified above.

The prefix xml

XML elements must not xmlbegin with the character string . It follows from this that XML namespace prefixes must not xmlbegin with either . It is actually xmlbound to a namespace for elements and attributes that the W3 consortium reserves for XML extensions.

The URI of this namespace is http://www.w3.org/XML/1998/namespace. It does not have to be specified explicitly, but is inserted by the processing programs themselves.

The following attributes with prefix currently exist xml(as of April 2010):

attribute meaning example
xml: long Language information about the content. Defined by ISO 3166 and ISO 639 . Values: 2-digit language code optionally followed by a hyphen and a capitalized 2-digit country code. Exception: non-standardized country and language codes.
<p xml:lang="de">
  Inhalt in deutscher Sprache, Land: undefiniert
</p>
<p xml:lang="en-US">
  Inhalt in englischer Sprache, Land: Vereinigte Staaten
</p>
<p xml:lang="sindarin-MITTELERDE">
  Inhalt in grauelbisch, Land: Mittelerde
</p>
xml: space May accept the two values preserve and default . preserve means that spaces and line breaks are displayed exactly as they are in the source text of the file (as with the <pre> element in HTML). default means that several spaces and line breaks are combined into a single space (default).
<div xml:space="default">
  ASCII-Art:
  <p xml:space="preserve">
   ¦\      _,,,---,,_
   /,`.-'`'    -.  ;-;;,_
  ¦,4-  ) )-,_..;\ (  `'-'
   '---(_/--'  `-'\_)  fL
  </p>
</div>
xml: base Base URL from which all relative links in the element are evaluated.
<h3>Wikipedia-Links</h3>
<ul xml:base="http://de.wikipedia.org/wiki/">
  <li><a href="XML">XML</a></li>
  <li><a href="SGML">SGML</a></li>
</ul>
xml: id Unique identifier for the element throughout the document
<p xml:id="Kapitel_1">Ein Absatz</p>

<p><a href="#Kapitel_1">Link zum obigen Absatz</a></p>

Attributes

Attributes in XML are so-called associated nodes . They are not treated like normal children of an element. In the case of namespaces, this means that attributes are not in the namespace of the element in which they are noted, but rather in the null namespace by default.

There are cases in which you want to explicitly change this. For example, links in SVG are taken from the XLink language , which provides a set of attributes with which links can be described. In this case, the individual attributes must be given a prefix:

<svg xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink">
  <a xlink:href="grafik2.svg">Link zu Grafik 2</a>
</svg>

There is no way of setting attributes in a specific namespace without a prefix.

example

In the following example, elements or attributes from the XML languages XHTML ( blue ), MathML ( red ), SVG ( green ) and without namespace (black) are mixed. In particular, it should be noted that the xmlns attribute itself is not in any namespace; its value is only colored to indicate which elements are affected by it.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Beispiel-Datei mit mehreren Namensräumen</title>
  </head>
  <body>
    <h1>Eine Mathe-Formel:</h1>
    <math xmlns="http://www.w3.org/1998/Math/MathML">
      <mi>x</mi><mo>=</mo><mn>2</mn>
    </math>
    <p>Und noch ein kleines Bild dazu:</p>
    <svg:svg>
      <svg:rect x="0" y="0" width="10" height="10" />
      <svg:text>
        <svg:tspan>Eine Formel in der Grafik:</svg:tspan>
        <svg:tspan>
          <math xmlns="http://www.w3.org/1998/Math/MathML">
            <mi>y</mi><mo>=</mo><mn>1</mn>
          </math>
        </svg:tspan>
      </svg:text>
    </svg:svg>
    <p>Eine SVG Grafik kann auch ohne Präfix verwendet werden:</p>
    <svg xmlns="http://www.w3.org/2000/svg">
      <circle svg:cx="10" svg:cy="10" svg:r="5" svg:fill="red" />
    </svg>
  </body>
</html>

Namespace notation according to James Clark

Namespace prefixes can be freely chosen. In some contexts, e.g. B. If you only see a small section of a more complex XML document, this arbitrariness can lead to misunderstandings. James Clark therefore introduced a different notation for element names in his article XML Namespaces , which is enjoying increasing popularity in the documentation of XML data.

A simple example could look like this:

 <{http://www.w3.org/1999/xhtml}html>
   <{http://www.w3.org/2000/svg}svg>
   </{http://www.w3.org/2000/svg}svg>
 </{http://www.w3.org/1999/xhtml}html>

Elements in the running text are then e.g. B. as {http://www.w3.org/1999/xhtml}htmlnoted.

The URI of a namespace is placed in front of each element in curly brackets. This uniquely characterizes the element with its namespace. This is the great advantage of this method, which at first glance seems a bit cumbersome.

Please note that this notation is not a valid XML. It is only used to illustrate and document namespaces.

Difficulties with XML namespaces

Colons in element names

XML processing programs are divided into two groups:

  • Programs that observe and process namespaces
  • Programs that ignore namespaces

The most important difference is the way colons are used in element names. If the document is processed by a namespace-sensitive program, all colons must be interpreted as separators for prefixes and element names - otherwise the program generates an error. Programs that do not distinguish between namespaces do not carry out this check.

In XML itself, it is allowed to define elements that already have a colon in the element name. However, this is expressly not recommended. However, files that use namespaces are thus well-formed XML documents.

URLs as namespaces

It is a regular question in the relevant forums what should be stored under the URL that is used as the namespace definition. The simple answer is that nothing need exist there. URLs, and more generally URIs, are used as a definition because they can provide certain clues as to by whom the "language" was developed and because the respective persons or organizations have some at least symbolic control over a certain range of URLs (e.g. http://www.w3.org/… for the W3 consortium).

Document type definitions and namespaces

XML languages ​​are still often defined with so-called document type definitions. These DTDs, which date back to the SGML era, were never intended to respect namespaces (DTDs were developed in the 1980s). It is very cumbersome and sometimes impossible, e.g. B. with any prefixes or in XSLT to set up a generally valid DTD for an XML language or a document.

An attempt to do this for XHTML, MathML and SVG in one file can be found at the W3 consortium. There, by means of entities that can be defined afterwards, it is possible to redefine prefixes for each document.

See also

Individual evidence

  1. W3C XHTML namespace (English)
  2. W3C XML namespace (English)
  3. XML Namespaces according to James Clark (English)
  4. To XHTML + MathML + SVG Profile (English)

Web links