Using the ancestor Axis

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 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:

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"/>  </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 


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net