When the context node is an element, the namespace::
axis will locate all namespace nodes which apply to that element. The namespace::
axis can be used only with elements, and locates only namespace nodes.
A typical XSLT style sheet for use with Microsoft Internet Explorer declares two namespaces: one for elements in the XSLT namespace itself, and one for elements in the HTML 4.0 namespace. Although our sample orgchart.xml document contains no namespace declarations, the following XSLT style sheet, namespace.xsl, can be used with any source XML document which does contain them.
Note The <?xml-stylesheet?>
processing instruction refers to this style sheet itself. Therefore you can even open this style sheet directly in Internet Explorer.
The namespace.xsl style sheet contains three template rules:
<xsl:template match="/">
sets up basic HTML elements in the result tree.<xsl:template match="text()"/>
suppresses all text nodes.<xsl:template match="*">
matches all element nodes. For each element:
<elementname>
element:.local-name()
function for the context node; the value of the namespace-uri()
function for the context node; and the value(s) of any namespace node(s) in scope for the context node, each on a separate line.Regardless of the namespace URI, which applies to a given element name (shown in the second column), each element has a namespace node (third column, the nodes along the namespace::
axis) for every namespace that it or any of its ancestors declares.
XML File (nsdemo.xml)
<?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="namespace.xsl" ?> <nsdemo xmlns:ns="http://uri.for.ns-prefix" xmlns="http://uri.for.no-prefix"> <branch1> <ns:sub_branch1.1/> <sub_branch1.2/> </branch1> <branch2 xmlns:ns2="http://uri.for.ns2-prefix"> <ns:sub_branch2.1/> <sub_branch2.2/> <ns2:sub_branch2.3/> </branch2> </nsdemo>
XSLT File (namespace.xsl)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="namespace.xsl" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40" >
<xsl:template match="/">
<html>
<head><title>Namespaces in XSLT Style Sheet</title></head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="text()"/>
<xsl:template match="*">
<h3>Namespaces in effect for <<xsl:value-of select="name()"/>> element:</h3>
<table border="1">
<tr>
<th>Local-name</th>
<th>Namespace-uri</th>
<th>Namespace node(s)</th>
</tr>
<tr>
<td valign="top"><xsl:value-of select="local-name()"/></td>
<td valign="top"><xsl:value-of select="namespace-uri()"/></td>
<td valign="top">
<xsl:for-each select="namespace::*
">
<xsl:value-of select="."/><br />
</xsl:for-each>
</td>
</tr>
</table>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Formatted Output
Sample XML File for Navigating XPath Axes