Subject: RE: [xsl] sort and grouping on 2 different attributes From: Américo Albuquerque <aalbuquerque@xxxxxxxxxxxxxxxx> Date: Tue, 21 Jan 2003 14:58:40 -0000 |
Hi Joeri. To group on 2 attributes you'll have to use concat like this: <xsl:key name="group" match="node" use="concat(@attr1,' ',@attr2)"/> When applying the key you'll do the same: select="key('group',concat(@attr1,' ',@attr2)" Related to your problem, this templates do what you ask, you'll have to change them to your needs. <xsl:key name="recs" match="rec" use="@A"/> <xsl:key name="recs" match="rec" use="concat(@A,' ',@B)"/> <xsl:template match="root"> <xsl:apply-templates select="rec[@A='0'][generate-id()=generate-id(key('recs',concat(@A,' ',@B)))]|rec[not(@A='0')][generate-id()=generate-id(key('recs',@A))]"> <xsl:sort select="@A" data-type="number"/> <xsl:sort select="@B" data-type="number"/> </xsl:apply-templates> </xsl:template> <xsl:template match="rec"> <xsl:text>titulo-</xsl:text> <xsl:value-of select="position()"/> <xsl:text> </xsl:text><!-- or whatever you like as title --> <xsl:choose> <!-- choose whet key to apply --> <xsl:when test="@A=0"> <!-- apply to A='0', so group also by @B --> <xsl:apply-templates select="key('recs',concat(@A,' ',@B))" mode="table"> <xsl:sort select="@A" data-type="number"/> <xsl:sort select="@B" data-type="number"/> </xsl:apply-templates> </xsl:when> <xsl:otherwise> <!-- apply to A<>'0', group just by @A --> <xsl:apply-templates select="key('recs',@A)" mode="table"> <xsl:sort select="@A" data-type="number"/> <xsl:sort select="@B" data-type="number"/> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> <xsl:text> </xsl:text> </xsl:template> <xsl:template match="rec" mode="table"> <!-- replace this for yours --> <!-- here I'm just displaying the matched nodes --> <xsl:text> <rec</xsl:text> <xsl:apply-templates select="@*" mode="showattribs"/> <xsl:text>></xsl:text> <xsl:value-of select="."/> <xsl:text></rec> </xsl:text> </xsl:template> <xsl:template match="@*" mode="showattribs"> <xsl:text> </xsl:text> <xsl:value-of select="name()"/> <xsl:text>="</xsl:text> <xsl:value-of select="."/> <xsl:text>"</xsl:text> </xsl:template> -----Original Message----- From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Joeri Belis Sent: terça-feira, 21 de Janeiro de 2003 12:42 To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: [xsl] sort and grouping on 2 different attributes I have the following simplified xml <root> <rec A="0" B="1">1</rec> <rec A="0" B="3">2</rec> <rec A="0" B="3">3</rec> <rec A="0" B="3">4</rec> <rec A="1" B="1">5</rec> <rec A="1" B="1">6</rec> <rec A="2" B="1">7</rec> <rec A="3" B="1">8</rec> </root> each node has 2 attributes ( A and B ). When A="0" then B should be used for grouping When A != "0" then A should be used for grouping. Sorting should be done on A and B ( my simplified example xml is already sorted on A and B) the result should be than when a group changed i display subtitle title-1 <rec A="0" B="1">1</rec> title-2 <rec A="0" B="3">2</rec> <rec A="0" B="3">3</rec> <rec A="0" B="3">4</rec> title-3 <rec A="1" B="1">5</rec> <rec A="1" B="1">6</rec> title-4 <rec A="2" B="1">7</rec> title-5 <rec A="3" B="1">8</rec> I find i hard to find a simple but waterproof solution for grouping on the 2 different attributes. Joeri XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] sort and grouping on 2 differ, Joeri Belis | Thread | [xsl] Correctly sorting ID's, Niki Dinsey - CSW |
[xsl] sort and grouping on 2 differ, Joeri Belis | Date | [xsl] Correctly sorting ID's, Niki Dinsey - CSW |
Month |