Mega Code Archive

 
Categories / XML Tutorial / Xpath
 

Select=ancestor

File: Data.xml <?xml version="1.0"?> <names>   <name>     <given>A</given>     <family>B</family>   </name> </names> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <xsl:output method="text" />   <xsl:template match="/">     <xsl:apply-templates select="child::names" />   </xsl:template>   <xsl:template match="child::names">     <xsl:apply-templates select="child::name[18]" />   </xsl:template>   <xsl:template match="child::name[18]">     <xsl:value-of       select="ancestor::names/child::name[1]/child::given" />     <xsl:text> </xsl:text>     <xsl:value-of       select="ancestor::names/child::name[1]/child::family" />     <xsl:text> is first on the list, and </xsl:text>     <xsl:value-of select="child::given" />     <xsl:text> </xsl:text>     <xsl:value-of select="child::family" />     <xsl:text> is last.</xsl:text>   </xsl:template> </xsl:stylesheet>