XSL:FO :在循环中重复相同的 table 行

XSL:FO : Repeat same table row in a loop

我正在通过 XSL:FO 生成标签。标签将使用相同的文本。所以我的 PDF 页面由 table 组成,只有 n 次相同的行,直到页面结束:

<fo:table-row>
  <fo:table-cell>
   <fo:block>
    <xsl:value-of select = "foo"/>
   </fo:block>
  </fo:table-cell>
  <fo:table-cell>
   <fo:block>
    <xsl:value-of select = "bar"/>
   </fo:block>
  </fo:table-cell>
</fo:table-row>

我不想复制粘贴代码n次来填满一个页面。我尝试了某种循环,将行添加 n 次。

我发现了这个结构:

<xsl:for-each select="1 to 20">..</xsl:for-each>

没有成功,但出现了错误。

有什么简单的循环方法 XSL:FO?

提前致谢。

正如@martin-honnen 刚才所说,但代码为:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="xs" version="2.0">
    <xsl:variable name="root" select="/" />
    <xsl:template match="/">
        <xsl:for-each select="1 to 10">
            <xsl:for-each select="$root">
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="foo" />
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="bar" />
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

<xsl:for-each select="1 to 10"> 中,上下文是 1 to 10 的当前整数值,您不能 select 节点相对于整数。您需要通过为当前节点创建一个变量并在内部 xsl:for-each(如上所述)或您的 select 属性中使用该变量来重新建立上下文。哪个更好主要取决于个人喜好,这可以归结为您必须在 select 属性中重复引用变量的次数。


如果您坚持使用 XSLT 1.0,xsl:for-each 需要能够 select 足够的节点(实际上来自任何地方)以便能够重复正确的次数:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="xs" version="1.0">
    <xsl:variable name="document" select="/*" />
    <xsl:template match="/*">
        <xsl:for-each select="(//node())[position() &lt;= 10]">
            <xsl:for-each select="$document">
                <fo:table-row>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="foo" />
                        </fo:block>
                    </fo:table-cell>
                    <fo:table-cell>
                        <fo:block>
                            <xsl:value-of select="bar" />
                        </fo:block>
                    </fo:table-cell>
                </fo:table-row>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

但是,IMO 最好忘记 xsl:for-each 等,并递归执行:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format"
    exclude-result-prefixes="xs" version="1.0">
    <xsl:template match="/*" name="row">
        <xsl:param name="count" select="1" />
        <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    <xsl:value-of select="foo" />
                </fo:block>
            </fo:table-cell>
            <fo:table-cell>
                <fo:block>
                    <xsl:value-of select="bar" />
                </fo:block>
            </fo:table-cell>
        </fo:table-row>
        <xsl:if test="$count &lt; 10">
            <xsl:call-template name="row">
                <xsl:with-param name="count" select="$count + 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>