xsl-list
[Top] [All Lists]

Re: [xsl] Simple grouping with XSL2

2009-07-31 01:30:10
Thanks heaps Ken, I was really thinking of it the incorrect way.
This taught me a lot. Thanks!

Andy.

On Fri, Jul 31, 2009 at 3:05 PM, G. Ken
Holman<gkholman(_at_)cranesoftwrights(_dot_)com> wrote:
At 2009-07-31 13:14 +1000, Andy Kohn wrote:

I'm trying to do a simple grouping and then post the siblings but I'm
a little bit confused.

I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Entries>
  <entry>
     <First>First Group</First>
     <Second>sample data I</Second>
  </entry>
  <entry>
      <First>Second Group</First>
     <Second>sample data II</Second>
  </entry>
   <entry>
       <First>First Group</First>
       <Second>sample data III</Second>
   </entry>
</Entries>

I'm trying to group the data using the <First> tag and then show all
the <Second> tags data.
Example of the result:

<Entries>
  <entryTransformation>
     <data>First Group</data>
     <secondaryData>sample data I</secondaryData>
     <secondaryData>sample data III</secondaryData>
  </entryTransformation>
  <entryTransformation>
     <data>Second Group</data>
     <secondaryData>sample data II</secondaryData>
  </entryTransformation>
</Entries>


I'm using
<xsl:for-each-group select="/Entries/entryTransformation/data"
group-by=".">
To make the grouping, but then I don't know how to iterate the
<Second> elements.

You've got your perspective backwards by trying to address nodes in the
result tree, when you must always address nodes in the source tree.

I hope the example below helps.

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

t:\ftemp>type andy.xml
<?xml version="1.0" encoding="UTF-8"?>
<Entries>
  <entry>
     <First>First Group</First>
     <Second>sample data I</Second>
  </entry>
  <entry>
      <First>Second Group</First>
     <Second>sample data II</Second>
  </entry>
   <entry>
       <First>First Group</First>
       <Second>sample data III</Second>
   </entry>
</Entries>

t:\ftemp>xslt2 andy.xml andy.xsl
<?xml version="1.0" encoding="UTF-8"?>
<Entries>
  <entryTransformation>
     <data>First Group</data>
     <secondaryData>sample data I</secondaryData>
     <secondaryData>sample data III</secondaryData>
  </entryTransformation>
  <entryTransformation>
     <data>Second Group</data>
     <secondaryData>sample data II</secondaryData>
  </entryTransformation>
</Entries>
t:\ftemp>type andy.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
               version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="/Entries">
 <Entries>
   <xsl:for-each-group select="entry" group-by="First">
     <entryTransformation>
       <data><xsl:value-of select="First"/></data>
       <xsl:for-each select="current-group()">
         <secondaryData>
           <xsl:value-of select="Second"/>
         </secondaryData>
       </xsl:for-each>
     </entryTransformation>
   </xsl:for-each-group>
 </Entries>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>

--
In-depth XSLT/XQuery hands-on training - Oakland/CA/USA 2009-08-03
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>