xsl-list
[Top] [All Lists]

RE: sort and grouping on 2 different attributes

2003-01-21 07:58:40
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[(_at_)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>&#10;</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>&#10;</xsl:text>
 </xsl:template>
 
 <xsl:template match="rec" mode="table">
  <!-- replace this for yours -->
  <!-- here I'm just displaying the matched nodes -->
  <xsl:text> &lt;rec</xsl:text>
  <xsl:apply-templates select="@*" mode="showattribs"/>
  <xsl:text>&gt;</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&lt;/rec&gt;&#10;</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(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Joeri Belis
Sent: terça-feira, 21 de Janeiro de 2003 12:42
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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



<Prev in Thread] Current Thread [Next in Thread>