0
votes

I have the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>title1</title>
  </cd>
  <cd>
    <title>title1</title>
  </cd>
  <cd>
    <title>title2</title>
  </cd>
  <cd>
    <title>title2</title>
  </cd>
  <cd>
    <title>title2</title>
  </cd>
  <cd>
    <title>title2</title>
  </cd>
  <cd>
    <title>title4</title>
  </cd>
  <cd>
    <title>title5</title>
  </cd>
 </catalog>

Is there a way to know the position of the first <cd> that contains title2 ?

i have tryed with

<xsl:value-of select="count(catalog/cd[contains(., 'title2')])"/>

and

<xsl:value-of select="count(catalog/cd[./title='title2']/preceding-sibling::*)" />

but they counts nodes that contains title2

2

2 Answers

1
votes

I think that the expression should be

<xsl:value-of select="count(//catalog/cd[1]/following-sibling::cd[title='title2'][1]/preceding-sibling::cd)+1" />
0
votes

In addition, if for-each /catalog/cd and wants to find the first /title = 'title2' but starting from the current for-each position, you could use

<xsl:value-of select="count(current()[1]/following-sibling::cd[title='title2'][1]/preceding-sibling::cd)+1" />

Which is exactly what i need. Thanks @gtosto !