building an XSLT string using variable in a foreach loop

xslt change variable value in a loop
xslt for loop counter example
xslt variable
xslt variable change value
xsl:for-each variable
xslt test if variable is empty
how to assign value-of one variable to another variable in xslt
xslt concatenate strings in loop

The problem I'm facing perhaps simple for others, but being a beginner XSL - I'm yet to find a proper solution. What I want to do is build a string by concatenating the results of variables define in for-each loop.

Sorry it should be this:

<linked-hash-map>
<entry>
    <string>properties</string>
    <list>
        <linked-hash-map>
            <entry>
                <string>property_id</string>
                <int>123</int>
            </entry>
            <entry>
                <string>type</string>
                <string>H</string>
            </entry>
            <entry>
                <string>status</string>
                <string>CURRENT</string>
            </entry>
            <entry>
                <string>description</string>
                <string>Test</string>
            </entry>
            <entry>
                <string>lots</string>
                <list>
                    <linked-hash-map>
                        <entry>
                            <string>lot_id</string>
                            <int>123</int>
                        </entry>
                        <entry>
                            <string>lot_number</string>
                            <int>11</int>
                        </entry>
                        <entry>
                            <string>plan_number</string>
                            <int>100</int>
                        </entry>
                        <entry>
                            <string>plan_type</string>
                            <string>CC</string>
                        </entry>
                        <entry>
                            <string>plan_id</string>
                            <int>1</int>
                        </entry>
                    </linked-hash-map>
                </list>
            </entry>
        </linked-hash-map>
    </list>
</entry>

Desired output: SP - 31 - 108661

So basically I want to concatenate the plan_type, lot_number, and plan_number together into one string.

I've tried this:

<xsl:for-each select="linked-hash-map/entry/linked-hash-map/entry/list/linked-hash-map/entry" >
    <xsl:choose>
        <!-- LOT NUMBER -->
        <xsl:when test="string[1] = 'lot_number'">
            <xsl:variable name="lot_number" select="int" />
        </xsl:when>
        <!-- PLAN NUMBER -->
        <xsl:when test="string[1] = 'plan_number'">
            <xsl:variable name="plan_number" select="int" />
        </xsl:when>
        <!-- PLAN TYPE -->
        <xsl:when test="string[1] = 'plan_type'">
            <xsl:variable name="plan_type" select="string[2]" />
        </xsl:when>
    </xsl:choose>
</xsl:for-each>

<!-- LOT DETAIL -->
// concatenate string in here.
<xsl:value-of select="concat($plan_type, '-', $lot_number, '-', $plan_number)" />

But it doesn't work because of the variable being out of scope. How can I achieve that desired output? Thank you.


To change as little as possible, try the following XSLT-1.0 code:

<xsl:template match="linked-hash-map">
    <xsl:variable name="plan_type" select="entry[string='plan_type']/string[2]/text()" />
    <xsl:variable name="lot_number" select="entry[string='lot_number']/int/text()" />
    <xsl:variable name="plan_number" select="entry[string='plan_number']/int/text()" />
    <xsl:value-of select="concat($plan_type, ' - ', $lot_number, ' - ', $plan_number)" /> 
</xsl:template> 

Its result is:

SP - 31 - 108661

building an XSLT string (variable) in a foreach loop, I don't believe you can use XSL variables to concatenate, because once a variable value is set, it can't be changed. Instead, I think you want  My courage is not enough to read all your code. But from a theorical point of view and considering the way variables work in XSLT, the normal way to do what you want is to use the main and natural characteristic of a template : recursivity.


Note that variables are local in scope to the block in which they are declared, so the each variable in this case would only be accessible within the xsl:when

But you don't need xsl:for-each or variables here. You can just do something like this (assuming you were in a template matching linked-hash-map

<xsl:value-of select="concat(entry[string[1]='plan_type']/string[2], '-', entry[string[1]='lot_number']/int, '-', entry[string[1]='plan_number']/int)" />

Or maybe, to make it a bit more readable, separate out the lines

<xsl:value-of select="entry[string[1]='plan_type']/string[2]" />
<xsl:text>-</xsl:text>
<xsl:value-of select="entry[string[1]='lot_number']/int" />
<xsl:text>-</xsl:text>
<xsl:value-of select="entry[string[1]='plan_number']/int" />

See http://xsltfiddle.liberty-development.net/eiZQaGs/2

building a XSLT string (variable) in a foreach loop, I don't think you can use XSL variables to concatenate, because once the value of a variable is set, it cannot be changed . Instead, I think you want something  I am using the for-each loop to get the values of the sub-nodes of the <employee> node.The problem I am facing is I cannot figure out how to store the concatenated values in another temperoary variable in XSLT.I found on many sites that we cannot update the variables in the XSLT, so is there any alternative solution for this?


We don't have an XSLT version number on the question, so I wondered what I would write in 3.0. Perhaps:

        <xsl:value-of select="for $n in ('plan_type', 'lot_number', 'plan_number') 
                              return entry[string[1]=$n]/*[2]" 
                      separator=" - "/>

set variable in foreach and use it outside the foreach - XSLT, In a foreach loop I need to get the first value if the querystring is empty. convert it once, first build you variable up using a temp variable then  The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!


[Solved] Update variable value in XSLT for-each loop, Not sure why you want to screw up a well formed XML document and create something that will be difficult to maintain, but who am I to judge. Building on the previous example, we can also use a ForEach loop to read multiple data points out of a set of .txt files. The following script works in much the same way as those in the previous example, but performs a little more complex data processing on the data it reads:


XSLT: Need to concatenate strings in loop and hold them for later use, Hi, I am making some loops using xsl:for-each. Now I want to And want to keep it in some variable for later use. e.g.: for each iteration  The loop container gets the XML File into variables name "XMLFileName" which the loop current file, in turn, used by XML Task for validation. XML task is configured in the option "Validate". I have provided the XML Data in variable name "XMLFileName" also get the name from the loop container and XSD file content File Connection.


XSLT <xsl:for-each> Element, Well organized and easy to understand Web building tutorials with lots of examples of how to The <xsl:for-each> element allows you to do looping in XSLT. Processing multiple files in SSIS with the foreach loop The SSIS foreach loop is configured to allow you to easily ingest multiple data files in a single data flow task. For this to work, all of the files would need to reside in the same directory structure (either locally or on the network), and they must all have the same structure and metadata.