0

Is it possible to tell whether a node is contained within (or equal to) another node in XSLT? For example, consider this code snippet:

<xsl:variable name="itemSection" select=".."/>
<xsl:for-each select="key('enemyItems', @key)">
    <xsl:variable name="enemyList" select="./attributes/@value"/>
    <xsl:variable name="enemyListSection" select="../../.."/>
                      .
                      .
                      .
</xsl:for-each>

Is it possible to tell whether itemSection is contained within (or equal to) enemyListSection?

Paul Reiners
  • 8,576
  • 33
  • 117
  • 202
  • check for Dimitre correct answer. I've missed the "(or equal to)" part. –  Jul 30 '10 at 17:56

2 Answers2

2

In XPath 1.0

$itemSection[ancestor::*[generate-id()=generate-id($enemyListSection)]]

In XPath 2.0

$itemSection[ancestor::*[. is $enemyListSection]]
1

Just a small adjustment to Alejandro's answer:

In XPath 1.0

$itemSection[ancestor-or-self::*[generate-id()=generate-id($enemyListSection)]] 

In XPath 2.0

$itemSection[ancestor-or-self::*[. is $enemyListSection]]

Because the original question asked:

Is it possible to tell whether itemSection is contained within (or equal to) enemyListSection?

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I've missed the important "(or equal to)". Yours is the correct answer. –  Jul 30 '10 at 17:55