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

Grouping while sorting

Subject: Grouping while sorting
From: "Ragulf Pickaxe" <jawxml@xxxxxxxxxxx>
Date: Sun, 21 Nov 2004 17:59:27 +0000
xsl while
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

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.