Search code examples
xsltxslt-2.0ibm-datapower

How to Store For Loop Result into XSLT Variable


I have below XSLT and i want to save for loop result into XSLT variable or DP variable

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:variable name="Variable_01">88888,777777</xsl:variable>
    <xsl:variable name="Variable_02">abc,xyz</xsl:variable>
    <root>
        <xsl:for-each select="tokenize($Variable_01, ',')">
            <xsl:variable name="i" select="position()"/>
            <xsl:text>{"Group":"</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>", "Name":"</xsl:text>
            <xsl:value-of select="tokenize($Variable_02, ',')[$i]"/>
            <xsl:text>"}</xsl:text>
            <xsl:if test="position()!=last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </root>
</xsl:template>


Solution

  • Wrap the xsl:for-each with an xsl:variable and the product of the xsl:for-each will be assigned to that variable.

    The following demonstrates how to do that:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
            <xsl:variable name="Variable_01">88888,777777</xsl:variable>
            <xsl:variable name="Variable_02">abc,xyz</xsl:variable>
            <root>
                <xsl:variable name="Variable_03">
                   <xsl:for-each select="tokenize($Variable_01, ',')">
                       <xsl:variable name="i" select="position()"/>
                       <xsl:text>{"Group":"</xsl:text>
                       <xsl:value-of select="."/>
                       <xsl:text>", "Name":"</xsl:text>
                       <xsl:value-of select="tokenize($Variable_02, ',')[$i]"/>
                       <xsl:text>"}</xsl:text>
                       <xsl:if test="position()!=last()">
                           <xsl:text>,</xsl:text>
                       </xsl:if>
                   </xsl:for-each>
                </xsl:variable>
                <xsl:sequence select="$Variable_03"/>
            </root>
        </xsl:template>
    </xsl:stylesheet>
    

    You could also move that variable declaration and for-each outside of the root element:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
            <xsl:variable name="Variable_01">88888,777777</xsl:variable>
            <xsl:variable name="Variable_02">abc,xyz</xsl:variable>
            <xsl:variable name="Variable_03">
                <xsl:for-each select="tokenize($Variable_01, ',')">
                    <xsl:variable name="i" select="position()"/>
                    <xsl:text>{"Group":"</xsl:text>
                    <xsl:value-of select="."/>
                    <xsl:text>", "Name":"</xsl:text>
                    <xsl:value-of select="tokenize($Variable_02, ',')[$i]"/>
                    <xsl:text>"}</xsl:text>
                    <xsl:if test="position()!=last()">
                        <xsl:text>,</xsl:text>
                    </xsl:if>
                </xsl:for-each>
            </xsl:variable>
            <root>
                <xsl:sequence select="$Variable_03"/>
            </root>
        </xsl:template>
    </xsl:stylesheet>