[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: sort and grouping on 2 different attributes

Subject: RE: sort and grouping on 2 different attributes
From: Américo Albuquerque <aalbuquerque@xxxxxxxxxxxxxxxx>
Date: Tue, 21 Jan 2003 14:58:40 -0000
sorting by 2 attributes
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>&#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@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:  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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.