This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: Multiple grouping with substrings
- From: Ben Fry <Ben_Fry at amt-sybex dot com>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Wed, 14 Aug 2002 09:04:28 +0100
- Subject: RE: [xsl] Multiple grouping with substrings
- Reply-to: xsl-list at lists dot mulberrytech dot com
Thanks Jeni,
That was spot on as usual.
Ben
-----Original Message-----
From: Jeni Tennison [mailto:jeni@jenitennison.com]
Sent: 13 August 2002 19:46
To: Ben Fry
Cc: XSL Mulberry
Subject: Re: [xsl] Multiple grouping with substrings
Hi Ben,
> I have a grouping problem I cannot work out. I have done a couple of
> stylesheets with keys but this one has me befuddled.
>
> I am having trouble declaring and using a key like this; (I am new
> to keys so go easy here)
>
> <xsl:key name="xprogs" match="doc/prog[contains(.,'msx']"
> use="substring(.,4,1)"/>
>
> My algorithm is
> a). Group by 3rd character of prog node ie 'x' in msx123 followed
> by 'y' in msy123
>
> b). For each unique 4th character of prog node list prog nodes 4
(or
> ideally custom value)
> per line separated by a single space (produce new line when
a
> different 4th character
> encountered).
OK, it sounds as though you want to group by the third *and* fourth
characters. You want to group *all* the prog elements rather than just
those that contain 'msx', so your match attribute should match all
prog elements. And if you want the third and fourth characters, then
you need substring(., 3, 2):
<xsl:key name="progs" match="prog" use="substring(., 3, 2)" />
Then you have to think about selecting all those prog elements that
have a unique letter-number combination: if they're the first prog
with that particular letter-number combination:
<xsl:template match="doc">
<xsl:for-each select="prog[generate-id() =
generate-id(key('progs',
substring(., 3, 2))[1])]">
<xsl:variable name="progs"
select="key('progs', substring(., 3, 2))" />
...
</xsl:for-each>
</xsl:template>
Once you've got that set of $progs together, you can group them by
their position. Say you set a global parameter to the number you want
in each group:
<xsl:param name="nprogs" select="4" />
then you can loop through the $progs and use position() mod $nprogs to
work out whether you need to add a newline or a space before the value
of the particular prog:
<xsl:template match="doc">
<xsl:for-each select="prog[generate-id() =
generate-id(key('progs',
substring(., 3, 2))[1])]">
<xsl:variable name="progs"
select="key('progs', substring(., 3, 2))" />
<xsl:for-each select="$progs">
<xsl:choose>
<xsl:when test="position() mod $nprogs = 1">
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="." />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Cheers,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
=====================================================================
DISCLAIMER
1. The information contained in this E-mail is confidential.
It is intended only for the stated addressee(s) and access
to it by any other person is unauthorised.
If you are not an addressee, you must not disclose, copy,
circulate or in any other way use or rely on the information
contained in this E-mail. Such unauthorised use may be unlawful.
If you have received this E-mail in error, please inform us
immediately and delete it and all copies from your system.
2. The views expressed in this E-mail are those of the author,
and do not represent the views of AMT-Sybex Group Ltd., its
associates or subsidiaries, unless otherwise expressly indicated.
In the avoidance of doubt, the insertion of the name of AMT-Sybex
Group Ltd., its associate or subsidiary under the name of the sender
may constitute an express indication that the views stated in the Mail
are those of the named company.
=====================================================================
For more information on the AMT Sybex group visit: http://www.amt-sybex.com
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list