Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Navigating Along the namespace Axis

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:

Example

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 &lt;<xsl:value-of select="name()"/>&gt; 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

See Also

Sample XML File for Navigating XPath Axes