0

I have a requirement where i have to save calculated values from each block of XML into a array like variable, i will use this array values for comparison later on in my XSLT code.

Can someone help in this that how can we save values in array in XSLT 1.0 or is there any other option to store such values.

Thanks, Mayank

1 Answer 1

3

There are no arrays in XSLT, you have primitive values of type string, number, boolean and complex data types of a node-set of XML nodes and of result tree fragments in XSLT 1.0 and several more primitive data types in XSLT 2.0 and sequences of nodes and atomic items as the complex data type.

Thus if you want to store data in XSLT 1.0 you store it in a result tree fragment e.g.

<xsl:variable name="data-rtf">
  <item>a</item>
  <item>b</item>
</xsl:variable>

then to process it further you need to use exsl:node-set or similar as in <xsl:variable name="data" select="exsl:node-set($data-rtf)" xmlns:exsl="http://exslt.org/common"/> to have a node-set and then you can access e.g. $data/item[1], $data/item[2].

With XSLT 2.0 you don't need the exsl:node-set or similar function, you can simply store data as a temporary tree (fragment) and access the nodes with XPath so you would use

<xsl:variable name="data">
  <item>a</item>
  <item>b</item>
</xsl:variable>

and then access $data/item[1], $data/item[2].

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

4 Comments

My requirement is that I have to calculate few values in each node and store these values at runtime into a variable and compare these values in the later stages in my xslt. I mean that the variable "data-rtf" would have values which would be computed during runtime and used in later stages of processing. I also looked for options to read the accumlated values from result tree but couldnt find an option.
Inside of <xsl:variable>...</xsl:variable> you can use any XSLT code to populate it at run-time.
$data/item[1] works, but $data/item[$num] doesn’t (where <xsl:param name="num" as="xsl:number"/>)
it seems $data/item[position() = $num] works

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.