Namespace declarations are scoped using an element container. In the default declaration example, all unqualified elements (without an 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". Within 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.