Ask Your Question
0

How can XSLT retrieve the position of an element that is between specific other elements?

asked 2023-05-04 20:10:43 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-04 20:17:02 +0000

woof gravatar image

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
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-04 20:10:43 +0000

Seen: 8 times

Last updated: May 04 '23