Your expression :
//*[generate-id()=generate-id($myVariable[7])]/ancestor::*
right one .
The reason that it "does not work" may be due to the fact that $myVariable[7] does not contain the expected.
Here is a simple complete example using the above expression and getting the expected. correct results :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:variable name="myVariable" select="/*/*/*/*/num"/> <xsl:template match="/"> <xsl:for-each select= "//*[generate-id() = generate-id($myVariable[7]) ] /ancestor::* "> <xsl:value-of select="name()"/> <xsl:text>
</xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet>
when this conversion is applied to the following XML document :
<a> <b> <c> <nums> <num>01</num> <num>02</num> <num>03</num> <num>04</num> <num>05</num> <num>06</num> <num>07</num> <num>08</num> <num>09</num> <num>10</num> </nums> </c> </b> </a>
the desired, correct result is created (the names of all the ancestors of $myVariable[7] ) :
a b c nums
source share