tags:

views:

149

answers:

2

I have the following XML (simplified):

<node1>
    <node2>
        <node3>
        </node3>
    </node2>
</node1>

And I need to determine (using XSL) if node3 has a parent named node1 (not only the inmediate parent, so in the example node3 is a child of node1)

The following code is not working:

<xsl:if test="parent::node1">

</xsl:if>

Thank you

+1  A: 

try this:

<xsl:if test="count(ancestor::node1)&gt;0">

</xsl:if>

You can omit the count if you like, it is not required. It can be useful when you are in a recursive structure to find the depth the current node is at.

Gerco Dries
+4  A: 

node3 is not a direct child, it is a descendant. Use the ancestor axis instead, which selects all ancestors (parent, grandparent, etc.) of the current node.

http://www.w3schools.com/xpath/xpath_axes.asp

<xsl:if test="ancestor::node1">

</xsl:if>
Mads Hansen