xsl-list
[Top] [All Lists]

Re: [xsl] Grouping consecutive page numbers for index (xslt 1.0)

2013-08-21 09:29:53
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. ________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>