Subject: Re: [xsl] Sorting complex objects From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sun, 08 Mar 2009 09:20:45 -0400 |
I need to transform a file of random-order complex items into an ordered file. I tried using 'for-each-group' nested, but failed to get the results I wanted
As I said, in the input file, the items are random and an item may occur more than once and have <SomeData> elements with different contents - both <SomeData> elements need to be preserved in the output file. The <Items> are ordered by <Heading>, then sequentially by the three <SubDivX> elements, and finally by <SomeData>. Everything sorts as strings.
I have been thinking about something like:
('ab','xy') ('abcd',ef')
('abcd','ef') <!--key "abcdef"--> ('ab','xy') <!--key "abxy"-->
group-by="concat(Heading,'', SubDiv1,'', SubDiv2,'',SubDiv3)">
group-by="concat(Heading,'
', SubDiv1,'
', SubDiv2,'
',SubDiv3)">
t:\ftemp>type mark.xml <List> <Item> <Heading>Prague</Heading> <SubDiv1>Avenues</SubDiv1> <SubDiv2>Crosswalks</SubDiv2> <SomeData>12</SomeData> </Item> <Item > <Heading>Prague</Heading> <SomeData>1</SomeData> <!-- Needs to me consolidated--> </Item> <Item> <Heading>Prague</Heading> <SubDiv1>Avenues</SubDiv1> <SomeData></SomeData> </Item> <Item> <Heading>Prague</Heading> <SubDiv1>Avenue</SubDiv1> <SubDiv2>Crosswalks</SubDiv2> <SubDiv3>Dangerous</SubDiv3> <SomeData></SomeData> </Item> <Item> <Heading>Bonn</Heading> <SubDiv1>Avenue</SubDiv1> <SubDiv2>Crosswalks</SubDiv2> <SubDiv3>Dangerous</SubDiv3> <SomeData></SomeData> </Item> <Item> <Heading>Prague</Heading> <SubDiv1>Streets</SubDiv1> <SomeData></SomeData> </Item> <Item> <Heading>Washington</Heading> <SomeData></SomeData> </Item> <Item > <Heading>Prague</Heading> <SomeData>2</SomeData><!-- Needs to me consolidated--> </Item> </List>
t:\ftemp>call xslt2 mark.xml mark.xsl <?xml version="1.0" encoding="UTF-8"?> <Item> <Heading>Bonn</Heading> <SubDiv1>Avenue</SubDiv1> <SubDiv2>Crosswalks</SubDiv2> <SubDiv3>Dangerous</SubDiv3> <SomeData/> </Item> <Item> <Heading>Prague</Heading> <SomeData>1</SomeData> <SomeData>2</SomeData> </Item> <Item> <Heading>Prague</Heading> <SubDiv1>Avenue</SubDiv1> <SubDiv2>Crosswalks</SubDiv2> <SubDiv3>Dangerous</SubDiv3> <SomeData/> </Item> <Item> <Heading>Prague</Heading> <SubDiv1>Avenues</SubDiv1> <SomeData/> </Item> <Item> <Heading>Prague</Heading> <SubDiv1>Avenues</SubDiv1> <SubDiv2>Crosswalks</SubDiv2> <SomeData>12</SomeData> </Item> <Item> <Heading>Prague</Heading> <SubDiv1>Streets</SubDiv1> <SomeData/> </Item> <Item> <Heading>Washington</Heading> <SomeData/> </Item> t:\ftemp>type mark.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="List"> <xsl:for-each-group select="Item" group-by="concat(Heading,'
', SubDiv1,'
', SubDiv2,'
',SubDiv3)"> <xsl:sort select="current-grouping-key()"/> <!--reconstitute the Item--> <Item> <xsl:copy-of select="Heading,SubDiv1,SubDiv2,SubDiv3"/> <!--consolidating the <SomeData> elements--> <xsl:for-each select="current-group()"> <xsl:sort select="SomeData"/> <xsl:copy-of select="SomeData"/> </xsl:for-each> </Item> </xsl:for-each-group> </xsl:template>
-- XQuery/XSLT training in Prague, CZ 2009-03 http://www.xmlprague.cz XQuery/XSLT/XSL-FO training in Los Angeles/Anaheim - 2009-06-01/10 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Sorting complex objects, Mark Wilson | Thread | [xsl] Processing related complex ob, Mark Wilson |
[xsl] Sorting complex objects, Mark Wilson | Date | [xsl] Processing related complex ob, Mark Wilson |
Month |