Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

1.2 Elements, namespaces, and namespace declarations

<prefix:localname xmlns:prefix='namespace URI'/>
<prefix:localname xmlns:prefix='namespace URI'></prefix:localname/>
<prefix:localname xmlns:prefix='namespace URI'>children</prefix:localname/>

Because XML allows designers to chose their own tagnames, it is possible that two or more designers may choose the same tagnames for some or all of their elements. XML namespaces provide a way to distinguish deterministically between XML elements that have the same local name but are, in fact, from different vocabularies. This is done by associating an element with a namespace. A namespace acts as a scope for all elements associated with it. Namespaces themselves also have names. A namespace name is a uniform resource identifier (URI). Such a URI serves as a unique string and need not be able to be dereferenced. The namespace name and the local name of the element together form a globally unique name known as a qualified name.

Namespace declarations appear inside an element start tag and are used to map a namespace name to another, typically shorter, string known as a namespace prefix. The syntax for a namespace declaration is xmlns:prefix='URI'. It is also possible to map a namespace name to no prefix using a default namespace declaration. The syntax for a default namespace declaration is xmlns='URI'. In both cases, the URI may appear in single quotes (') or double quotes ("). Only one default namespace declaration may appear on an element. Any number of nondefault namespace declarations may appear on an element, provided they all have different prefix parts. It is legal, although not particularly useful, to map the same URI to more than one prefix.

All namespace declarations have a scope—that is, a set of elements to which they may apply. A namespace declaration is in scope for the element on which it is declared and all of that element's descendants. The in-scope mapping of a given prefix to a namespace name can be overridden by providing a new mapping for that prefix on a descendant element. The in-scope default namespace can be overridden by providing a new default namespace declaration on a descendant element.

The names of all elements in a document that conforms to the Namespaces in the XML specification are QNames. Syntactically, all QNames have a local name and an optional prefix. Both the local name and the prefix are NCNames. An NCName is a name without a colon in it. The syntax for an element with a prefix is the prefix, followed by a colon, followed by the local name. The namespace of an element with a given prefix is the namespace specified by the in-scope namespace declaration for that prefix. It is an error if no such namespace declaration is in scope. The namespace of unprefixed elements is the namespace specified by the in-scope default namespace declaration, if any. If no default namespace declaration is in scope, then such elements are not in any namespace. Elements not in any namespace are known as unqualified elements. The namespace name of unqualified elements is the empty string "". If a default namespace declaration is in scope and an unqualified element is required, the default namespace declaration can be masked by providing a namespace declaration of the form xmlns='' on the element.

Examples

Qualified and unqualified elements

<pre:Person xmlns:pre='urn:example-org:People' > <name>Martin</name>
 <age>33</age>
</pre:Person>

An element with a local name of Person and a prefix of pre that is mapped to the namespace name urn:example-org:People. The element has children with local names of name and age. Both of these child elements are unqualified; that is, they are not in any namespace.

Qualified and unqualified elements using a default namespace declaration

<Person xmlns='urn:example-org:People' >
 <name xmlns=''>Martin</name>
 <age xmlns=''>33</age>
</Person>

An element with a local name of Person and no prefix. The element is in the namespace urn:example-org:People by virtue of an in-scope default namespace declaration for that URI. The element has children with local names of name and age. Both of these child elements are unqualified; that is, they are not in any namespace. This example is equivalent to the previous example.

Qualified elements

<pre:Person xmlns:pre='urn:example-org:People' >
 <pre:name>Martin</pre:name>
 <pre:age>33</pre:age>
</pre:Person>

An element with a local name of Person and a prefix of pre that is mapped to the namespace URI urn:example-org:People. The element has children with local names of name and age. Both of these child elements also have a prefix of pre and are in the urn:example-org:People namespace.

Qualified elements using a default namespace declaration

<Person xmlns='urn:example-org:People' >
 <name>Martin</name>
 <age>33</age>
</Person>

An element with a local name of Person and no prefix. The element is in the namespace urn:example-org:People by virtue of an in-scope default namespace declaration for that URI. The element has children with local names of name and age. Both of these child elements are also in the urn:_example-org:People namespace. This example is equivalent to the previous example.

  • + Share This
  • 🔖 Save To Your Account