Navigating Along the parent Axis

MSXML 5.0 SDK

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

Navigating Along the parent Axis

The parent:: axis is the inverse of the child:: axis. It selects the node in the document tree that is immediately above the context node, in hierarchical order.

All nodes except the root node have one and only one parent node; the root node has no parent. The parent of a namespace or attribute node is the element node declaring it. The parent of a comment or PI is the element that contains it, or, if the comment or PI precedes or follows the root element, the parent is the root node.

In the XSLT template rule shown below, the match pattern for the template rule sets the context node to a particular <director> element, the one whose <name> element equals "Michelle Votava". The template portion of the rule builds a table with several rows: one for column headings, and one for each attribute of the requested <director> element. For each such attribute, the template constructs a table cell for the name of the attribute, its value, and using the parent:: axis, the name and value of the attribute's parent.

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

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

</xsl:stylesheet>

Formatted Output

Note   The parent of an attribute is considered to be the element in whose start tag the attribute is assigned a value.

See Also

Navigating Along the child Axis