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

Re: using xsl:for-each-group, grouping by child nodes?

Subject: Re: using xsl:for-each-group, grouping by child nodes?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 04 Oct 2008 17:48:46 -0400
Re:  using xsl:for-each-group
At 2008-10-05 01:06 +1000, Eric Scheid wrote:
Sadly, it seems the tool I'm using (Filemaker Pro) doesn't like it, throwing
an error of "Unknown XSL element: for-each-group". I'm guessing it doesn't
support XSLT 2.0 functions yet, which is a bummer.

Then use XSLT 1.0 to solve the problem ... no reason to give up.


I hope the below helps.

. . . . . . . Ken


t:\ftemp>type eric.xml <RESULTSET FOUND="6"> <ROW MODID="3" RECORDID="1"> <COL><DATA>joe</DATA></COL> <COL><DATA>2</DATA></COL> <COL><DATA>pitcher</DATA></COL> <COL><DATA>ny</DATA></COL> <COL><DATA>mets</DATA></COL> </ROW> <ROW MODID="3" RECORDID="2"> <COL><DATA>mark</DATA></COL> <COL><DATA>11</DATA></COL> <COL><DATA>outfielder</DATA></COL> <COL><DATA>ny</DATA></COL> <COL><DATA>mets</DATA></COL> </ROW> <ROW MODID="3" RECORDID="3"> <COL><DATA>jane</DATA></COL> <COL><DATA>13</DATA></COL> <COL><DATA>outfielder</DATA></COL> <COL><DATA>ny</DATA></COL> <COL><DATA>cubs</DATA></COL> </ROW> <ROW MODID="3" RECORDID="4"> <COL><DATA>mike</DATA></COL> <COL><DATA>7</DATA></COL> <COL><DATA>outfielder</DATA></COL> <COL><DATA>ny</DATA></COL> <COL><DATA>mets</DATA></COL> </ROW> </RESULTSET>

t:\ftemp>type eric.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <!--group first by team; using a key of two parts with an unlikely
      character in between to avoid any ambiguous concatenations-->
  <xsl:variable name="players" select="/*/ROW"/>
  <xsl:for-each select="$players">
    <xsl:if test="generate-id(.)=
                  generate-id($players[concat(COL[4]/DATA,'&#xd;',COL[5]/DATA)=
             concat(current()/COL[4]/DATA,'&#xd;',current()/COL[5]/DATA)][1])">
      <!--found the first player for a given team-->
      <xsl:if test="generate-id(.)!=generate-id($players[1])">
        <xsl:text>&#xa;</xsl:text>
      </xsl:if>
      <xsl:text>&#xa;    Team: </xsl:text>
      <xsl:value-of select="concat(COL[5]/DATA,', ',COL[4]/DATA)"/>
      <xsl:variable name="team"
                    select="$players[concat(COL[4]/DATA,'&#xd;',COL[5]/DATA)=
             concat(current()/COL[4]/DATA,'&#xd;',current()/COL[5]/DATA)]"/>
      <!--now group by position; reflecting plural where applicable-->
      <xsl:for-each select="$team">
        <xsl:if test="generate-id(.)=
          generate-id($team[COL[3]/DATA=current()/COL[3]/DATA][1])">
          <xsl:text>&#xa;       </xsl:text>
          <xsl:value-of select="COL[3]/DATA"/>
          <xsl:variable name="position"
                        select="$team[COL[3]/DATA=current()/COL[3]/DATA]"/>
          <xsl:if test="count($position)>1">s</xsl:if>:<xsl:text/>
          <!--show all in the position-->
          <xsl:for-each select="$position">
            <xsl:text>&#xa;           </xsl:text>
            <xsl:value-of select="concat( COL[1]/DATA,', ',COL[2]/DATA )"/>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>call xslt eric.xml eric.xsl eric.out

t:\ftemp>type eric.out

    Team: mets, ny
       pitcher:
           joe, 2
       outfielders:
           mark, 11
           mike, 7

    Team: cubs, ny
       outfielder:
           jane, 13
t:\ftemp>rem Done!



--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

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.