Declaring XML Namespaces

You declare a namespace with an attribute, xmlns:prefix="namespace-URI", and then write prefix:name on that element or any descendant. The tour file declares bn for BookNest's names, xlink for XLink (XLink Simple Links) and xi for XInclude (XInclude and Document Assembly). The URI is only an identifier, never fetched and compared character by character. The prefix is a local abbreviation, so bn:book and x:book are the same element if both prefixes map to the same URI. XPath 3.0 and later can skip prefixes with a braced URI literal, Q{uri}local:

An element's prefixed name, local name and namespace URI
xquery -s:booknest-tour.xml -qs:'//Q{https://booknest.example.com/ns/catalog}book !
  (name(), local-name(), namespace-uri())'
Output
"bn:book"
"book"
"https://booknest.example.com/ns/catalog"

Two prefixes are reserved: xml is bound to http://www.w3.org/XML/1998/namespace 189 without any declaration, and xmlns may never be declared. Attribute uniqueness works on expanded names, so <book xmlns:a="urn:x" xmlns:b="urn:x" a:id="1" b:id="2"/> is an error (xmllint 3,427 : "Namespaced Attribute id in 'urn:x' redefined"), as is rebinding xml ("xml namespace prefix mapped to wrong URI"). Choose a namespace URI once and keep it: to every tool, a new URI means a new vocabulary.


Namespace

A namespace is used to prevent name clashes of elements and attributes. This can be important when different people are working together, each creating their own XML documents. By convention, an HTTP-style URL is used as a namespace, although no connection to the URL is needed to process the XML document.

A namespace is declared in an opening tag like this:
xmlns:prefixName="namespaceName"

Syntax for declaring a namespace in an opening tag.

A namespace can only be used after it has been declared in an ancestor or current element. To use a namespace, prepend an element name or attribute name with 'prefixName:'.

If the part ':prefixName' is omitted, a default namespace is assumed. The scope of the namespace is the content of the tag itself. To use the default namespace, the descendant element names and attribute names need not be prefixed with any prefix name.

Reserved Prefix Names

The prefix name 'xml' is reserved and bound to 'http://www.w3.org/XML/1998/namespace'. It may be declared, but must not be bound to any other namespace name. Other prefix names must not be bound to this namespace name, and it must not be declared as the default namespace.
The prefix name 'xmlns' is reserved and bound to 'http://www.w3.org/2000/xmlns/'. It must not be declared. Other prefix names must not be bound to this namespace name. It must not be declared as the default namespace. Element names must not have the prefix xmlns.
Prefix names beginning with 'xml' are reserved.


No tag may contain two attributes that have the same names, or the same local names prefixed by prefix names bound to the same namespace name.

With XPath 3.0, XML namespaces may be referenced using braced URI literals. For example, the qualified name math:pi may be expanded to Q{http://www.w3.org/2005/xpath-functions/math}pi, embedding the namespace URI inside the prefix. See XPath.
ch02-namespace-example.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<bookslist xmlns:gb="http://GoldenBookstore.com"
           xmlns:xlink="http://www.w3.org/1999/xlink"
           xmlns:xi="http://www.w3.org/2003/XInclude">
   <gb:book>
      <author gb:gender="f"
              xlink:type="simple"
              xlink:href="/images/jkrowling.gif"
              xlink:show="new">J. K. Rowling</author>
   </gb:book>
</bookslist>