XSLT递归地添加元素

XSLT递归地添加元素,xslt,xslt-1.0,Xslt,Xslt 1.0,我是XSLT的新手,尝试将传入文件转换为另一个程序处理。目标是尝试向现有XML文件添加空行,以模拟页面长度,以便其他程序正确处理。我需要计算每个元素之间的元素,如果总数小于75,则将剩余的行添加为空白。在XSLT中递归添加空行的最佳方法是什么?到目前为止,我传入的XML如下所示: <page> <line>Some text</line> <line>Some text</line> <line>So

我是XSLT的新手,尝试将传入文件转换为另一个程序处理。目标是尝试向现有XML文件添加空行,以模拟页面长度,以便其他程序正确处理。我需要计算每个
元素之间的
元素,如果总数小于75,则将剩余的行添加为空白。在XSLT中递归添加空行的最佳方法是什么?到目前为止,我传入的XML如下所示:

<page>
    <line>Some text</line>
    <line>Some text</line>
    <line>Some text</line>
    etc...
</page>

一些文本
一些文本
一些文本
等
我一直在看使用节点将事物分组的示例,如下所示(设置group size变量):


我还发现了一些关于分段递归的结果,如下例所示:

<xsl:template name="priceSumSegmented">
  <xsl:param name="productList"/>
  <xsl:param name="segmentLength" select="5"/>
  <xsl:choose>
    <xsl:when test="count($productList) > 0">
      <xsl:variable name="recursive_result1">
        <xsl:call-template name="priceSum">
          <xsl:with-param name="productList"
            select="$productList[position() <= $segmentLength]"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="recursive_result2">
        <xsl:call-template name="priceSumSegmented">
          <xsl:with-param name="productList"
            select="$productList[position() > $segmentLength]"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$recursive_result1 + $recursive_result2"/>
    </xsl:when>
    <xsl:otherwise><xsl:value-of select="0"/></xsl:otherwise>
  </xsl:choose> 
</xsl:template>


如果您能帮助我们找到解决问题的最佳方法,我们将不胜感激。

我认为您让问题变得比实际需要的更复杂了。即使在XSLT 1.0中,您也可以简单地执行以下操作:

<xsl:template match="page">
    <xsl:copy>
        <xsl:copy-of select="line"/>
        <xsl:call-template name="generate-lines">
            <xsl:with-param name="n" select="75 - count(line)"/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

<xsl:template name="generate-lines">
    <xsl:param name="n"/>
    <xsl:if test="$n > 0">
        <line/>
        <!-- recursive call -->
        <xsl:call-template name="generate-lines">
            <xsl:with-param name="n" select="$n - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>


您为什么只使用XSLT 1?在XSLT2和更高版本中,XPath2
to
表达式用于构造一个序列,例如
1到(75-计数(行))
,无需递归。谢谢。这一切都很顺利。我肯定认为这会更复杂。谢谢你的帮助。
<xsl:template match="page">
    <xsl:copy>
        <xsl:copy-of select="line"/>
        <xsl:call-template name="generate-lines">
            <xsl:with-param name="n" select="75 - count(line)"/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

<xsl:template name="generate-lines">
    <xsl:param name="n"/>
    <xsl:if test="$n > 0">
        <line/>
        <!-- recursive call -->
        <xsl:call-template name="generate-lines">
            <xsl:with-param name="n" select="$n - 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>