XML Interviews Question page16
-
Do I need to use XML namespaces?
Maybe, maybe not. If you don't have any naming conflicts in the XML documents you are using today, as is often the case with documents used inside a single organization, then you probably don't need to use XML namespaces. However, if you do have conflicts today, or if you expect conflicts in the future due to distributing your documents outside your organization or bringing outside documents into your organization, then you should probably use XML namespaces. Regardless of whether you use XML namespaces in your own documents, it is likely that you will use them in conjunction with some other XML technology, such as XSL, XHTML, or XML Schemas.
For example, the following XSLT (XSL Transformations) stylesheet uses XML namespaces to distinguish between element types defined in XSLT and those defined elsewhere:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Address">
<!-- The Addresses element type is not part of the XSLT namespace. -->
<Addresses>
<xsl:apply-templates/>
</Addresses>
</xsl:template>
</xsl:stylesheet>
-
What is the relationship between XML namespaces and the XML 1.0 recommendation?
Although the XML 1.0 recommendation anticipated the need for XML namespaces by noting that element type and attribute names should not include colons, it did not actually support XML namespaces. Thus, XML namespaces are layered on top of XML 1.0. In particular, any XML document that uses XML namespaces is a legal XML 1.0 document and can be interpreted as such in the absence of XML namespaces. For example, consider the following document:
<google:A xmlns:google="http://www.google.org/">
<google:B google:C="bar"/>
</google:A>
If this document is processed by a namespace-unaware processor, that processor will see two elements whose names are google: A and google:B. The google:A element has an attribute named xmlns:google and the google:B element has an attribute named google:C. On the other hand, a namespace-aware processor will see two elements with universal names {http://www.google.org}A and {http://www.google.org}B. The {http://www.google.org}A does not have any attributes; instead, it has a namespace declaration that maps the google prefix to the URI http://www.google.org. The {http://www.google.org}B element has an attribute named {http://www.google.org}C.
Needless to say, this has led to a certain amount of confusion. One area of confusion is the relationship between XML namespaces and validating XML documents against DTDs. This occurs because the XML namespaces recommendation did not describe how to use XML namespaces with DTDs. Fortunately, a similar situation does not occur with XML schema languages, as all of these support XML namespaces. The other main area of confusion is in recommendations and specifications such as DOM and SAX whose first version predates the XML namespaces recommendation. Although these have since been updated to include XML namespace support, the solutions have not always been pretty due to backwards compatibility requirements. All recommendations in the XML family now support XML namespaces.
-
What is the difference between versions 1.0 and 1.1 of the XML namespaces recommendation?
There are only two differences between XML namespaces 1.0 and XML namespaces 1.1:
* Version 1.1 adds a way to undeclare prefixes. For more information, see question 4.7.
* Version 1.1 uses IRIs (Internationalized Resource Identifiers) instead of URIs. Basically, URIs are restricted to a subset of ASCII characters, while IRIs allow much broader use of Unicode characters. For complete details, see section 9 of Namespaces in XML 1.1.
NOTE: As of this writing (February, 2003), Namespaces in XML 1.1 is still a candidate recommendation and not widely used. PART II: DECLARING AND USING XML NAMESPACES
-
How do I declare an XML namespace in an XML document?
To declare an XML namespace, you use an attribute whose name has the form:
xmlns:prefix
--OR--
xmlns
These attributes are often called xmlns attributes and their value is the name of the XML namespace being declared; this is a URI. The first form of the attribute (xmlns:prefix) declares a prefix to be associated with the XML namespace. The second form (xmlns) declares that the specified namespace is the default XML namespace.
For example, the following declares two XML namespaces, named http://www.google.com/ito/addresses and http://www.google.com/ito/servers. The first declaration associates the addr prefix with the http://www.google.com/ito/addresses namespace and the second declaration states that the http://www.google.com/ito/servers namespace is the default XML namespace.
<Department
xmlns:addr="http://www.google.com/ito/addresses"
xmlns="http://www.google.com/ito/servers">
NOTE: Technically, xmlns attributes are not attributes at all -- they are XML namespace declarations that just happen to look like attributes. Unfortunately, they are not treated consistently by the various XML recommendations, which means that you must be careful when writing an XML application. For example, in the XML Information Set (http://www.w3.org/TR/xml-infoset), xmlns "attributes" do not appear as attribute information items. Instead, they appear as namespace declaration information items. On the other hand, both DOM level 2 and SAX 2.0 treat namespace attributes somewhat ambiguously. In SAX 2.0, an application can instruct the parser to return xmlns "attributes" along with other attributes, or omit them from the list of attributes. Similarly, while DOM level 2 sets namespace information based on xmlns "attributes", it also forces applications to manually add namespace declarations using the same mechanism the application would use to set any other attributes