49

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
  • 332,285
  • 67
  • 532
  • 628
Wondering
  • 4,950
  • 22
  • 71
  • 90

1 Answers1

100

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

<xsl:value-of select="../@attr1"/>
Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
  • 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