[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Grouping consecutive page numbers for index (xslt

Subject: Re: Grouping consecutive page numbers for index (xslt 1.0)
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Thu, 22 Aug 2013 12:59:19 -0400
Re:  Grouping consecutive page numbers for index (xslt
Hi,

The same, using sibling recursion (all the usual caveats apply):

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

   <xsl:template match="page">
      <xsl:copy>
         <xsl:apply-templates select="." mode="range"/>
      </xsl:copy>
      <xsl:apply-templates select="following-sibling::page[1]" mode="next-out"/>
   </xsl:template>

   <xsl:template match="page" mode="range">
      <xsl:apply-templates/>
      <xsl:apply-templates select="following-sibling::page[1]" mode="next-in"/>
   </xsl:template>

   <xsl:template match="page[not(. = preceding-sibling::page[1] + 1)]"
mode="next-in" priority="1"/>

   <xsl:template match="page[not(. + 1 = following-sibling::page[1])]"
mode="next-in">
      <xsl:text>-</xsl:text>
      <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="page" mode="next-in">
      <xsl:apply-templates select="following-sibling::page[1]" mode="next-in"/>
   </xsl:template>

   <xsl:template match="page" mode="next-out">
      <xsl:apply-templates select="following-sibling::page[1]" mode="next-out"/>
   </xsl:template>

   <xsl:template match="page[not(. = preceding-sibling::page[1] + 1)]"
mode="next-out">
      <xsl:apply-templates select="."/>
   </xsl:template>

David's is a little shorter, but mine has more templates!

Of course, XSLT 2.0 is so much nicer --

<xsl:template match="pages">
   <xsl:copy>
      <xsl:for-each-group select="page"
group-starting-with="page[not(. = preceding-sibling::page[1] + 1)]">
         <page>
            <xsl:value-of select="current-group()[1]"/>
            <xsl:if test="exists(current-group()[2])">
               <xsl:text>-</xsl:text>
               <xsl:value-of select="current-group()[last()]"/>
            </xsl:if>
         </page>
      </xsl:for-each-group>
   </xsl:copy>
</xsl:template>

Cheers, Wendell

Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^


On Wed, Aug 21, 2013 at 10:29 AM, David Carlisle <davidc@xxxxxxxxx> wrote:
> On 21/08/2013 14:49, Kevin Bird wrote:
>>
>> Hello,
>>
>> Would really appreciate some guidance with the following problem.
>> I'm stuck with XSLT 1.0.
>> Any pointers as to the best approach would help.
>>
>> INPUT:
>> <pages>
>>         <page>1</page>
>>         <page>2</page>
>>         <page>3</page>
>>         <page>6</page>
>>         <page>12</page>
>>         <page>13</page>
>>         <page>14</page>
>>         <page>15</page>
>>         <page>16</page>
>> </pages>
>>
>> DESIRED OUTPUT:
>> <pages>
>>         <page>1-3</page>
>>         <page>6</page>
>>         <page>12-16</page>
>> </pages>
>>
>> Regards.
>>
>> --
>
>
>
>
> <page>1-3</page>
> <page>6</page>
> <page>12-16</page>
>
>
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:output indent="yes"/>
>
> <xsl:template match="pages">
> <xsl:apply-templates select="page[1]">
>  <xsl:with-param name="first" select="page[1]"/>
>  <xsl:with-param name="last" select="page[1]"/>
> </xsl:apply-templates>
> </xsl:template>
>
> <xsl:template match="page">
>  <xsl:param name="first"/>
>  <xsl:param name="last"/>
>  <xsl:choose>
>   <xsl:when test="following-sibling::page[1][.=$last+1]">
>    <xsl:apply-templates select="following-sibling::page[1][.=$last+1]">
>     <xsl:with-param name="first" select="$first"/>
>     <xsl:with-param name="last" select="$last+1"/>
>    </xsl:apply-templates>
>   </xsl:when>
>   <xsl:when test="following-sibling::page">
>    <page><xsl:if test="$first!=$last"><xsl:value-of
> select="$first"/>-</xsl:if><xsl:value-of select="$last"/></page>
>    <xsl:apply-templates select="following-sibling::page[1]">
>     <xsl:with-param name="first" select="following-sibling::page[1]"/>
>     <xsl:with-param name="last" select="following-sibling::page[1]"/>
>    </xsl:apply-templates>
>   </xsl:when>
>     <xsl:otherwise>
>    <page><xsl:if test="$first!=$last"><xsl:value-of
> select="$first"/>-</xsl:if><xsl:value-of select="$last"/></page>
>     </xsl:otherwise>
>  </xsl:choose>
> </xsl:template>
>   </xsl:stylesheet>
>
>
> David
>
>
> ________________________________________________________________________
> The Numerical Algorithms Group Ltd is a company registered in England
> and Wales with company number 1249803. The registered office is:
> Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.
>
> This e-mail has been scanned for all viruses by Star. The service is
> powered by MessageLabs.
> ________________________________________________________________________

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.