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 nodeunless 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:
<?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"/> </xsl:template> </xsl:stylesheet>
And heres the result when I use this stylesheet on planets.xml:
<?xml version="1.0" encoding="utf-8"?> Mercury Venus Earth