48

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

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

XSLT:

<xsl:template match="c">
  <xsl:value-of select="attribute of b node">
</xsl:template>
Tomalak
  • 322,446
  • 66
  • 504
  • 612
Wondering
  • 4,820
  • 21
  • 70
  • 90

1 Answers1

98

You can go "up" a level using "..". So:

<xsl:value-of select="../@attr1"/>
Adam Batkin
  • 49,616
  • 8
  • 123
  • 114
  • 1
    yeah just now coded //@attr1 and it worked for me...anyways thanks for ur help. – Wondering Sep 11 '09 at 08:54
  • 25
    @Wondering - the expression "//@attr1" will scan the ENTIRE document (and won't stop even when it finds the first match). This is very inefficient and could grab the wrong @attr1(if you have that attribute in other places). @Adam Batkin's solution is more efficient and less likely to accidentally select the wrong value. – Mads Hansen Sep 11 '09 at 11:05
  • @Mads: Thanks for ur inputs and information,will implement the same – Wondering Sep 14 '09 at 07:50