[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] number collapsing


Subject: RE: [xsl] number collapsing
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 14 Dec 2004 13:59:40 -0000

One observation:

>        <xsl:when test="$begin castable as xs:integer">
>          <xsl:variable name="begin2" select="$begin" as="xs:integer"/>

This may fail if $begin is a string. The string "123" is castable to
xs:integer, but not assignable to xs:integer. Better to write the second
line as 

<xsl:variable name="begin" select="xs:integer($begin)" as="xs:integer"/>

to force the cast.

My personal preference is always to use the "as" attribute on xsl:param to
make it clear what type of argument you are expecting. This will lead to
much more comprehensible error messages if (when) you get it wrong.

I think your new problem can be tackled using a "group-starting-with". The
only problem is that this requires nodes rather than atomic values. So we'll
start by putting the numbers into a temporary tree:

<xsl:function name="f:ranges" as="xs:string">
  <xsl:param name="pagelist" as="xs:integer*"/>
  <xsl:variable name="tree">
    <xsl:for-each select="$pagelist">
      <page nr="{.}"/>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="ranges" as="xs:string*">
    <xsl:for-each-group select="$pagelist/page"
           group-starting-with="page[@nr ne preceding-sibling::page[1]/@nr +
1]">
      <xsl:sequence select="
         if (count(current-group()) eq 1) 
         then string(@nr)
         else concat(string(@nr), '-',
string(current-group()[last()]/@nr))"/>
    </xsl:for-each-group>
  </xsl:variable>
  <xsl:value-of>[<xsl:value-of select="$ranges" separator=",
"/>]</xsl:value-of>
</xsl:function>

Another nice grouping use case.

Not tested!

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


> -----Original Message-----
> From: Bruce D'Arcus [mailto:bdarcus@xxxxxxxxxxxxx] 
> Sent: 14 December 2004 12:39
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] number collapsing
> 
> Awhile back I got help from the list working on a function to 
> collapse 
> page numbers (e.g. 455-456 becomes 455-56).*
> 
> I now have a related issue, which I'm now sure how to tackle:
> 
> In numeric style citations, the in-text markers look like 
> [1].  If one 
> has multiple references, then, you'd get [1, 2, 3].
> 
> So how can I get [1-3] or (in some cases)  [1-3, 5]?
> 
> Bruce
> 
> * here it is:
> 
>    <xsl:function name="bib:number-condense">
>      <xsl:param name="begin"/>
>      <xsl:param name="end"/>
>      <xsl:choose>
>        <xsl:when test="$begin castable as xs:integer">
>          <xsl:variable name="begin" select="$begin" as="xs:integer"/>
>          <xsl:choose>
>            <xsl:when test="$begin gt 100 and $begin mod 100 
> and $begin 
> idiv 100 eq $end idiv 100">
>              <xsl:value-of select="$end mod 100"/>
>            </xsl:when>
>            <xsl:otherwise>
>              <xsl:value-of select="$end"/>
>            </xsl:otherwise>
>          </xsl:choose>
>        </xsl:when>
>        <xsl:otherwise>
>          <xsl:value-of select="$end"/>
>        </xsl:otherwise>
>      </xsl:choose>
>    </xsl:function>


Current Thread