iPogosov
iPogosov

Reputation: 367

How to read attribute of a parent node from a child node in XSLT

Just want to know how to read an attribute of a parent node(A) from a child node(c) in XSLT. code:

<A attr1="xx">
  <b>
    <c>
    </c>
  </b>
</A>

XSLT:

<xsl:template match="c">
  <xsl:value-of select="attribute of A node">
</xsl:template>

Upvotes: 1

Views: 635

Answers (1)

Tim C
Tim C

Reputation: 70598

A is not actually the parent of c but an ancestor (b is the parent!), but the code you are looking for is this

<xsl:value-of select="ancestor::A/@attr1">

(You could replace ancestor with parent in the case A was the direct parent of c)

You could also do this:

<xsl:value-of select="../../@attr1">

But this would assume A is always the 'grand-parent` (i.e. the parent of the parent).

Upvotes: 3

Related Questions