Set - 4

Question 6 :

What happens when an XML namespace declaration goes out of scope?

Answer :

When an XML namespace declaration goes out of scope, it simply no longer applies. For example, in the following, the declaration of the http://www.google.org/ namespace does not apply to the C element because this is outside its scope. That is, it is past the end of the B element, on which the http://www.google.org/ namespace was declared.

<!-- B is in the http://www.google.org/ namespace;
C is not in any XML namespace. -->
<A>
<B xmlns="http://www.google.org/">abcd</B>
<C>efgh</C>
</A>

In addition to the declaration no longer applying, any declarations that it overrode come back into scope. For example, in the following, the declaration of the http://www.google.org/ namespace is brought back into scope after the end of the B element. This is because it was overridden on the B element by the declaration of the http://www.bar.org/ namespace.

<!-- A and C are in the http://www.google.org/ namespace.
B is in the http://www.bar.org/ namespace. -->
<A xmlns="http://www.google.org/">
<B xmlns="http://www.bar.org/">abcd</B>
<C>efgh</C>
</A>


Question 7 :

What happens if no XML namespace declaration is in scope?

Answer :

If no XML namespace declaration is in scope, then any prefixed element type or attribute names result in namespace errors. For example, in the following, the names google:A and google:B result in namespace errors. 

<?xml version="1.0" ?>
<google:A google:B="error" />

In the absence of an XML namespace declaration, unprefixed element type and attribute names do not belong to any XML namespace. For example, in the following, the names A and B are not in any XML namespace.


Question 8 :

Can multiple XML namespace declarations be in scope at the same time?

Answer :

Yes, as long as they don't use the same prefixes and at most one of them is the default XML namespace. For example, in the following, the http://www.google.org/ and http://www.bar.org/ namespaces are both in scope for all elements:

<A xmlns:google="http://www.google.org/"
xmlns:bar="http://www.bar.org/">
<google:B>abcd</google:B>
<bar:C>efgh</bar:C>
</A>

One consequence of this is that you can place all XML namespace declarations on the root element and they will be in scope for all elements. This is the simplest way to use XML namespaces.


Question 9 :

How can I declare XML namespaces so that all elements and attributes are in their scope?

Answer :

XML namespace declarations that are made on the root element are in scope for all elements and attributes in the document. This means that an easy way to declare XML namespaces is to declare them only on the root element.


Question 10 :

Does the scope of an XML namespace declaration ever include the DTD?

Answer :

No.
XML namespaces can be declared only on elements and their scope consists only of those elements and their descendants. Thus, the scope can never include the DTD.