XML Interviews Question page21
-
If an element or attribute is in the scope of an XML namespace declaration, is its name in that namespace?
Not necessarily. When an element or attribute is in the scope of an XML namespace declaration, the element or attribute's name is checked to see if it has a prefix that matches the prefix in the declaration. Whether the name is actually in the XML namespace depends on whether the prefix matches. For example, in the following, the element type names A, B, and D and the attribute names C and E are in the scope of the declaration of the http://www.google.org/ namespace. While the names A, B, and C are in that namespace, the names D and E are not.
<google:A xmlns:google="http://www.google.org/">
<google:B google:C="google" />
<bar:D bar:E="bar" />
</google:A>
-
What happens when an XML namespace declaration goes out of scope?
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>
-
What happens if no XML namespace declaration is in scope?
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.
-
Can multiple XML namespace declarations be in scope at the same time?
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.
-
How can I declare XML namespaces so that all elements and attributes are in their scope?
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.
-
Does the scope of an XML namespace declaration ever include the DTD?
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.
-
Can I use XML namespaces in DTDs?
Yes and no. In particular, DTDs can contain qualified names but XML namespace declarations do not apply to DTDs .
This has a number of consequences. Because XML namespace declarations do not apply to DTDs:
1. There is no way to determine what XML namespace a prefix in a DTD points to. Which means...
2. Qualified names in a DTD cannot be mapped to universal names. Which means...
3. Element type and attribute declarations in a DTD are expressed in terms of qualified names, not universal names. Which means...
4. Validation cannot be redefined in terms of universal names as might be expected.
This situation has caused numerous complaints but, as XML namespaces are already a recommendation, is unlikely to change. The long term solution to this problem is an XML schema language: all of the proposed XML schema languages provide a mechanism by which the local name in an element type or attribute declaration can be associated with an XML namespace. This makes it possible to redefine validity in terms of universal names.
-
Do XML namespace declarations apply to DTDs?
No. In particular, an xmlns attribute declared in the DTD with a default is not an XML namespace declaration for the DTD.. (Note that an earlier version of MSXML (the parser used by Internet Explorer) did use such declarations as XML namespace declarations, but that this was removed in MSXML 4.
-
Can I use qualified names in DTDs?
Yes.
For example, the following is legal:
<!ELEMENT google:A (google:B)>
<!ATTLIST google:A
google:C CDATA #IMPLIED>
<!ELEMENT google:B (#PCDATA)>
However, because XML namespace declarations do not apply to DTDs , qualified names in the DTD cannot be converted to universal names. As a result, qualified names in the DTD have no special meaning. For example, google:A is just google:A -- it is not A in the XML namespace to which the prefix google is mapped. The reason qualified names are allowed in the DTD is so that validation will continue to work. Can the content model in an element type declaration contain element types whose names come.