Subject: Re: XSLT 1 and grouping numbers by if the following number is current number + 1 and so on
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Mon, 24 Mar 2014 10:42:41 -0700
|
Actually, I first thought about using the "group-adjacent" attribute:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:for-each-group select="a"
group-adjacent="number(.) eq
number(following-sibling::a[1]) -1">
<xsl:if
test="not(position() eq last)
or
not(number(current-group()[last()])
eq
number(current-group()[last() -1]/preceding-sibling::a[1])+1)">
<group>
<xsl:sequence select=".[not(number(.) eq
number(preceding-sibling::a[1]) +1)]"/>
<xsl:sequence select="current-group()[position() gt 1]"/>
<xsl:sequence select="current-group()[last()]
/following-sibling::a[1]
[number(current-group()[last()]) + 1 eq number(.)]"/>
</group>
</xsl:if>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided source XML document:
<t>
<a>1</a>
<a>3</a>
<a>4</a>
<a>5</a>
<a>7</a>
<a>9</a>
<a>10</a>
</t>
the wanted, correct result is produced:
<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>
<group/>
Cheers,
Dimitre
On Mon, Mar 24, 2014 at 8:05 AM, David Carlisle <davidc@xxxxxxxxx> wrote:
> On 24/03/2014 14:44, G. Ken Holman wrote:
>>
>> You can use keys as illustrated below.
>
>
> yeh that completes the triple: for-each-group (DC), sibling recursion
> (MK) and Muenchian grouping (KH). For bonus points we should get DN
> to post a divide and conquer version:-)
>
--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
|