Namespace declarations are scoped using an element container. All namespace prefixes are valid only within the element in which they are defined. Unqualified elements (those without an explicit namespace on the element) use the default namespace.
The following example shows unqualified elements, <book id="bk101">
and <book id="bk109">
within the "http://www.example.microsoft.com/catalog/"
namespace.
<catalog xmlns="http://www.example.microsoft.com/catalog/"> <book id="bk101"> ... </book> <book id="bk109"> ... </book> </catalog>
Note, however, that unqualified attributes do not belong to the default namespace. Therefore, the id
attribute in the previous example does not belong to any namespace. In order for the id
attribute to belong to the same namespace as the book element, prefix it like this:
<catalog xmlns="http://www.example.com/catalog/" xmlns:cat="http://www.example.com/catalog/"> <book cat:id="bk101"> ... </book> <book cat:id="bk109"> ... </book> </catalog>
In the following example, all unqualified elements within the <table>
element are scoped to the default namespace "http://www.w3.org/TR/REC-html40"
. Within the <details>
tag, the default namespace is set to "urn:example.microsoft.com:beerinfo"
. All unqualified tags within the <details>
element are scoped to this namespace.
<Beers> <!-- the default namespace is now that of HTML --> <table xmlns="http://www.w3.org/TR/REC-html40"> <tr><td>Name</td><td>Origin</td><td>Description</td></tr> <tr> <!-- drop the HTML namespace inside table cells --> <td><brandName xmlns="urn:example.microsoft.com:beerinfo">Coho</brandName></td> <td><origin xmlns="urn:example.microsoft.com:beerinfo">Bath, UK</origin></td> <td> <details xmlns="urn:example.microsoft.com:beerinfo"> <class>Bitter</class> <hop>Fuggles</hop> <pro>Wonderful hop, light alcohol, good summer beer</pro> <con>Fragile; excessive variance pub to pub</con> </details> </td> </tr> </table> </Beers>
The following table shows the elements and attributes of the preceding XML example and their properties as presented by Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office.
Node type | nodeName | namespaceURI | prefix | baseName |
---|---|---|---|---|
Element | Beers | n/a | n/a | Beers |
Element | table | http://www.w3.org/TR/REC-html40 | n/a | table |
Attribute | xmlns | n/a | n/a | xmlns |
Element | tr | http://www.w3.org/TR/REC-html40 | n/a | tr |
Element | td | http://www.w3.org/TR/REC-html40 | n/a | td |
Element | td | http://www.w3.org/TR/REC-html40 | n/a | td |
Element | td | http://www.w3.org/TR/REC-html40 | n/a | td |
Element | tr | http://www.w3.org/TR/REC-html40 | n/a | tr |
Element | td | http://www.w3.org/TR/REC-html40 | n/a | td |
Element | brandName | urn:example.microsoft.com:beerinfo | n/a | brandName |
Attribute | xmlns | n/a | n/a | xmlns |
Element | td | http://www.w3.org/TR/REC-html40 | n/a | td |
Element | origin | urn:example.microsoft.com:beerinfo | n/a | origin |
Attribute | xmlns | n/a | n/a | xmlns |
Element | td | http://www.w3.org/TR/REC-html40 | n/a | td |
Element | details | urn:example.microsoft.com:beerinfo | n/a | details |
Attribute | xmlns | n/a | n/a | xmlns |
Element | class | urn:example.microsoft.com:beerinfo | n/a | class |
Element | hop | urn:example.microsoft.com:beerinfo | n/a | hop |
Element | pro | urn:example.microsoft.com:beerinfo | n/a | pro |
Element | con | urn:example.microsoft.com:beerinfo | n/a | con |