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

Navigating Along the following-sibling Axis

The following-sibling:: XPath axis locates all nodes, elements only, by default which share the same parent as the context node itself, and which appear in their entirety after the context node, in document order. Since the root node has no parent, it also has no following-siblings. If the context node is an attribute or namespace node, the following-sibling:: node locates an empty node-set.

In the sample XML document, each <name> element is followed in the document tree by information about the given employee's organization, including employees at the same level of the hierarchy. This information can be captured by the XSLT template rule for the employee named Shelly Szymanski, shown in the sample XSLT file.

The template rule in the sample XSLT file contains two references to the following-sibling:: axis. The first, used as the value of the <xsl:if> element's test attribute, ensures that the template will be created in the result tree only if the selected <name> element has following-sibling nodes.

This example also demonstrates that the string-value of an element node is a combination of all text nodes contained by that element. In our sample document, the <director> elements do not contain any text nodes of their own. The only text nodes belong to the <name> and <region> elements subordinate to a given <director> element. Thus, the string-value of each <director> element is the combined string-value of all subordinate <name> and <region> elements.

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-followsib.xsl.

XSLT File (orgchart-followsib.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="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>

</xsl:stylesheet>

Formatted Output