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!
|