3

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?

2 Answers 2

4

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.

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

Comments

2

I don't see why you would need an array. You're just renaming the elements. Just use these transforms:

<xsl:template match="transfer">
  <fund><xsl:value-of select="."/></fund>
</xsl:template>

<xsl:template match="station">
  <place><xsl:value-of select="."/></place>
</xsl:template>

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.