1

is it possible declare a string array in XSLT 2.0?

I would like declare something as below:

<xsl:variable name="countries" select="('IT','EN','SP')" />

but doesn't work.

Thanks.

3
  • 1
    What does "but doesn't work" mean? Also, it's a sequence; not an array. Commented Nov 12, 2015 at 16:04
  • Possible duplicate of creating arrays in xslt Commented Nov 12, 2015 at 16:07
  • How can I create an array of string? This post is not duplicated... Commented Nov 13, 2015 at 7:47

1 Answer 1

2

There are no arrays in the data model that XSLT/XPath 2.0 work with. Your code <xsl:variable name="countries" select="('IT','EN','SP')" /> binds a sequence of string values to the variable named countries and you should be able to access single items with a positional predicate like e.g. $countries[2].

If you think you need arrays then you need to use XPath 3.1 where you can use <xsl:variable name="countries" select="['IT', 'EN', 'SP']"/>, see http://www.w3.org/TR/xpath-31/#id-arrays. I am not aware of any released XSLT processor that supports this, however, I think XQuery implementations like BaseX have already been upgraded to support this.

As long as you simply want to store three or more atomic values in a flat data type I don't see why you would need the new array feature, the sequence ('IT','EN','SP') should suffice. Arrays can be nested, sequences not, as (1, (2, 3), 4) results in a flat sequence (1, 2, 3, 4), so in case you needed a nested data structure (with primitive values like numbers or strings) you might need an array and have to wait until there is support for XPath 3.1, you can however always created an XML tree structure for nested data in current versions of XSLT.

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

3 Comments

I solved with: <xsl:variable name="tokenized" select="tokenize($countries,',')"/>
My question now is: with XSLT 1.0 how do it?
I don't understand what you think you have solved with tokenize($countries,','). As for XSLT 1.0, its data types are primitive values like strings, booleans and numbers and "complex" values like node sets and result tree fragments. There no sequences of strings in that data model, nor arrays of any type. If you want to store three string values then the closest is a node set of three elements, each containing one string. If you want values not selected in an input document then you need to create a result tree fragment and convert it into a node set with exsl:node-set.

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.