XSL-FO:在for-each select / group-ending-with语句中使用变量?

时间:2015-07-16 14:56:31

标签: xml xslt-2.0 xsl-fo

假设我有一个如下所示的XML文件:

dnx-clr-win-x64/1.0.0-beta4

我有一些看起来像这样的XSL-FO:

<books>
  <book>
    <name>Book 1</name>
  </book>
  <book>
    <name>Book 2</name>
  </book>
  <book>
    <name>Book 3</name>
  </book>
  <book>
    <name>Book 4</name>
  </book>
  <book>
    <name>Book 5</name>
  </book>
  <book>
    <name>Book 6</name>
  </book>
  <book>
    <name>Book 7</name>
  </book>
  <book>
    <name>Book 8</name>
  </book>
  <book>
    <name>Book 9</name>
  </book>
  <book>
    <name>Book 10</name>
  </book>
</books>

现在,此代码创建一个包含5列的表,该列显示XML文件中每本书的名称。首先,<xsl:template match="books" mode="table"> <fo:table border="1pt solid black" table-layout="fixed" width="100%" font-size="11pt"> <fo:table-body border="inherit"> <xsl:for-each-group select="book" group-ending-with="book[position() mod 5 = 0]"> <fo:table-row table-layout="fixed" border="inherit"> <xsl:for-each select="current-group()"> <fo:table-cell border="inherit" padding-left="5px" padding-top="1px" padding-bottom="1px"> <fo:block> <xsl:value-of select="name" /> </fo:block> </fo:table-cell> </xsl:for-each> <xsl:for-each select="0 to (5 - count(current-group())-1)"> <fo:table-cell border="inherit"> <fo:block> <xsl:text>&nbsp;</xsl:text> </fo:block> </fo:table-cell> </xsl:for-each> </fo:table-row> </xsl:for-each-group> </fo:table-body> </fo:table> </xsl:template> 将列表划分为5个(或更少,如果没有更多左侧)书籍,然后第一个for-each-group将书名放在表中,而第二个for-each-select弄清楚我的表格中是否有任何空位,并在最后几列填写不间断的空格。

结果表如下所示:

picture of table

这一切都很好,但是现在我希望能够指定表中的列数,而不是使用当前的硬编码值5.所以,如果我是提供一个看起来像的xml文件此...

for-each-select

有没有什么办法可以使用<books> <cols>3</cols> /* list of books */ </books> 节点来指定我想在结果表中看到的列数?如果没有,是否有更好的方法来完成我正在寻找的东西?

1 个答案:

答案 0 :(得分:1)

我认为只需要使用这样的变量:

<xsl:template match="books" mode="table">
    <xsl:variable name="cols" select="xs:integer(cols)"/>

然后,在select语句中使用变量,如下所示:

<xsl:for-each-group select="book" group-ending-with="book[position() mod $cols = 0]">

<xsl:for-each select="0 to ($cols - count(current-group())-1)">

演示: http://xsltransform.net/pPqsHUc/1

相关问题