Example of Declaring Namespaces in XSLT

MSXML 5.0 SDK

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

Example of Declaring Namespaces in XSLT

Example

This example produces a table that lists each element in the style sheet, in document order, and provides some information about the parent of each element to make it easier to locate a specific instance of an element. The table also shows the namespaces associated with each element—that is, the namespace the element itself belongs to, and all the namespaces in scope for that element.

To view this example, view the XSLT file in Internet Explorer.

XML File

No XML file is required, because the XSLT file calls itself.

If you want to see the XSLT file applied to a separate XML file, you can use the Sample XML File (books.xml). Below the line <?xml version='1.0'?>, add the following line:

<?xml-stylesheet type="text/xsl" href="ns_decl.xsl"?>

XSLT File (ns_decl.xsl)

This file invokes itself, so you can view it directly in Internet Explorer. You could also use this style sheet with any XML document. Code related to namespaces is highlighted.

<?xml-stylesheet type="text/xsl" href="ns_decl.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="text()"/>

<xsl:template match="/">
    <HTML>
    <BODY><TABLE border="1">
        <tr style="background-color: silver">
            <th>Child Name</th>
            <th>Parent</th>
            <th>Child #</th>
            <th>Namespace URI</th>
            <th>Namespace(s) in Scope</th>
        </tr>
        <xsl:apply-templates/>
    </TABLE></BODY>
    </HTML>
</xsl:template>

<xsl:template match="*">
    <tr>
        <td valign="top"><xsl:value-of select="name()"/></td>
        <transform:variable name="root_or_name" 
            xmlns:transform="http://www.w3.org/1999/XSL/Transform">
            <transform:choose>
                <xsl:when test="name(..)=''">--ROOT--</xsl:when>
                <transform:otherwise>
                    <transform:value-of select="name(..)"/>
                </transform:otherwise>
            </transform:choose>
        </transform:variable>    
        <td valign="top">
           <xsl:value-of select="$root_or_name"/>
        </td>
        <td valign="top">
            <xsl:value-of select="position()"/> of <xsl:value-of select="last()"/>
        </td>
        <td valign="top">
           <xsl:value-of select="namespace-uri()"/>
        </td>
        <td>
            <xsl:for-each select="namespace::*">
                <xsl:value-of select="."/><br />
            </xsl:for-each>
        </td>
    </tr>
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

Formatted Output

The following output is for the elements used to generate the value of the root_or_name variable in the XSLT file. Notice that the namespace prefix was changed from xsl to transform, but the namespace URI remains unchanged. So <transform:variable> is equivalent to <xsl:variable>. This applies to all its children, as well.

Processor Output

A portion of the XSLT processor output is shown here, with line breaks added for clarity.

<?xml version="1.0" encoding="UTF-16"?>
<HTML xmlns="http://www.w3.org/TR/REC-html40">
<BODY>
<TABLE border="1">
<tr style="background-color: silver">
<th>Child Name</th>
<th>Parent</th>
<th>Child #</th>
<th>Namespace URI</th>
<th>Namespace(s) in Scope</th>
</tr>
<tr>
<td valign="top">xsl:stylesheet</td>
<td valign="top">--ROOT--</td>
<td valign="top">2 of 2</td>
<td valign="top">http://www.w3.org/1999/XSL/Transform</td>
<td>http://www.w3.org/XML/1998/namespace
<br />http://www.w3.org/1999/XSL/Transform
<br />http://www.w3.org/TR/REC-html40
<br /></td>
</tr>
<tr>
<td valign="top">xsl:template</td>
<td valign="top">xsl:stylesheet</td>
<td valign="top">1 of 3</td>
<td valign="top">http://www.w3.org/1999/XSL/Transform</td>
...
</TABLE>
</BODY></HTML>