Althea Veronica
Althea Veronica

Reputation: 47

Get the position of a string in an Array using XSLT

I want to get the position of a string in an array. For example, my XML File is <Data>AAAA</Data>. And, I need to get the position that defines in the variable list:

<xsl:variable name="array" as="element()*">
    <Item>GGGG</Item>
    <Item>DDDD</Item>
    <Item>AAAA</Item>
    <Item>UUUU</Item>
</xsl:variable>

Given the example XML File, the output should generate the position of 'AAAA' in the array list. So, the output should be 2. The value of <Data> can also be changed. Here is my sample code for getting the position.

<xsl:value-of select="$array[Data[position()]]"/> 

But, I can't get the expected output. All the value in the variable item list was generated in the output.

Upvotes: 0

Views: 1399

Answers (1)

Aniket V
Aniket V

Reputation: 3247

XSLT 2.0 provides a index-of function which can be used in this case to determine the position of AAAA in the sequence contained in variable $array

<xsl:value-of select="index-of($array, Data)" />

This will return a value of 3 for your input <Data>AAAA</Data>.

Upvotes: 1

Related Questions