Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To retrieve the position of an element that is between specific other elements, you can use the "count" and "preceding-sibling" functions in XSLT.

For example, if you want to retrieve the position of an "item" element that is between two "category" elements, you can use the following XSLT expression:

count(preceding-sibling::category) + 1

This expression counts the number of "category" elements that come before the current "item" element, and then adds 1 to the count to get the position of the current "item" element.

Assuming you have the following XML structure:

<root>
  <category>Category 1</category>
  <item>Item 1.1</item>
  <item>Item 1.2</item>
  <category>Category 2</category>
  <item>Item 2.1</item>
  <item>Item 2.2</item>
  <item>Item 2.3</item>
</root>

If you apply the above XSLT expression to each "item" element, you will get the following positions:

  • "Item 1.1": position 1
  • "Item 1.2": position 2
  • "Item 2.1": position 1
  • "Item 2.2": position 2
  • "Item 2.3": position 3