Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 16th, 2009, 12:29 PM
Registered User
 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem using for-each-group in an XSLT statement

Hello,
I am a beginner at XSLT 2.0, and have been trying to combine two stylesheets that I have created (I used a program called mapforce to generate stub XSLT code, then manually edited it to get the rest of the way there) into a single one, as otherwise I am running one transformation, creating the xml file for that, and then running the second transformation, and this is introducing an unnecessary intermediary step.
The problem I am having revolves around figuring out what I need to write to group the nodes, so that I can group the Sites together by their attributes. I think I should be using for-each-group to group the root, row and readings node, but I haven't been able to figure out how to do this.

The initial XML is as follows:
<root>
<row>
<Readings>
<Site id="002">
<Reading>
<RecordTitle>Electricity</RecordTitle>
<Value>60.31</Value>
<Date>30-Apr-2008</Date>
<UnitOfMeasure>Kilowatt-hour</UnitOfMeasure>
<Type>Actual</Type>
</Reading>
</Site>
</Readings>
</row>
<row>
<Readings>
<Site id="1826">
<Reading>
<RecordTitle>Electricity</RecordTitle>
<Value>58409.20</Value>
<Date>31-Jan-2008</Date>
<UnitOfMeasure>Kilowatt-hour</UnitOfMeasure>
<Type>Actual</Type>
</Reading>
</Site>
</Readings>
</row>
<row>
<Readings>
<Site id="1826">
<Reading>
<RecordTitle>Electricity</RecordTitle>
<Value>54445.52</Value>
<Date>26-Feb-2008</Date>
<UnitOfMeasure>Kilowatt-hour</UnitOfMeasure>
<Type>Actual</Type>
</Reading>
</Site>
</Readings>
</row>
</root>
The first transformation:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<Readings>
<xsl:attribute name="xsi:noNamespaceSchemaLocation">U:/INTEGR~1/CREATE~1/source/targetschema.xsd</xsl:attribute>
<xsl:for-each select="row">
<xsl:for-each select="Readings">
<xsl:for-each select="Site">
<Site>
<xsl:for-each select="@id">
<xsl:attribute name="id">
<xsl:value-of select="xs:string( . )"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="Reading">
<Reading>
<xsl:for-each select="RecordTitle">
<RecordTitle>
<xsl:value-of select="xs:string( . )"/>
</RecordTitle>
</xsl:for-each>
<xsl:for-each select="Value">
<Value>
<xsl:value-of select="xs:decimal( . )"/>
</Value>
</xsl:for-each>
<xsl:for-each select="Date">
<Date>
<xsl:value-of select="xs:token( . )"/>
</Date>
</xsl:for-each>
<xsl:for-each select="UnitOfMeasure">
<UnitOfMeasure>
<xsl:value-of select="xs:string( . )"/>
</UnitOfMeasure>
</xsl:for-each>
<xsl:for-each select="Type">
<Type>
<xsl:value-of select="xs:string( . )"/>
</Type>
</xsl:for-each>
</Reading>
</xsl:for-each>
</Site>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</Readings>
</xsl:template>
</xsl:stylesheet>

The second transformation:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Readings">
<Readings>
<xsl:for-each-group select="Site" group-by="@id">
<Site>
<xsl:for-each select="@id">
<xsl:attribute name="id">
<xsl:value-of select="xs:string( . )"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="current-group()">
<Reading>
<RecordTitle>
<xsl:value-of select="Reading/RecordTitle"/>
</RecordTitle>
<Value>
<xsl:value-of select="Reading/Value"/>
</Value>
<Date>
<xsl:value-of select="Reading/Date"/>
</Date>
<UnitOfMeasure>
<xsl:value-of select="Reading/UnitOfMeasure"/>
</UnitOfMeasure>
<Type>
<xsl:value-of select="Reading/Type"/>
</Type>
</Reading>
</xsl:for-each>
</Site>
</xsl:for-each-group>
</Readings>
</xsl:template>
</xsl:stylesheet>

The end output for the file should look like the following:
<Readings>
<Site id="002">
<Reading>
<RecordTitle>Electricity</RecordTitle>
<Value>60.31</Value>
<Date>30-Apr-2008</Date>
<UnitOfMeasure>Kilowatt-hour</UnitOfMeasure>
<Type>Actual</Type>
</Reading>
</Site>
<Site id="1826">
<Reading>
<RecordTitle>Electricity</RecordTitle>
<Value>58409.2</Value>
<Date>31-Jan-2008</Date>
<UnitOfMeasure>Kilowatt-hour</UnitOfMeasure>
<Type>Actual</Type>
</Reading>
<Reading>
<RecordTitle>Electricity</RecordTitle>
<Value>54445.52</Value>
<Date>26-Feb-2008</Date>
<UnitOfMeasure>Kilowatt-hour</UnitOfMeasure>
<Type>Actual</Type>
</Reading>
</Site>
</Readings>

Thanks, any input would be much appreciated, let me know if any more details are needed!
 
Old January 16th, 2009, 12:44 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a single stylesheet that transforms your XML input to the described output:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="root">
    <Readings>
      <xsl:for-each-group select="row/Readings/Site" group-by="@id">
        <Site id="{current-grouping-key()}">
          <xsl:copy-of select="current-group()/Reading"/>
        </Site>
      </xsl:for-each-group>
    </Readings>
  </xsl:template>

</xsl:stylesheet>
Note that I haven't tried to make sense of the stylesheets you posted, I have simply written a new one.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old January 16th, 2009, 12:52 PM
Registered User
 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow, this works. I guess I shouldn't be using other programs to generate the code for me, this seems much more efficient and streamlined than the mess I originally had to work with :)

Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
"Group by" problem dani1 XSLT 2 October 30th, 2008 03:21 PM
XSLT simulation of two column Group By wholden XSLT 2 March 31st, 2008 08:57 AM
How to group xsl elements in xslt.10 suji XSLT 1 February 29th, 2008 03:44 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
Problems with GROUP BY and CASE statement karim SQL Server 2000 2 September 23rd, 2004 06:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.