SCDJWS Study Guide: XML Namespace


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Namespace Scope

The scope of an XML namespace declaration declaring a prefix extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner declarations with the same xmlns:<prefix> or xmlns (i.e., default namespace) declaration (in such case, the namespace name identifier has been changed).

In other words, the namespace declaration is considered to apply to the element where it is specified and to all elements within the content of that element, unless overridden by another namespace declaration with the same NSAttName part. For example:

<?xml version="1.0"?>
<!-- all elements here are explicitly in the school namespace -->
<school:school xmlns:school='http://www.theschool.com/'>
<school:department>Computer Science</school:department>
<school:building>Bill Gates Hall</school:building>
<school:lab>210</school:lab>
<school:classroom>212, 214, 216</school:classroom>
</school:school>

Multiple namespace prefixes can be declared as attributes of a single element, as shown in this example:

<?xml version="1.0"?>
<!-- both namespace prefixes are available throughout -->
<bk:book xmlns:bk='urn:loc.gov:books'
       xmlns:isbn='urn:ISBN:0-395-36341-6'>
<bk:title>Cheaper by the Dozen</bk:title>
<isbn:number>1568491379</isbn:number>
</bk:book>

Re-declaring Namespaces

Namespace prefixes can be overridden by redeclaring the prefix at a nested scope, but it is not possible to undeclare a namespace prefix.

Correct:

<d:student xmlns:d='http://www.develop.com/student'>
<d:id>3235329</d:id>
<d:name xmlns:d='urn:names-r-us'>John Smith</d:name>
<d:language>C#</d:language>
<d:score>A</d:score>
</d:student>

Incorrect:

<d:student xmlns:d='http://www.develop.com/student'>
<d:id xmlns:d="">3235329</d:id>
......
</d:student>


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |