Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store values in an array and use it later in XSL?

Tags:

xml

xslt-1.0

I would like to know how to store a value from XML to an array variable in XSL and use those arrays to create a new XML file.

Using XSL version 1.0, here's an example:

Convert sample1.xml TO sample2.xml using XSL

sample1.xml

<root>
    <transfer>A</transfer>
    <station>A</station>
    <transfer>B</transfer>
    <station>B</station>
    <transfer>C</transfer>
    <station>C</station>    
</root>

sample2.xml

<root>
    <fund>A</fund>
    <place>A</place>
    <fund>B</fund>
    <place>B</place>
    <fund>C</fund>
    <place>C</place>    
</root>

So I need to store all the values from the <transfer> tag to an array and put the values into the <fund> tag.

So far I extracted values from one XML file and put the values in another XML file.

How can I put these items into an array?

like image 252
Vignesh Lakshmanan Avatar asked Dec 31 '25 05:12

Vignesh Lakshmanan


1 Answers

As noted by Jeff Mercado, you don't need to store any values to solve the task at hand, because overriding the identity rule with two ranaming templates produces the wanted result.

However, in other tasks having an "array"-like capability comes handy.

Generally, you can define a variable to contain a specific set of nodes and these can be accessed by their position (in document order), specifying this position in a predicate.

Here is an example:

<xsl:variable name="vTransfers" select="/*/transfer"/>

defines a variable named vTransfers with value the node-set of all transfer elements each of which is a child of the top element of the XML document.

Then:

$vTransfers[1]

selects the first element that is contained in $vTransfers.

$vTransfers[2]

selects the second element that is contained in $vTransfers, ...

$vTransfers[position() = $k]

selects the node from $vTransfers whose position, in document order, is equal to the value contained in the variable $k.

like image 92
Dimitre Novatchev Avatar answered Jan 06 '26 23:01

Dimitre Novatchev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!