I want to repeat the following lines in an XML document n times, n being set in the variable $n
|
The template shall return the CDATA string n x 2 times for i = 0 to n.
Well, that's fairly trivial:
<xsl:template name="repeat">
<xsl:param name="string"/>
<xsl:param name="n"/>
<xsl:param name="i" select="0"/>
<xsl:if test="2 * $n > $i">
<xsl:value-of select="substring-before($string,'[i]')" disable-output-escaping="yes"/>
<xsl:value-of select="concat('[', floor($i div 2), ']')"/>
<xsl:value-of select="substring-after($string,'[i]')" disable-output-escaping="yes"/>
<xsl:call-template name="repeat">
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="n" select="$n"/>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Or, if you prefer:
<xsl:template name="repeat">
<xsl:param name="string"/>
<xsl:param name="n"/>
<xsl:param name="i" select="0"/>
<xsl:if test="$n > $i">
<xsl:variable name="output">
<xsl:value-of select="substring-before($string,'[i]')"/>
<xsl:value-of select="concat('[', $i, ']')"/>
<xsl:value-of select="substring-after($string,'[i]')"/>
</xsl:variable>
<xsl:value-of select="concat($output, $output)" disable-output-escaping="yes"/>
<xsl:call-template name="repeat">
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="n" select="$n"/>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
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
<doc>
<generate/>
</doc>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="n" select="3"/>
<xsl:param name="baseString">
<![CDATA[
<Cell ss:StyleID="s22"><Data ss:Type="String">WSCEAllergens[i]</Data></Cell>
]]>
</xsl:param>
<!--Identity transform-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="generate">
<xsl:call-template name="copyXML">
<xsl:with-param name="string" select="$baseString"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="copyXML">
<xsl:param name="string"/>
<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:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
XML Output
(Formatted after transform. Actual output would be on a single line because of the normalize-space()
.)
<doc>
<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>
</doc>
Also note that the namespace prefix ss
should be bound to a namespace in the output.