I want to repeat the following lines in an XML document n times, n being set in the variable $n
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[i]</Data>
</Cell>
and rather than writing clumsy cascades like
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[0]</Data>
</Cell>
<xsl:if test="$n > 1>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[1]</Data>
</Cell>
<xsl:if test="$n > 2>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[2]</Data>
</Cell>
.
.
.
</xsl:if>
</xsl:if>
I'd like to solve this with an elegant template, but I have no idea how to iteratively glue XML and text strings together to get something like this:
n=3
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[0]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[1]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[2]</Data>
</Cell>
I have applied Daniel's code to my XSLT with this template
<xsl:template name="repeater">
<xsl:param name="string"/>
<xsl:param name="n"/>
<xsl:param name="count" select="0"/>
<xsl:value-of select="normalize-space(
concat(substring-before($string,'['),
'[',$count,']',
substring-after($string,']')))" disable-output-escaping="yes"/>
<xsl:if test="$n - 1 > $count">
<xsl:call-template name="copyXML">
<xsl:with-param name="count" select="$count+1"/>
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="n" select="$n"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
and with the following call
<xsl:call-template name="repeater">
<xsl:with-param name="n" select="$nAllergens"/>
<xsl:with-param name="string">
<![CDATA[
<Cell ss:StyleID="s22"><Data ss:Type="String">WSCEAllergens[i]</Data></Cell>
]]>
</xsl:with-param>
</xsl:call-template>
$nAllergens being 3, I get back
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[0]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[1]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[2]</Data>
</Cell>
That's good. But what if I want to get
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[0]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[0]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[1]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[1]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[2]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">WSCEAllergens[2]</Data>
</Cell>
?
I have a) added a second line to my template b) changed the concat-call to concat(substring-before($string,'['), '[',$count,']',substring-after($string,']'), substring-before($string,'['), '[',$count,']',substring-after($string,']'))
Both ways I only get WSCEAllergens[0] WSCEAllergens[0] WSCEAllergens[1] WSCEAllergens[2]
Well, that's fairly trivial:
Or, if you prefer:
Here's one option. In the example, the element
generate
will be replaced by the string$n
number of times. I'm using DOE (disable-output-escaping
) so the string is actual XML in the output. If it's supposed to be a string, just remove the DOE attribute.XML Input
XSLT 1.0
XML Output
(Formatted after transform. Actual output would be on a single line because of the
normalize-space()
.)Also note that the namespace prefix
ss
should be bound to a namespace in the output.