Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xslt Looping using xsl:variable

Tags:

xslt

I wanted to use xsl:variable and loop based on it's count, but I am not sure if its possible in Xslt. for example if I have a variable name count

<xsl:variable name="count" as="xs:integer" select="4"/>

Can I make use of variable, in below form!!!

<xsl:if test="some condition"/>
loop from 0 to $count
...do something here
end loop
</xsl:if>

Is it possible?

My Input XML:

<Root>
<Element>
    <Value>1</Value>
    <Value>2</Value>
</Element>
<Element>
    <Value>1</Value>
    <Value>2</Value>
    <Value>3</Value>
    <Value>4</Value>
</Element>
    <Element>
    <Value>1</Value>
</Element>
</Root>

Expected output in flat file is (with line-breaks):

1,2,,
1,2,3,4
1,,,

Any help appreciated. Thanks Mh.

like image 377
user1961100 Avatar asked Dec 11 '22 17:12

user1961100


2 Answers

Using XSLT 2.0 a solution could be:

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" />
    <xsl:variable name="count" select="4" />

    <xsl:template match="Element">
        <xsl:value-of select="for $i in 1 to $count return concat(Value[$i], '')"
                      separator="," />
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

    <xsl:template match="text()" />

</xsl:stylesheet>

Note: you can also use an if statement instead of the concat function.


Just for completeness, a solution written using XSLT 1.0:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" />

    <xsl:variable name="count" select="4" />

    <!-- Ignore all text elements -->    
    <xsl:template match="text()" />

    <xsl:template match="Element">
        <xsl:if test="$count > 0">
            <!-- Output existing values -->
            <xsl:apply-templates select="Value[position() &lt;= $count]" />
            <!-- Output remaining commas -->
            <xsl:call-template name="print-commas">
                <xsl:with-param name="number"
                                select="$count - count(Value)" />
            </xsl:call-template>            
            <!-- Line break -->
            <xsl:text>&#xA;</xsl:text>
        </xsl:if>
    </xsl:template>

    <!-- Print the first value without a comma preprended to the value -->
    <xsl:template match="Value[1]">
        <xsl:value-of select="." />
    </xsl:template>

    <!-- Print the reamaining value with a comma preprended to the value -->
    <xsl:template match="Value">
        <xsl:value-of select="concat(',', .)" />
    </xsl:template>

    <!-- Print the given amount of commas -->
    <xsl:template name="print-commas">
        <!-- Number of commas to be printed -->
        <xsl:param name="number" />

        <xsl:if test="$number > 0">
            <xsl:text>,</xsl:text>
            <!-- Recursive call decrementing the number of commas to
                 be printed -->
            <xsl:call-template name="print-commas">
                <xsl:with-param name="number"
                                select="$number - 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>
like image 144
Pablo Pozo Avatar answered Jan 22 '23 04:01

Pablo Pozo


Yes, this is possible in XSLT 2.0 (which, from the as="xs:integer" in your example, I assume you're using). The following transform will produce your expected output from your example input:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="count" select="4"/>

    <xsl:template match="text()" />

    <xsl:template match="Element">
        <xsl:variable name="curElement" select="."/>
        <xsl:for-each select="1 to $count">
            <xsl:variable name="curVal" select="."/>
            <xsl:value-of select="$curElement/Value[. = $curVal]"/>
            <xsl:if test="$curVal != $count">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>
like image 21
JLRishe Avatar answered Jan 22 '23 02:01

JLRishe