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

Re: XSLT 1 and grouping numbers by if the following n

Subject: Re: XSLT 1 and grouping numbers by if the following number is current number + 1 and so on
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 24 Mar 2014 10:44:21 -0400
Re:  XSLT 1 and grouping numbers by if the following  n
At 2014-03-24 07:08 -0700, Mario Madunic wrote:
(using XSLT 1)
I would like to group numbers by if the following number is current number + 1 and so on.


Example:

<a>1</a>
<a>3</a>
<a>4</a>
<a>5</a>
<a>7</a>
<a>9</a>
<a>10</a>

<group><a>1</a></group>
<group><a>3 <a>4</a> <a>5</a></group>
<group><a>7</a></group>
<group><a>9</a> <a>10</a></group>

Any insight will be appreciated.

You can use keys as illustrated below.


I hope this helps.

. . . . . . . . Ken

T:\ftemp>type mario.xml
<?xml version="1.0" encoding="UTF-8"?>
<as>
 <a>1</a>
 <a>3</a>
 <a>4</a>
 <a>5</a>
 <a>7</a>
 <a>9</a>
 <a>10</a>
</as>
T:\ftemp>type mario.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:key name="firsts"
         match="a"
   use="generate-id((. | preceding-sibling::a)
                    [not(. = preceding-sibling::a[1] + 1)]
                    [last()])"/>

<xsl:output indent="yes"/>

<xsl:template match="as">
  <groups>
    <xsl:for-each select="a[key('firsts',generate-id(.))]">
      <group>
        <xsl:copy-of select="key('firsts',generate-id(.))"/>
        </group>
    </xsl:for-each>
  </groups>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>xslt mario.xml mario.xsl
<?xml version="1.0" encoding="utf-8"?>
<groups>
   <group>
      <a>1</a>
   </group>
   <group>
      <a>3</a>
      <a>4</a>
      <a>5</a>
   </group>
   <group>
      <a>7</a>
   </group>
   <group>
      <a>9</a>
      <a>10</a>
   </group>
</groups>
T:\ftemp>





--
Public XSLT, XSL-FO, UBL & code list classes: Melbourne, AU May 2014 |
Contact us for world-wide XML consulting and instructor-led training |
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm |
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/ |
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ profile:      http://plus.google.com/+GKenHolman-Crane/about |
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal |


--- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com

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.