The descendant-or-self::
axis, like the descendant::
axis, locates all nodes which are descended from the context node, its children, its children's children, and so on. Unlike descendant::
, however, descendant-or-self::
also selects the context node itself.
Attribute and namespace nodes cannot be located using the descendant-or-self::
axis. Since attribute and namespace nodes have no descendants, using descendant-or-self::
when the context node is an attribute or namespace node always returns an empty node-set. To locate descendant comment and processing instruction (PI) nodes as well as elements, use the node()
, comment()
, or processing-instruction()
node-type test with this axis.
To display information about the employee named Steve Masters and the employees whose elements descend from his in orgchart.xml, you can use an XSLT template rule such as orgchart-descself.xsl, below.
Once the template rule's match pattern ("name[.='Steve Masters']"
) has established the branch of the family tree to be processed, the template rule sets up a level-2 heading and a table for each <name>
element node along the descendant-or-self::
axis. Each row of the table, other than column headers, contains the value of that <name>
element, and the value of its parent's empID
and empdate
attributes.
XML File (orgchart.xml)
Use orgchart.xml (in Sample XML File for Navigating XPath Axes) and edit its href
attribute to refer to orgchart-descself.xsl.
XSLT File (orgchart-descself.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[.='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>
</xsl:stylesheet>
Formatted Output
Note Kim Akers (Steve Masters's "parent") and Katie McAskill-White (Masters's "sibling") do not appear in the list, as neither of those relationships are ever found on the descendant-or-self::
axis.
Navigating Along the descendant Axis