XML Namespaces
XML Namespaces
Section titled “XML Namespaces”XML Namespaces provide a mechanism to avoid naming conflicts between elements defined in different XML vocabularies.
Imagine combining XML from different sources – namespaces prevent ambiguity when elements have the same name but different meanings.
The Problem: Name Conflicts
Section titled “The Problem: Name Conflicts”In XML, you define your own element names. This freedom can lead to conflicts when mixing XML documents from different applications or standards.
Consider this XML representing an HTML table:
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr></table>Now, consider this XML representing a piece of furniture:
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length></table>If you combined these fragments, how would a program know which <table> element it’s dealing with? They share the same name but represent entirely different concepts.
The Solution: Using Prefixes
Section titled “The Solution: Using Prefixes”Name conflicts can be resolved by using prefixes mapped to unique namespaces.
Here’s the combined example using prefixes h (for HTML) and f (for furniture):
<root> <h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table>
<f:table xmlns:f="https://example.com/furniture-schema"> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table></root>Now, h:table and f:table are distinct, eliminating the conflict.
XML Namespaces - The xmlns Attribute
Section titled “XML Namespaces - The xmlns Attribute”When using prefixes, you must declare a namespace for each prefix using the xmlns attribute.
The syntax is xmlns:prefix="URI".
In the example above:
xmlns:h="http://www.w3.org/TR/html4/"declares the namespace for thehprefix.xmlns:f="https://example.com/furniture-schema"declares the namespace for thefprefix.
The URI (Uniform Resource Identifier) acts as a unique identifier for the namespace. It doesn’t necessarily point to a retrievable resource, although it often points to documentation.
Namespace declarations can apply to the element they are defined on and its children. You can also declare namespaces on the root element for broader scope:
<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="https://example.com/furniture-schema"> <h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table>
<f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table></root>Key Point: The URI’s purpose is uniqueness, not necessarily location. Parsers use the URI string itself to distinguish namespaces.
Uniform Resource Identifier (URI)
Section titled “Uniform Resource Identifier (URI)”A Uniform Resource Identifier (URI) is a string of characters used to identify a name or a resource. URLs (Uniform Resource Locators) are the most common type of URI, specifying location (like a web address). URNs (Uniform Resource Names) are another type, focused on unique naming.
In XML namespaces, the URI serves primarily as a unique name.
Default Namespaces
Section titled “Default Namespaces”You can declare a default namespace for an element and its descendants using xmlns="URI" (without a prefix). Elements without a prefix within its scope belong to this default namespace.
Example for the HTML table:
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr></table>Example for the furniture table:
<table xmlns="https://example.com/furniture-schema"> <name>African Coffee Table</name> <width>80</width> <length>120</length></table>Note: Attributes without prefixes do not belong to the default namespace. They need an explicit prefix if they belong to a namespace.
Namespaces in Practice: XSLT Example
Section titled “Namespaces in Practice: XSLT Example”XSLT (Extensible Stylesheet Language Transformations) is an XML-based language used to transform XML documents. XSLT itself uses namespaces extensively.
Consider this simple XSLT stylesheet transforming a CD catalog into HTML:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
</xsl:stylesheet>Here, elements like <xsl:stylesheet>, <xsl:template>, <xsl:for-each>, and <xsl:value-of> belong to the XSLT namespace, identified by the xsl prefix and the URI http://www.w3.org/1999/XSL/Transform. Elements without a prefix (like <html>, <h2>, <table>, etc.) are literal result elements, intended to be part of the HTML output.
Namespaces are essential in many XML-based technologies like SVG, SOAP, and XML Schema.