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

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


Subject: Re: [xsl] Grouping consecutive page numbers for index (xslt 1.0)
From: David Carlisle <davidc@xxxxxxxxx>
Date: Wed, 21 Aug 2013 15:29:33 +0100

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
Keywords