Re: [xsl] Kosher XSLT 3.0 numbering solution?

Subject: Re: [xsl] Kosher XSLT 3.0 numbering solution?
From: "Martin Honnen martin.honnen@xxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 1 Sep 2016 14:04:10 -0000
On 01.09.2016 15:51, David Sewell dsewell@xxxxxxxxxxxx wrote:
I have a numbering task that is trivially easy to handle using an XSLT
extension that permits modification of global variables (such as
saxon:assign) but which is harder to accomplish via pure XSLT. I don't
*need* to accomplish this as I have code that works fine, but I'm just
curious about alternate solutions.

I have an XML file representing the contents of a printed book. I need
to add page break indicators that carry the page numeration, e.g. <pb
n="51"/>. Because I'm lazy, I just want to plug in empty  markers and
then run a script to add the numeration.

This would be a straightforward job for xsl:number if all pagination was
sequential. However, parts of the book are commented out, so there may
be a page jump from say page 11 to page 30. In a case like that, I will
insert <pb n="30"/> to provide a hard-coded indication of the number.
That makes use of xsl:number more complicated. The solution via updates
to a global variable is simple, along the lines of

  <xsl:variable name="bodyPageNo" select="0" as="xs:integer"
saxon:assignable="yes"/>
  <xsl:template match="pb">
    <xsl:variable name="pageNumber">
       <saxon:assign name="bodyPageNo" select="
          if (@n) then xs:integer(@n) else $bodyPageNo + 1
          "/>
       <xsl:value-of select="$bodyPageNo"/>
    </xsl:variable>
   [etc.]

If I avoid saxon:assign, I don't see any way around some moderately
messy brute-force calculation in the <pb> template to identify the
closest preceding <pb> element with an @n value (if any), and use that
value plus the number of intervening <pb> elements to assign a
numeration to the current element. Does anything jump out as a more
elegant approach?


I wonder, given XSLT 3.0 as the target, whether the above does not translate directly into an accumulator e.g.

<xsl:accumulator name="bodyPageNo" as="xs:integer" initial-value="0">
<xsl:accumulator-rule match="pb" select="if (@n) then xs:integer(@n) else $value + 1"/>
</xsl:accumulator>


then elsewhere you can use

accumulator-before('bodyPageNo')

respectively

accumulator-after('bodyPageNo')

Current Thread