xml - XSL-FO: Use variable within for-each select / group-ending-with statement? -


say have xml file looks this:

<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> 

and have xsl-fo looks this:

<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 (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> 

right now, code creates table 5 columns displays name of each of books in xml file. first, for-each-group partitions list 5 (or less, if there no more left) books, first for-each-select places book names in table, while second for-each-select figures out if there empty positions in table, , puts non-breaking spaces fill in last few columns.

the resulting table looks this:

picture of table

this fine , dandy, want able specify number of columns in table, instead of using current hardcoded value of 5. if instead supply xml file looks this...

<books>   <cols>3</cols>   /* list of books */ </books> 

is there way able use cols node specify number of columns want see in resulting table? if not, there better way accomplish i'm looking for?

i think necessary use variable this:

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

then, use variable in select statements, this:

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

and

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

demo: http://xsltransform.net/ppqshuc/1


Comments