Xslt Looping using xsl: variable

I wanted to use xsl: variable and loop based on counting it, but I'm not sure if it's possible in Xslt. for example if I have a counter of variable names

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

      

Can I use the variable below in the 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 (with line breaks):

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

      

Any help is appreciated. Thanks Mh.

+3


source to share


3 answers


Yes, this is possible in XSLT 2.0 (which, from as="xs:integer"

in your example, I assume you are using). The following conversion will give 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>

      

+3


source


Using XSLT 2.0, the 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>

      

+6


source


Example "loop" in:

http://www.w3schools.com/xsl/xsl_for_each.asp

you can "match" to every "item" without the need for "for every"

<xsl:template match="element" >
  <xsl:if test="not(constant(value, ' '))">
     <xsl:value-of select="concat(value/text(), ',')"/>
  <xsl:if>
</xsl:template>

      

+2


source







All Articles