XML technologies / XML / XML in a Nutshell / XPointer / Namespaces

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

Since XPointers may occur outside XML documents, including HTML documents, database fields or magazine pages, they need their own mechanism for binding namespace prefixes to namespace URIs. This is done by placing one or more xmlns sections before the xpointer part. The syntax is xmlns(praefix=URI). For example, this XPointer maps svg as a prefix to the namespace "http://www.w3.org/2000/svg" and then selects all rect elements in that namespace:

xmlns(svg=http://www.w3.org/2000/svg) xpointer(//svg:rect)

As with most namespace usage, the URI matters in an XPointer, not the prefix. The preceding XPointer finds all rect elements in the namespace "http://www.w3.org/2000/svg", regardless of their prefix or whether they belong to the default namespace.

A prefixless default namespace cannot be defined for an XPointer. However, prefixed names in an XPointer can refer to unprefixed, namespace-qualified elements in the target document. For example, this XPointer finds the third div element in an XHTML document:

xmlns(html=http://www.w3.org/1999/xhtml) xpointer(//html:div[3])

It uses the prefix html, to identify the XHTML namespace, although XHTML documents themselves never use prefixes.

Multiple namespace prefixes can be used by adding more xmlns declarations. For example, this XPointer selects all svg elements in XHTML documents by declaring a prefix for the SVG and XHTML namespaces.

xmlns(svg=http://www.w3.org/2000/svg)
 xmlns(h=http://www.w3.org/1999/xhtml) xpointer(/h:html//svg:svg)

When an XPointer is embedded in an XML document, that document’s namespace bindings do not apply to it. Only bindings established by xmlns declarations apply. If XPointers contain XPath expressions referring to namespaced elements or attributes, they must be preceded by the appropriate xmlns snippets declaring those namespaces.

  

 

 

 

back to top