1

I'm trying to create a XSLT function that takes in an array and an integer (representing a position) and returns the element at the given position. This is what I have so far:

<xsl:function name="getArrayPosition" as="item()*">
  <xsl:param name="position"/>
  <xsl:param name="srcCodesArray" as="item()*"/>
  <xsl:value-of select="$srcCodesArray[$position]"/>
</xsl:function>

My input will be something very simple, such as

<srcCodes>
  <item>Test1</item>
  <item>Test2</item>
</srcCodes>
1
  • I think you have your answer, but next time it would be good to let people know what result you're getting from the code you have, and how it differs from the desired result. Commented Jul 23, 2013 at 13:26

1 Answer 1

2

If you pass in an xs:integer then your code should work to select an item by its position. But I would suggest to simply declare the variable of that type e.g.

<xsl:param name="position" as="xs:integer"/>

(where you put the namespace declaration xmlns:xs="http://www.w3.org/2001/XMLSchema" on the xsl:stylesheet), then it should work even if string values are passed in that can be cast to an integer or a node whose string value can be cast to an integer.

And of course doing <xsl:value-of select="$srcCodesArray[$position]"/> does not return the item itself but rather creates a text node with the string value of the item. So you want

<xsl:sequence select="$srcCodesArray[$position]"/>

instead.

Finally I would suggest to learn and use the proper terminology, the XSLT/XPath/XQuery data model does not have any arrays, it has sequences.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.