[xsl] Using for-each-group on a <day> collection to arrange a weekday calendar

Subject: [xsl] Using for-each-group on a <day> collection to arrange a weekday calendar
From: "Michael Friedman sumarimike@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 7 Nov 2017 18:02:43 -0000
Greetings,

I am struggling with taking a collection of <day> elements and formatting
them in groups of 5, to simulate a weekday calendar in PDF output. I've
looked through the archives and not been able to find anything that gets me
further than where I am stuck.

XML:
-------
<course id="c1">
<segment id="se1">
<day id="d1">
Day 1
</day>
<day id="d2">
Day 2
</day>
</segment>
<segment id="se2">
<day id="d3">
Day 3
</day>
</segment>
...
</course>

Imagine if you will, two courses. One course has 24 days, a second course
has 16 days.

The desired output is a table for course 1:
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
Day 6 | Day 7 | Day 8 | Day 9 | Day 10
Day 11 | Day 12 | Day 13 | Day 14 | Day 15
Day 16 | Day 17 | Day 18 | Day 19 | Day 20
Day 21 | Day 22 | Day 23 | Day 24 |

and for course two:
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
Day 6 | Day 7 | Day 8 | Day 9 | Day 10
Day 11 | Day 12 | Day 13 | Day 14 | Day 15
Day 16 |

Using the XSLT below, I am getting a successful course 1 table (I think by
accident), but my course 2 table looks like this:
Day 1
Day 2 | Day 3 | Day 4 | Day 5 | Day 6
Day 7 | Day 8 | Day 9 | Day 10 | Day 11
Day 12 | Day 13 | Day 14 | Day 15 | Day 16

I could use some help solving. I've gone code-blind. I think it's related
to how I am dividing up my group by testing the remainder of the total
divided by 5. If I monkey with that calculation I get bizarre results.

XSLT2.0 using Arbortext PE 6.1 (via XSL only transform, so through saxon
9.1.0.5 build 121514):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format";
version="2.0">

<xsl:template match="course">
<xsl:variable name="v_dayCount">
<xsl:value-of select="count(descendant::day)"/>
</xsl:variable>
<fo:block margin-top="12pt">
<fo:block text-align="center" id="@id">
<fo:table border="1pt solid black">
<fo:table-column column-width="100pt"/>
<fo:table-body>
<xsl:for-each-group select="descendant::day"
group-ending-with="day[(count(preceding::day[ancestor::course[1]])+1) mod 5
= 0]">
<xsl:sort select="position()" order="ascending" data-type="number"/>
<fo:table-row height="15pt" background-color="gray" border-top="1pt solid
black">
<xsl:for-each select="current-group()">
<xsl:apply-templates select="self::day" mode="calendartitle"/>
</xsl:for-each>
</fo:table-row>
</xsl:for-each-group>
</fo:table-body>
</fo:table>
</fo:block>
</fo:block>
</xsl:template>

<xsl:template match="day" mode="calendartitle">
<fo:table-cell border-bottom="1pt solid black" border-right="1pt solid
black" display-align="center">
<fo:block font-size="8pt" text-align="center" font-weight="bold">
<fo:basic-link internal-destination="@id">
<xsl:value-of select="title"/>
</fo:basic-link>
</fo:block>
</fo:table-cell>
</xsl:template>

</xsl:stylesheet>

Kind Regards,
Michael Friedman [PST time zone]

Current Thread