[xsl] Grouping while sorting

Subject: [xsl] Grouping while sorting
From: "Ragulf Pickaxe" <jawxml@xxxxxxxxxxx>
Date: Sun, 21 Nov 2004 17:59:27 +0000
Hello all,

I seem to be caught on trying to sort something while at the same time grouping them.
In the first input (see below), I have no problems, but the second gives me problems in the current solution.


The problem seems that I get the nodes in sorted order, but the test on the previous sibling goes to the document order.
How do I make the test so that I can group these elements, and still retain the sorted order?


Thank you very much in advance!
Ragulf Pickaxe

<?xml version="1.0">
<Root> <!-- Input 1 -->
 <Elem GroupID="A" Order="1">A1</Elem>
 <Elem GroupID="A" Order="2">A2</Elem>
 <Elem GroupID="B" Order="4">B4</Elem>
 <Elem GroupID="A" Order="5">A5</Elem>
 <Elem GroupID="C" Order="1">C1</Elem>
 <Elem GroupID="B" Order="7">A7</Elem>
</Root>


<?xml version="1.0"> <Root> <!-- Input 2 --> <Elem GroupID="A" Order="7">A7</Elem> <Elem GroupID="A" Order="5">A5</Elem> <Elem GroupID="B" Order="4">B4</Elem> <Elem GroupID="A" Order="2">A2</Elem> <Elem GroupID="C" Order="1">C1</Elem> <Elem GroupID="B" Order="1">A1</Elem> </Root>



<?xml version="1.0">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="/">
 <Output>
   <xsl:apply-templates select="Root"/>
 </Output>
</xsl:template>

<xsl:template match="Root">
 <xsl:apply-templates select="Elem">
   <xsl:sort select="@GroupID">
   <xsl:sort select="@Order" data-type="number">
 </xsl:apply-templates>
</xsl:template>

<xsl:template match="Elem">
 <xsl:if test="not(preceding-sibling::Elem/@GroupID=current()/@GroupID)">
   <Group><xsl:value-of select="@GroupID"/></Group>
 </xsl:if>
 <xsl:value-of select="."/> -
</xsl:template>
</xsl:stylesheet>


Output from input 1: <Output> <Group>A</Group>A1 - A2 - A5 - A7 - <Group>B</Group>B4 - <Group>C</Group>C1 - </Output>

Output from input 2
<Output>
 A1 - A2 - A5 - <Group>A</Group>A7 -
 <Group>B</Group>B4 -
 <Group>C</Group>C1 -
</Output>

(Summary: I would very much like to get second output to be the same as the first output - I hope that this is possible).

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search! http://search.msn.com/


Current Thread