xsl-list
[Top] [All Lists]

Re: [xsl] XSLT 1 and grouping numbers by if the following number is current number + 1 and so on

2014-03-24 09:44:52
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(_at_)CraneSoftwrights(_dot_)com 
|
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


--~------------------------------------------------------------------
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>
--~--