-1

I'm trying to find the position of the <b> element (which I'd expect to be 2) in the below XML using XSLT:

<root>
  <a>
    ...
  </a>
  <b>
    ...
  </b>
</root>

I'm trying to retrieve this value during a for-each loop through root/*.

Based on this similar question, I've tried using: count(root/b/preceding-sibling::*)+1 but all I seem to get is the value '1'. Can anyone spot what I'm doing wrong here?

Community
  • 1
  • 1
danw
  • 1,608
  • 4
  • 29
  • 48
  • Where exactly are you when you use the above expression? XSLT is very much context-dependent. – michael.hor257k Jul 15 '14 at 16:15
  • Hi @michael.hor257k, please see my updated question. I'm trying to retrieve this value whilst looping through the children of `root`. In reality, I have more than 2 child elements of `root`, so I'm trying to figure out how to find the position of element 'x' whilst focusing on element 'y' in my loop. – danw Jul 15 '14 at 16:18

1 Answers1

1

I'm trying to retrieve this value during a for-each loop through root/*.

As I suspected, this is a context issue. During your loop, you are either in the context of <a> or of <b>. In both cases, the relative path root/b/... selects nothing. You may have more success using an absolute path:

 count(/root/b/preceding-sibling::*)+1
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51