Surender Thakran
Surender Thakran

Reputation: 4054

How to select child of an ancestor via XPath?

I have an XML structure similar to:

<a>
   <b>
      <c id="2.3">
      </c>
   </b>
   <d>
      <e>
         <f>
         </f>
      </e>
   </d>
</a>

I am inside a template for "f" where i want to put a when with a test on the first number of id of c, for instance:

<xsl:template match="f">
   <xsl:choose>
      <xsl:when test="substring-before(../../../c/@id, '.') = '2'">
         <xsl:text>Successful</xsl:text>
      </xsl:when>
   </xsl:choose>
</xsl:template>

The above code doesn't works!

Can anyone suggest me a way to make it work?

Thnx in advance!!

Upvotes: 1

Views: 71

Answers (2)

Tomalak
Tomalak

Reputation: 338416

Relative paths are nice, but sometimes it's useful to be more explicit.

<xsl:template match="f">
   <xsl:choose>
      <xsl:when test="substring-before(ancestor::a[1]/b/c/@id, '.') = '2'">
         <xsl:text>Successful</xsl:text>
      </xsl:when>
   </xsl:choose>
</xsl:template>

Upvotes: 4

BeniBela
BeniBela

Reputation: 16947

Because there is a b between the ancestor and the c.

This should work:

substring-before(../../../b/c/@id, '.') = '2'

(assuming the second <b> was supposed to be a </b>)

Upvotes: 4

Related Questions