Re: [xsl] two level grouping problem

Subject: Re: [xsl] two level grouping problem
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 25 Oct 2007 17:41:39 -0400
At 2007-10-25 16:19 -0400, Terry Ofner wrote:
I am trying to group down to two levels. Here is a sample of the
original xml (blank lines added for clarity):
...
Here is what I am looking for. Notice that the g_codes have been
grouped and the AK objectives have been groups together separated by
a pipe character.
...
My current XSLT:
...
<xsl:key name="gcode_key" match="standard" use="g_code"/>
<xsl:key name="state_by_gcode" match="standard" use="concat(., '+', / standard/state-objective/@state)" />

I think I would have used variables, but you were on the right start ... but you missed in the use= attribute.


Apparently my first group key is working. I am not sure what the
second one is doing, if anything. Can anyone show me where I am going
wrong?

There were a number of places where you lost track if you were at a <standard> element or a <g_code> element. Usually you used "." when you shouldn't have and you should have been addressing g_code.


Rather than enumerate all the changes, the working code is below.

I hope this helps.

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


T:\ftemp>type terry.xml <state> <standard><g_code>G4U1S01</g_code> <state-objective state="AL">R.3.6. Blah blah blah</state-objective></standard>

<standard><g_code>G4U1S01</g_code>
<state-objective state="AK">[4] 2.1.4. Blah blah blah</state-objective></standard>


<standard><g_code>G4U1S01</g_code>
<state-objective state="AK">[4] 2.1.2. Blah blah blah</state-objective></standard>


<standard><g_code>G4U1S01</g_code>
<state-objective state="AK">[4] 2.1.1. Blah blah blah</state-objective></standard>


<standard><g_code>G4U1S02</g_code>
<state-objective state="AL">R.3.7. Blah blah blah</state-objective></standard>
. . . .
</state>

T:\ftemp>type terry.xsl
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="gcode_key" match="standard" use="g_code"/>
<xsl:key name="state_by_gcode" match="standard"
         use="concat(g_code, '+', state-objective/@state)" />


<xsl:template match="/"> <xsl:for-each select="/state/standard[generate-id(.)= generate-id(key ('gcode_key' , g_code))]/g_code"> <xsl:sort/>

   <xsl:text>&#10;</xsl:text>
   <standard>
     <g_code><xsl:value-of select="."/></g_code>
     <xsl:text>&#10;</xsl:text>

    <xsl:for-each select="key('gcode_key', .)
                          [generate-id() =
                          generate-id(key('state_by_gcode',
                          concat(g_code, '+', state-objective/@state))[1])]">
      <xsl:sort select="state-objective/@state"/>
      <state-objective state="{state-objective/@state}">
        <xsl:for-each select="key('state_by_gcode',
                              concat(g_code, '+', state-objective/@state))">
          <xsl:if test="position()>1"> | </xsl:if>
          <xsl:value-of select="state-objective"/>
        </xsl:for-each>
      </state-objective>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </standard>
 </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>call xslt terry.xml terry.xsl terry.out

T:\ftemp>echo on

T:\ftemp>type terry.out
<?xml version="1.0" encoding="UTF-8"?>

<standard>
   <g_code>G4U1S01</g_code>

<state-objective state="AK">[4] 2.1.4. Blah blah blah | [4] 2.1.2. Blah blah blah | [4] 2.1.1. Blah blah blah</state-objective>

<state-objective state="AL">R.3.6. Blah blah blah</state-objective>

</standard>

<standard>
   <g_code>G4U1S02</g_code>

<state-objective state="AL">R.3.7. Blah blah blah</state-objective>

</standard>
T:\ftemp>rem Done!



--
Comprehensive in-depth XSLT2/XSL-FO1.1 classes: Austin TX,Jan-2008
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and 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 Cancer Awareness Jul'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread