RE: [xsl] Can't solve the following XSL transformation

Subject: RE: [xsl] Can't solve the following XSL transformation
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 25 Jul 2005 07:31:26 +0100
Here's a simplified form of the problem. Given this input:

<chap>
  <para lines="3"/>
  <para lines="5"/>
  <para lines="8"/>
  etc
</chap>

produce a copy of the input, with a <page-break/> element inserted after any
para that causes the cumulative number of lines to exceed 30.

The answer to this is what I call "sibling recursion", something like this:

<xsl:template match="chap">
<xsl:copy>
  <xsl:apply-templates select="para[1]"/>
</xsl:copy>
</xsl:template>

<xsl:template match="para">
  <xsl:param name="lines-so-far" select="0"/>  <!-- 0 is the default value
of the parameter -->
  <xsl:copy-of select="."/>
  <xsl:choose>
  <xsl:when test="$lines-so-far + @lines <= 30">
    <xsl:apply-templates select="following-sibling::para[1]">
       <xsl:with-param name="lines-so-far" select="$lines-so-far + @lines"/>
    </xsl:apply-templates>
  </xsl:when>
  <xsl:otherwise>
    <page-break/>
    <xsl:apply-templates select="following-sibling::para[1]"/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Sibling recursion using xsl:apply-templates has a couple of advantages over
call-template recursion: the sequence over which you are iterating is an
implicit parameter (it's identified implicitly by the context node); and the
recursion terminates naturally when there are no more siblings.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: John Robb [mailto:john_ok@xxxxxx]
> Sent: 25 July 2005 06:22
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] Can't solve the following XSL transformation
>
> I'm a novice in XSLT and it's not very easy for me to realize the
> second part of the algorithm.Can you please give me a short example.
> Thank you in advance.
>
> Sun, 24 Jul 2005 23:26:09 +0100, "Michael Kay" <mike@xxxxxxxxxxxx>
> PISAL(A):
>
> > The first stage is a classical grouping problem: see
> > http://www.jenitennison.com/xslt/grouping for advice on using
> >Muenchian
> > grouping in XSLT 1.0, or use the new xsl:for-each-group instruction
> >in 2.0.
> > (Note, XSLT 1.1 doesn't really exist, it was a working draft that
> >wasn't
> > followed up).
> >
> > Insert the page breaks in a second pass through the data. This is a
> >little
> > tricky (I use it as an exercise on courses) and requires processing
> >the
> > sequence using recursion, passing a parameter in the recursive call
> >that
> > indicates the number of lines processed so far.
> >
> > Michael Kay
> > http://www.saxonica.com/
> >
> >> -----Original Message-----
> >> From: John Robb [mailto:john_ok@xxxxxx]
> >> Sent: 24 July 2005 22:45
> >> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >> Subject: [xsl] Can't solve the following XSL transformation
> >>
> >> I'm a novice in XSLT and doesn't have any experience.Can
> you help me
> >> to perform the following transformation:
> >> trasformed data should look as follows:
> >>
> >> ****************
> >> Sector 1
> >> ****************
> >> Subsector 1
> >> ++++++++++++++++
> >> ID[t]Value
> >> ----------------
> >> id1[t]value1
> >> ...
> >> idN valueN
> >> ----------------
> >> Subsector 2
> >> ++++++++++++++++
> >> ID[t]Value
> >> ----------------
> >> id1[t]value1
> >> ...
> >> idN valueN
> >> ...
> >> ****************
> >> Sector 2
> >> ****************
> >> Subsector 1
> >> ++++++++++++++++
> >> ID[t]Value
> >> ----------------
> >> id1[t]value1
> >> ...
> >> idN valueN
> >> ----------------
> >> Subsector 2
> >> ++++++++++++++++
> >> ID[t]Value
> >> ----------------
> >> id1[t]value1
> >> ...
> >> idN valueN
> >> ...
> >>
> >> There is a limitation: no more than 25 data rows (idX valueX) per
> >> page.Page breaks (<span style="page-break-before: always"></span>)
> >> cannot be inserted in the middle of data section but before
> >> sector/subsector only.I'm using XSLT v1.1.
> >> Here is the xml:
> >> <?xml version="1.0" encoding="iso-8859-1" ?>
> >> <data>
> >> [t]<item id="i1"[t]sector="s1"[t]subsector="u1">561</item>
> >> [t]<item id="i10"[t]sector="s3"[t]subsector="u4">15</item>
> >> [t]<item id="i22"[t]sector="s2"[t]subsector="u2">1234</item>
> >> [t]<item id="i11"[t]sector="s1"[t]subsector="u2">123</item>
> >> [t]<item id="i17"[t]sector="s1"[t]subsector="u3">165</item>
> >> [t]<item id="i61"[t]sector="s2"[t]subsector="u1">346</item>
> >> [t]<item id="i12"[t]sector="s2"[t]subsector="u5">3425</item>
> >> [t]<item id="i2"[t]sector="s3"[t]subsector="u4">78</item>
> >> [t]<item id="i14"[t]sector="s3"[t]subsector="u4">51</item>
> >> [t]<item id="i21"[t]sector="s1"[t]subsector="u5">346</item>
> >> [t]<item id="i39"[t]sector="s3"[t]subsector="u2">463</item>
> >> [t]<item id="i44"[t]sector="s2"[t]subsector="u3">151</item>
> >> [t]<item id="i89"[t]sector="s1"[t]subsector="u1">451</item>
> >> [t]<item id="i81"[t]sector="s2"[t]subsector="u4">771</item>
> >> [t]<item id="i36"[t]sector="s2"[t]subsector="u5">5654</item>
> >> [t]<item id="i27"[t]sector="s3"[t]subsector="u3">362</item>
> >> [t]<item id="i15"[t]sector="s1"[t]subsector="u5">234</item>
> >> [t]<item id="i18"[t]sector="s3"[t]subsector="u2">73</item>
> >> [t]<item id="i51"[t]sector="s3"[t]subsector="u5">567</item>
> >> [t]<item id="i26"[t]sector="s1"[t]subsector="u4">17</item>
> >> [t]<item id="i95"[t]sector="s3"[t]subsector="u5">67489</item>
> >> [t]<item id="i13"[t]sector="s1"[t]subsector="u3">54</item>
> >> [t]<item id="i71"[t]sector="s1"[t]subsector="u3">2</item>
> >> [t]<item id="i23"[t]sector="s2"[t]subsector="u1">345</item>
> >> [t]<item id="i7"[t]sector="s2"[t]subsector="u1">67</item>
> >> [t]<item id="i80"[t]sector="s1"[t]subsector="u3">7754</item>
> >> [t]<item id="i9"[t]sector="s3"[t]subsector="u4">343</item>
> >> [t]<item id="i4"[t]sector="s1"[t]subsector="u2">51</item>
> >> [t]<item id="i99"[t]sector="s3"[t]subsector="u5">5637</item>
> >> [t][t]
> >> [t][t]<sector id="s1">Sector 1</sector>
> >> [t]<sector id="s2">Sector 2</sector>
> >> [t]<sector id="s3">Sector 3</sector>
> >>
> >> [t]<subsector id="u1">Subsector 1</subsector>
> >> [t]<subsector id="u2">Subsector 2</subsector>
> >> [t]<subsector id="u3">Subsector 3</subsector>
> >> [t]<subsector id="u4">Subsector 4</subsector>
> >> [t]<subsector id="u5">Subsector 5</subsector>
> >> </data>

Current Thread