[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Grouping within sub elements


Subject: Re: [xsl] Grouping within sub elements
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 29 Jan 2005 08:38:33 -0500

At 2005-01-29 07:45 +0000, Steve W wrote:
I have been trying to use the Muenchian method to group sub-elements within
a particular element.
...
Can I apply the same sort of logic within an item group,  to get some output
something like :
...
I can't help thinking I'm missing something obvious but can't see it !

One isn't obliged to use the Muenchian group for sub-document-scope grouping, which is the case also when doing multiple-level grouping. I sometimes find variable-based grouping to be more understandable because then I'm dealing with all those constructs that need to be grouped in a single variable, rather than having to deal with document-wide scope. The Muenchian method always has document-wide scope, but you can assign any subset of a document to a variable.


This variable-based method is also very useful grouping across multiple documents, whereas the Muenchian Method's document-wide scope is not viable because you are only ever dealing with a single document.

The idea is to walk through a variable of constructs (however you've decided to create the variable) finding the first of a set, much like the Muenchian Method.

I hope this helps.

............................ Ken

T:\ftemp>type steve.xml
<BODY>
    <ITEM>
        <ID>AA</ID>
        <SUBITEM>1</SUBITEM>
        <SUBITEM>2</SUBITEM>
        <SUBITEM>1</SUBITEM>
        <SUBITEM>3</SUBITEM>
        <SUBITEM>2</SUBITEM>
    </ITEM>
    <ITEM>
        <ID>BB</ID>
        <SUBITEM>1</SUBITEM>
        <SUBITEM>4</SUBITEM>
        <SUBITEM>1</SUBITEM>
        <SUBITEM>4</SUBITEM>
        <SUBITEM>4</SUBITEM>
    </ITEM>
    <ITEM>
        <ID>CC</ID>
        <SUBITEM>2</SUBITEM>
        <SUBITEM>2</SUBITEM>
        <SUBITEM>3</SUBITEM>
        <SUBITEM>3</SUBITEM>
        <SUBITEM>2</SUBITEM>
    </ITEM>
</BODY>

T:\ftemp>saxon steve.xml steve.xsl

Item AA
Subitems 1, 2, 3
Item BB
Subitems 1, 4
Item CC
Subitems 2, 3
T:\ftemp>type steve.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="/BODY/ITEM">
Item <xsl:value-of select="ID"/><xsl:text>
Subitems </xsl:text>
    <xsl:variable name="subs" select="SUBITEM"/>
    <xsl:for-each select="$subs">
      <xsl:if test="generate-id(.)=
                    generate-id($subs[.=current()])">
        <xsl:if test="position()>1">, </xsl:if>
        <xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>


-- World-wide on-site corporate, govt. & user group XML/XSL training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal


Current Thread