recursion

Here should go questions about transforming XML with XSLT and FOP.
coupes
Posts: 12
Joined: Tue Jun 14, 2005 6:40 pm

recursion

Post by coupes »

Hi, it's me again :wink:

consider the following .xml :

Code: Select all


<list>
<item>item 1</item>
<item>item 2</item>
<item>item 3</item>
...
<item>item 15</item>
</list>
I want to create a list of the different items, however I only want a maximum of 10 items per page. I could I generate a new page each time I reached 10 items on the current page. I assume I'd have to use a recursive template but I've been looking into it for several hours now and I couldn't find a solution.

Any help would be immensly appreciated.

Thanks in advance,
Coupes
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Coupes,

You can use something similar with the following sample:

XML:

Code: Select all


<list>
<item>item 1</item>
<item>item 2</item>
<item>item 3</item>
<item>item 4</item>
<item>item 5</item>
<item>item 6</item>
<item>item 7</item>
<item>item 8</item>
<item>item 9</item>
<item>item 10</item>
<item>item 11</item>
<item>item 12</item>
<item>item 13</item>
<item>item 14</item>
<item>item 15</item>
<item>item 16</item>
</list>
XSL:

Code: Select all


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="list">
<xsl:for-each select="item[position() mod 10 = 1]">
<newPage>
<xsl:apply-templates select=".|following-sibling::item[position() <10]"/>
</newPage>
</xsl:for-each>
</xsl:template>
<xsl:template match="item">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
Result:

Code: Select all


<?xml version="1.0" encoding="utf-8"?>
<newPage>
<item>item 1</item>
<item>item 2</item>
<item>item 3</item>
<item>item 4</item>
<item>item 5</item>
<item>item 6</item>
<item>item 7</item>
<item>item 8</item>
<item>item 9</item>
<item>item 10</item>
</newPage>
<newPage>
<item>item 11</item>
<item>item 12</item>
<item>item 13</item>
<item>item 14</item>
<item>item 15</item>
<item>item 16</item>
</newPage>
Best Regards,
George
coupes
Posts: 12
Joined: Tue Jun 14, 2005 6:40 pm

Post by coupes »

Thank you very much George, I would never have thought of using modulus, sometimes I forget there is such an operator :roll:

The code works very well, and the solution is much simpler than I would have thought. Thank you again, this is much apreciated.

Regards,
Coupes
Post Reply