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

Sample XSLT File for Navigating XPath Axes

A subset of this file is used in each topic for XPath navigation axes.

XSLT File (orgchart.xsl)

<?xml version="1.0"?>
<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

<!-- Common template rule (suppresses all text nodes not 
   explicitly covered in succeeding template rules) -->
<xsl:template match="text()"/>

<!-- Parent axis -->
<xsl:template match="director[name='Michelle Votava']">
   <table border="1">
      <tr>
         <th>Attribute name</th>
         <th>Attribute value</th>
         <th>Parent name</th>
         <th>Parent value</th>
      </tr>
          <xsl:for-each select="attribute::*">
             <tr>
               <td><xsl:value-of select="name()"/></td>
               <td><xsl:value-of select="."/></td>
               <td><xsl:value-of select="name(parent::*)"/></td>
               <td><xsl:value-of select="parent::*"/></td>
             </tr>
          </xsl:for-each>
   </table>
</xsl:template>

<!-- Ancestor axis -->
<xsl:template match="*[name='Peter Porzuczek']">
   <h2>Peter Porzuczek's superiors:</h2>
       <xsl:for-each select="ancestor::*">
          <h4><xsl:value-of select="name"/></h4>
       </xsl:for-each>
</xsl:template>

<!-- Following axis -->
<xsl:template match="name[.='Katie McAskill-White']">
   <h2>Katie McAskill-White's followers:</h2>
       <xsl:for-each select="following::*/name">
          <h4><xsl:value-of select="."/></h4>
       </xsl:for-each>
</xsl:template>

<!-- Preceding axis -->
<xsl:template match="name[.='John Tippett']">
   <table border="1">
      <tr>
         <th>Node name</th>
         <th>Node value</th>
      </tr>
          <xsl:for-each select="preceding::*">
         <xsl:variable name="bgcolor">
                <xsl:choose>
                   <xsl:when test="name()='name'">yellow</xsl:when>
                   <xsl:otherwise>white</xsl:otherwise>
                </xsl:choose>
         </xsl:variable>
             <tr>
                <xsl:attribute name="style">background-color: <xsl:value-of select="$bgcolor"/>
                </xsl:attribute>
                <td>
                   <xsl:value-of select="name()"/>
                </td>
                <td>
                   <xsl:value-of select="."/>
                </td>
             </tr>
          </xsl:for-each>
       </table>
</xsl:template>

<!-- Following-sibling axis -->
<xsl:template match="name[.='Shelly Szymanski']">
   <xsl:if test="following-sibling::*">
      <h2>following-siblings of &lt;name&gt; 
         '<xsl:value-of select="."/>'</h2>
      <table border="1">
         <tr>
            <th>Node name</th>
            <th>Node value</th>
         </tr>
             <xsl:for-each select="following-sibling::*">
                <tr>
                   <td><xsl:value-of select="name()"/></td>
                   <td><xsl:value-of select="."/></td>
                </tr>
             </xsl:for-each>
          </table>
       </xsl:if>
</xsl:template>

<!-- Preceding-sibling axis -->
<xsl:template match="chairman">
   <xsl:if test="preceding-sibling::node()">
      <h2>preceding-siblings of '<xsl:value-of select="name"/>'</h2>
   <table border="1">
         <tr>
            <th>Node name</th>
            <th>Node value</th>
      </tr>
      <xsl:for-each select="preceding-sibling::node()">
          <tr>
            <td><xsl:value-of select="name()"/></td>
             <td><xsl:value-of select="."/></td>
          </tr>
       </xsl:for-each>
       </table>
   </xsl:if>
   <xsl:apply-templates/>
</xsl:template>

<!-- Attribute axis (note: two template rules) -->
<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="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>

<!-- Self axis -->
<xsl:template match="president[division='International']">
   <xsl:for-each select="descendant::name">
      <h2><xsl:value-of select="self::*"/></h2>
   </xsl:for-each>
</xsl:template>

<!-- Descendant-or-self axis -->
<xsl:template match="name[.='Steve Masters']">
   <h2>Steve Masters and descendant elements:</h2>
   <table border="1">
      <tr>
         <th>Name</th>
         <th>Emp ID</th>
         <th>Empl Date</th>
      </tr>
      <xsl:for-each select="parent::*/descendant-or-self::name">
         <tr>
            <td><xsl:value-of select="."/></td>
            <td><xsl:value-of select="parent::*/@empID"/></td>
            <td><xsl:value-of select="parent::*/@empdate"/></td>
         </tr>
      </xsl:for-each>
   </table>
</xsl:template>

<!-- Ancestor-or-self axis -->
<xsl:template match="name[.='Josh Barnhill']">
   <h2>Josh Barnhill's reporting structure:</h2>
   <table border="1">
      <tr>
         <th>Generation</th>
         <th>Name</th>
         <th>Emp ID</th>
         <th>Empl Date</th>
      </tr>
      <xsl:for-each select="ancestor-or-self::*[name()!='name']">
         <tr>
            <td align="center">
               <xsl:value-of select="position()-last()"/>
            </td>
            <td><xsl:value-of select="name"/></td>
            <td><xsl:value-of select="@empID"/></td>
            <td><xsl:value-of select="@empdate"/></td>
         </tr>
      </xsl:for-each>
   </table>
</xsl:template>

</xsl:stylesheet>