Subject: Re: [xsl] XSLT 1.0: Problem grouping disparate unordered data From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Wed, 15 Mar 2006 21:01:35 -0500 |
Firstly, I'm using XSLT 1.0.
The rules for grouping and sorting are fairly simple in the basic case:
Show Gorillas of priority greater than 2, sorted by priority, then by date descending; Show Chimpanzees of priority greater than 2, sorted by priority, then by date descending; Show Orangutans of priority greater than 2, sorted by priority, then by date descending; Show Bonobos of priority greater than 2, sorted by priority, then by date descending; ... However, I now have the further requirement that I return a single <section> containing only the first three items (or fewer if less than three match the "priority greater than 2" criterion). ... how can I achieve the necessary grouping and sorting?
I'm almost certain some straightforward Muenchian grouping will suffice
T:\ftemp>type nick.xsl <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nick="urn:x-nick" exclude-result-prefixes="nick" version="1.0">
<nick:type-groupings> <group type="Gorilla" order="1"/> <group type="Chimpanzee" order="2"/> <group type="Orangutan" order="3"/> <group type="Bonobo" order="4"/> </nick:type-groupings>
<xsl:template match="/"> <xsl:variable name="apes" select="/apes/ape"/> <section> <xsl:for-each select="$apes[@priority>2]"> <xsl:sort select="document('')/*/nick:type-groupings/ group[@type=current()/@type]/@order" data-type="number"/> <xsl:sort select="@priority" order="descending" data-type="number"/> <xsl:sort select="substring(@date,7,4)" order="descending"/> <xsl:sort select="substring(@date,4,2)" order="descending"/> <xsl:sort select="substring(@date,1,2)" order="descending"/> <xsl:if test="position()<=3"> <xsl:element name="{@type}"> <xsl:copy-of select="@priority | @date"/> </xsl:element> </xsl:if> </xsl:for-each> </section> </xsl:template>
</xsl:stylesheet> T:\ftemp>type nick1.xml <apes> <ape priority="5" date="25-01-2006" type="Chimpanzee" /> <ape priority="1" date="26-01-2006" type="Gorilla" /> <ape priority="2" date="29-01-2006" type="Chimpanzee" /> <ape priority="1" date="22-01-2006" type="Orangutan" /> <ape priority="3" date="22-01-2006" type="Bonobo" /> <ape priority="1" date="25-01-2006" type="Bonobo" /> <ape priority="4" date="24-01-2006" type="Gorilla" /> <ape priority="5" date="22-01-2006" type="Bonobo" /> <ape priority="4" date="26-01-2006" type="Chimpanzee" /> <ape priority="4" date="25-01-2006" type="Gorilla" /> <ape priority="2" date="25-01-2006" type="Bonobo" /> <ape priority="3" date="25-01-2006" type="Orangutan" /> <ape priority="1" date="25-01-2006" type="Bonobo" /> <ape priority="3" date="27-01-2006" type="Gorilla" /> <ape priority="1" date="25-01-2006" type="Chimpanzee" /> <ape priority="1" date="25-01-2006" type="Orangutan" /> </apes>
T:\ftemp>xslt nick1.xml nick.xsl con <?xml version="1.0" encoding="utf-8"?> <section> <Gorilla priority="4" date="25-01-2006"/> <Gorilla priority="4" date="24-01-2006"/> <Gorilla priority="3" date="27-01-2006"/> </section> T:\ftemp>type nick2.xml <apes> <ape priority="5" date="25-01-2006" type="Chimpanzee" /> <ape priority="1" date="26-01-2006" type="Gorilla" /> <ape priority="2" date="29-01-2006" type="Chimpanzee" /> <ape priority="1" date="22-01-2006" type="Orangutan" /> <ape priority="3" date="22-01-2006" type="Bonobo" /> <ape priority="1" date="25-01-2006" type="Bonobo" /> <ape priority="5" date="22-01-2006" type="Bonobo" /> <ape priority="2" date="25-01-2006" type="Bonobo" /> <ape priority="3" date="25-01-2006" type="Orangutan" /> <ape priority="1" date="25-01-2006" type="Bonobo" /> <ape priority="3" date="27-01-2006" type="Gorilla" /> <ape priority="1" date="25-01-2006" type="Chimpanzee" /> <ape priority="1" date="25-01-2006" type="Orangutan" /> </apes>
T:\ftemp>xslt nick2.xml nick.xsl con <?xml version="1.0" encoding="utf-8"?> <section> <Gorilla priority="3" date="27-01-2006"/> <Chimpanzee priority="5" date="25-01-2006"/> <Orangutan priority="3" date="25-01-2006"/> </section> T:\ftemp>
-- World-wide on-site corporate, govt. & user group XML/XSL training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Aug'05 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XSLT 1.0: Problem groupin, Wendell Piez | Thread | Re: [xsl] XSLT 1.0: Problem groupin, Nick Fitzsimons |
Re: [xsl] Displaying one section of, Wendell Piez | Date | [xsl] Keys Context Management, Karl Stubsjoen |
Month |