Navigating Along the attribute Axis

MSXML 5.0 SDK

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

Navigating Along the attribute Axis

When the context node is an element, use the attribute:: axis to locate that element's attributes. The attribute:: axis can be used only with element-type context nodes, and it locates attributes only.

To display a table of all employees' IDs and dates of employment, you could use three XSLT template rules, one that employs the attribute:: axis. This is shown in the XSLT sample file below.

The first template rule sets up the body of the result tree, including a table and a row of table headers. The second template rule suppresses the output of all element and text nodes, note the empty <xsl:template> element.

The third template rule makes an exception for the second, which is to process all <name> elements. The template placed into the result tree by this rule consists of a table row with four cells:

  • The value of this <name> element
  • The value of the parent element's empID attribute
  • The value of the parent element's empdate attribute

Example

XML File (orgchart.xml)

Use orgchart.xml (in Sample XML File for Navigating XPath Axes) and edit its href attribute to refer to orgchart-attr.xsl.

XSLT File (orgchart-attr.xsl)

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- suppress text nodes not covered in subsequent template rule -->
<xsl:template match="text()"/>

<xsl:template match="/">
    <html>
        <head><title>Employee Info</title></head>
             <body>
              <table border="1">
                <tr>
                    <th>Name</th>
                    <th>Emp ID</th>
                    <th>Empl Date</th>
                </tr>
                <xsl:apply-templates />
            </table>
             </body>
    </html>
</xsl:template>

<xsl:template match="*/text()"/>

<xsl:template match="name">
    <tr>
        <td><xsl:value-of select="."/></td>
        <td><xsl:value-of select="parent::*/attribute::empID"/></td>
        <td><xsl:value-of select="parent::*/attribute::empdate"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

Formatted Output