Using the ancestor Axis

The ancestor axis contains all the ancestors of the context node, including its parents, grandparents, great-grandparents, and so on. This axis always contains the root node—unless the context node is the root node. Look at the following example, which shows how to use the ancestor axis to find the names (as stored in a <NAME> element) of all the ancestors of <MASS> elements:

Listing 7.3. Using the ancestor Axis
 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="MASS"> <xsl:for-each select="ancestor::*"> <xsl:value-of select="./NAME"/> </xsl:for-each> </xsl:template> <xsl:template match="PLANET"> <xsl:apply-templates select="MASS"/> ...

Get Inside XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.