Subject: RE: Complicated grouping question
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 7 Sep 2006 16:26:49 +0100
|
INPUT
<pop>
<feature><f>featureid1</f><p>pubid1</p></feature>
<feature><f>featureid1</f><p>pubid2</p></feature>
<feature><f>featureid2</f><p>pubid1</p></feature>
<feature><f>featureid3</f><p>pubid1</p></feature>
<feature><f>featureid3</f><p>pubid2</p></feature>
</pop>
STYLESHEET
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="pop">
<xsl:variable name="v">
<xsl:for-each-group select="feature" group-by="f">
<feature>
<f><xsl:value-of select="current-grouping-key()" /></f>
<p>
<xsl:value-of separator=", ">
<xsl:perform-sort select="current-group()/p">
<xsl:sort select="."/>
</xsl:perform-sort>
</xsl:value-of>
</p>
</feature>
</xsl:for-each-group>
</xsl:variable>
<xsl:for-each-group select="$v/feature" group-by="p">
<feature>
<f><xsl:value-of select="current-group()/f" separator=", "/></f>
<p><xsl:value-of select="current-grouping-key()" /></p>
</feature>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
OUTPUT
<?xml version="1.0" encoding="UTF-8"?>
<feature>
<f>featureid1, featureid3</f>
<p>pubid1, pubid2</p>
</feature>
<feature>
<f>featureid2</f>
<p>pubid1</p>
</feature>
Michael Kay
http://www.saxonica.com/
> -----Original Message-----
> From: Josh Goodman [mailto:jogoodma@xxxxxxxxxxx]
> Sent: 07 September 2006 16:04
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Complicated grouping question
>
> Hi all,
>
> I have a XSL grouping problem that I am having difficulty
> with and for which I couldn't find a previous post about in
> the archives. I also noticed that there hadn't been a
> grouping question in a day or so I thought I would do my part
> to help out :-).
>
> I'm using XSLT 2.0 as implemented by Saxon 8.8 (congrats to
> Michael on this latest version). The XML I'm working with
> looks like this:
>
> <feature>
> <id>BACR48K23</id>
> <pub>
> <id>0105495</id>
> </pub>
> </feature>
> ...
>
> This can sometimes be repeated over again with a new
> feature/id and the same pub/id, a new pub/id and the same
> feature/id, or both ids being completely new.
> Each feature chunk is adjacent to the others but they are in
> random order. A completely flat view of this would look like
> this for example
>
> featureid1 pubid1
> featureid1 pubid2
> featureid2 pubid1
> featureid3 pubid1
> featureid3 pubid2
>
> The grouping I would like to do is first group by featureid
> to produce this list.
>
> featureid1 pubid1, pubid2
> featureid2 pubid1
> featureid3 pubid1, pubid2
>
> This is no problem using something along the lines of
>
> <xsl:for-each-group select="feature" group-by="id">
> <xsl:value-of select="current-grouping-key()" />
> <xsl:value-of select="current-group()/pub/id"
> separator=", " /> </xsl:for-each-group>
>
> The problem comes in to play because I need to do another
> round of grouping.
> This time on items from the second list grouped by those with
> a common set of pubid's. Doing this on the above list would
> produce this:
>
> featureid1, featureid3 pubid1, pubid2
> featureid2 pubid1
>
> I've not been able to wrap my head around the best way to
> achieve this. Anyone have suggestions?
>
> Thanks,
> Josh Goodman
|