3

I have a variable as below:

<xsl:variable name="ARRAY">
One,Two,Three,Four
</xsl:variable>

With XSLT 2.0 I used tokenize functions and I set an array variable:

<xsl:variable name="tokenizedSample" select="tokenize($ARRAY,',')"/>

and get array value with:

<xsl:value-of select="$tokenizedSample[1]"/>

Unfortunately I must use XSLT 1.0 and I don't know as replace this situation... I found some examples to create a template as below:

 <xsl:template name="SimpleStringLoop">
    <xsl:param name="input"/>
    <xsl:if test="string-length($input) &gt; 0">
      <xsl:variable name="v" select="substring-before($input, ',')"/>
      <field>
        <xsl:value-of select="$v"/>
      </field>
      <xsl:call-template name="SimpleStringLoop">
        <xsl:with-param name="input" select="substring-after($input, ',')"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

and calling this template as below:

        <xsl:variable name="fields">
              <xsl:call-template name="SimpleStringLoop">
                <xsl:with-param name="input" select="$ARRAY"/>
              </xsl:call-template>
        </xsl:variable>

and accessing to this new array with:

<xsl:value-of select="$fields[1]"/>

but doesn't work.

How can I do?

I would like a XSLT 1.0 variable as array because I want read it with for example:

$newArray[1]

Thanks.

1
  • As you've discovered, XPath 1.0 (and hence XSLT 1.0) does not support arbitrary sequences of values, only sets of nodes from the original document. Rather than asking about the detail of how you could make this particular approach work in 1.0, it would be more productive to explain the higher level task you are trying to accomplish, with some example input and desired output - it's likely that a different approach entirely will be more appropriate. Commented Nov 13, 2015 at 16:32

1 Answer 1

4

I don't see why you would define a variable that needs tokenizing, instead of defining it as "tokenized" to begin with.

In XSLT 1.0, this could be done as:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://www.example.com/my"
exclude-result-prefixes="my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<my:items>
    <item>One</item>
    <item>Two</item>
    <item>Three</item>
    <item>Four</item>
</my:items>

<!-- the rest of the stylesheet -->

</xsl:stylesheet>

With this in place, you can do:

<xsl:value-of select="document('')/xsl:stylesheet/my:items/item[2]"/>

from anywhere in your stylesheet to retrieve "Two".


Of course, you could put the "array" into a variable:

<xsl:variable name="my-items" select="document('')/xsl:stylesheet/my:items/item" />

so that you can shorten the reference to:

<xsl:value-of select="$my-items[2]"/>
Sign up to request clarification or add additional context in comments.

6 Comments

My variable ARRAY is a param variable and I must read it as array. This param variable is set with "One,Two,Three,Four", but I can set it as I want. What do you suggest?
I try to set variable so: <xsl:variable name="test" select="'<items><item>one</item><item>two</item><item>three</item></items>'"> but nothing...
@Diaboliko "My variable ARRAY is a param variable and I must read it as array." I am not sure what you mean by that.
I am sure... I SOLVED with this: <xsl:variable name="columns" select="ext:node-set($inline-array)/*"/> Thanks all the same, it was enough just to search better
Please @Diaboliko make your comment as an answer to help other like me to get an array with this way.
|

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.