Namespace declarations are scoped using an element container. In the default declaration example, all unqualified elements (without explicit namespace on the element) within the "BOOK" element belong to the "urn:BookLovers.org:BookInfo" namespace. In the following example, notice that all unqualified elements within the "table" element are scoped to the default namespace "http://www.w3.org/TR/REC-html40". On the "details" tag, the default namespace is set to "urn:beerstuff.org:brandinfo". 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:beerstuff.org:brandinfo">Huntsman</brandName></td> <td><origin xmlns="urn:beerstuff.org:brandinfo">Bath, UK</origin></td> <td> <details xmlns="urn:beerstuff.org:brandinfo"> <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 above XML example and their properties within the XML object model.
Node type | nodeName | namespaceURI | prefix | baseName |
---|---|---|---|---|
ELEMENT | Beers | Beers | ||
ELEMENT | table | http://www.w3.org/TR/REC-html40 | table | |
ATTRIBUTE | xmlns | xmlns | ||
ELEMENT | tr | http://www.w3.org/TR/REC-html40 | tr | |
ELEMENT | td | http://www.w3.org/TR/REC-html40 | td | |
ELEMENT | td | http://www.w3.org/TR/REC-html40 | td | |
ELEMENT | td | http://www.w3.org/TR/REC-html40 | td | |
ELEMENT | tr | http://www.w3.org/TR/REC-html40 | tr | |
ELEMENT | td | http://www.w3.org/TR/REC-html40 | td | |
ELEMENT | brandName | urn:beerstuff.org:brandinfo | brandName | |
ATTRIBUTE | xmlns | xmlns | ||
ELEMENT | td | http://www.w3.org/TR/REC-html40 | td | |
ELEMENT | origin | urn:beerstuff.org:brandinfo | origin | |
ATTRIBUTE | xmlns | xmlns | ||
ELEMENT | td | http://www.w3.org/TR/REC-html40 | td | |
ELEMENT | details | urn:beerstuff.org:brandinfo | details | |
ATTRIBUTE | xmlns | xmlns | ||
ELEMENT | class | urn:beerstuff.org:brandinfo | class | |
ELEMENT | hop | urn:beerstuff.org:brandinfo | hop | |
ELEMENT | pro | urn:beerstuff.org:brandinfo | pro | |
ELEMENT | con | urn:beerstuff.org:brandinfo | con |
The namespace shorthands are valid within the element in which they are defined. In the explicit declaration example, the shorthands "bk" and "money" are valid only within the "bk:BOOK" element.