xsl-list
[Top] [All Lists]

Re: [xsl] XSLT 1.0: Problem grouping disparate unordered data

2006-03-15 19:02:10
At 2006-03-15 18:00 +0000, Nick Fitzsimons wrote:
Firstly, I'm using XSLT 1.0.

First in my response is "thank you for supplying test data".

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 see this only as a sorting issue, since you are returning the result set in a single section.

I'm almost certain some
straightforward Muenchian grouping will suffice

If I have understood your requirement, it isn't necessary to use grouping.

I hope the code below helps.

. . . . . . . . . . Ken

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">

<xsl:output indent="yes"/>

<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[(_at_)priority>2]">
      <xsl:sort select="document('')/*/nick:type-groupings/
                        group[(_at_)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()&lt;=3">
        <xsl:element name="{(_at_)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(_at_)CraneSoftwrights(_dot_)com
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--