Add repeating XML code n times via xslt template

2020-04-18 07:36发布

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 &gt; 1>
  <Cell ss:StyleID="s22">
    <Data ss:Type="String">WSCEAllergens[1]</Data>
  </Cell>
    <xsl:if test="$n &gt; 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]

标签: xml xslt
2条回答
We Are One
2楼-- · 2020-04-18 07:44

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>
查看更多
Luminary・发光体
3楼-- · 2020-04-18 07:50

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.

查看更多
登录 后发表回答