23

In my XML I have the following:

<a>
  <b>
    <c something="false">
      <d>
        <e>
          <f>someResult</f>
        </e>
      </d>
    </c>
  </b>
</a>

Now in the XSL within a loop I can do the following:

<xsl:value-of select="f"></xsl:value-of>

But how can I get the attribute in c?

I've tried doing the following

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

As well as trying parent and nothing seems to be working. Can you get parent nodes like this?

Also, I cannot just do:

<xsl:value-of select="/a/b/c/@something"></xsl:value-of>

As there can be multiple of c.

ingh.am
  • 25,981
  • 43
  • 130
  • 177

2 Answers2

51

To move up the tree you use ".." per level ie in this instance probably

select="../../../@something"

You can also select an ancestor node by name (approx)

select="ancestor::c[1]/@something"  

See http://www.stackoverflow.com/questions/3672992 for further examples

demongolem
  • 9,474
  • 36
  • 90
  • 105
kaj
  • 5,133
  • 2
  • 21
  • 18
12

Use:

ancestor::c[1]/@something

This selects the attribute named something of the first (from the current node upwards) ancestor named c.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • 1
    @AkashTantri, no, `ancestor::c` is the node-list of *all* ancestor nodes that are named "c". `ancestor::c[1]` selects the nearest "c" ancestor of the current node. – Dimitre Novatchev Apr 18 '18 at 15:56