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

RE: "Group by" question

Subject: RE: "Group by" question
From: "Andreas L. Delmelle" <a_l.delmelle@xxxxxxxxxx>
Date: Mon, 22 Mar 2004 19:18:41 +0100
xsl group by
> -----Original Message-----
> From: Hilder Sousa [mailto:Helder.Sousa@xxxxxx]
>
> Hi :)
>
> I would like to kwonw how can I do a "group by".. I explain:
> Imagine i have the next xml:
>
<snip />
> I need a xsl to "group by" the address. Like this:
>

Hi,

The easiest (and in case you have *a lot* of these nodes, also the most
efficient) way to achieve this, is using keys:

Put a declaration like this on the top-level of your stylesheet:
<xsl:key name="doc-by-addr" match="doc"
         use="@address" />

Then further on, you can use for example

<xsl:apply-templates select="key('doc-by-addr', 'av. A1')" />

Which will apply-templates to all document nodes with an address attribute
equal to 'av. A1'.

If you add a global variable, like

<xsl:variable name="vaddr"
              select="doc[not(@address = preceding::doc/@address)]" />

and apply-templates to it

<xsl:template match="docs">
  <xsl:copy>
    <xsl:apply-templates select="$vaddr" mode="group"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="doc" mode="group">
  <address val="{@address}">
    <xsl:apply-templates select="key('doc-by-addr',@address)" />
  </address>
</xsl:template>

<xsl:template match="doc">
  <xsl:copy>
    <xsl:copy-of select="@id" />
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

Together with a generic copy-template for the pages/page nodes, this
produces the wanted result.


Hope this helps!

Cheers,

Andreas

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.