Xslt Looping using xsl:variable
xslt
Solution
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>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Problem
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.