I am trying to use the group-by functionality in XSLT 2.0 in XSLT 1.0. I have code working in XSLT 2.0 but my processor does not handle xslt 2.0. So i need some help using the group-by function in xslt 1.0.
XML data:
<table>
<row>
<month>03</month>
<year>2016</year>
<Id>10101</Id>
<Number>1</Number>
<code>A2004</code>
</row>
<row>
<month>03</month>
<year>2016</year>
<type>A</type>
<Id>10101</Id>
<Number>2</Number>
<code>A2004</code>
</row>
<row>
<month>04</month>
<year>2015</year>
<Id>10122</Id>
<Number>1</Number>
<code>A2004</code>
</row>
</table>
I am trying to group this xml on the ID level and the numbers within it.
<test>
<xsl:for-each-group select="$table" group-by="Id">
<xsl:variable name="var2_resultof_group_items" as="item()+" select="current-group()"/>
</xsl:variable>
<row>
<xsl:attribute name="Id">
<xsl:value-of select="normalize-space(current-grouping-key())"/>
</xsl:attribute>
<xsl:for-each select="$var2_resultof_group_items">
<xsl:attribute name="month">
<xsl:value-of select="month"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="$var2_resultof_group_items">
<xsl:attribute name="year">
<xsl:value-of select="year"/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each-group select="$var2_resultof_group_items" group-by="Number">
<xsl:variable name="var1_resultof_group_items" as="item()+" select="current-group()"/>
<line>
<xsl:attribute name="number" select="current-grouping-key()"/>
<xsl:for-each select="$var1_resultof_group_items">
<xsl:attribute name="code">
<xsl:value-of select="code"/>
</xsl:attribute>
</xsl:for-each>
</line>
</xsl:for-each-group>
</row>
</xsl:for-each-group>
</test>
This is the output i am getting with my xslt 2.0 code (desired output) but i am not sure how to perform this using xslt 1.0
<test>
<row Id="10101" month="03" year="2016">
<line Number="1" code="A2004"/>
<line Number="2" code="A2004"/>
</row>
<row Id="10122" month="04" year="2015">
<line Number="1" code="A2004"/>
</row>
</test>
I am able to use XLST 1.0 to get till the output below, but i am not sure how to implement grouping of ID and then the Numbers within it:
<test>
<row Id="10101" month="03" year="2016">
<line Number="1" code="A2004"/>
</row>
<row Id="10101" month="03" year="2016">
<line Number="2" code="A2004"/>
</row>
<row Id="10122" month="04" year="2015">
<line Number="1" code="A2004"/>
</row>
</test>
You may try this muenchian grouping XSLT 1.0 solution:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="kid" match="row" use="Id"/>
<xsl:template match="table">
<test>
<xsl:for-each select="row [
count ( key('kid',./Id)[1] | . ) = 1 ]">
<xsl:variable name="this" select="."/>
<row>
<xsl:attribute name="Id">
<xsl:value-of select="Id"/>
</xsl:attribute>
<xsl:attribute name="month">
<xsl:value-of select="month"/>
</xsl:attribute>
<xsl:attribute name="year">
<xsl:value-of select="year"/>
</xsl:attribute>
<xsl:for-each select="key('kid', $this/Id)">
<line>
<xsl:attribute name="number" >
<xsl:value-of select="Number"/>
</xsl:attribute>
<xsl:attribute name="code">
<xsl:value-of select="code"/>
</xsl:attribute>
</line>
</xsl:for-each>
</row>
</xsl:for-each>
</test>
</xsl:template>
</xsl:stylesheet>
With following output:
<test>
<row Id="10101" month="03" year="2016">
<line number="1" code="A2004"/>
<line number="2" code="A2004"/>
</row>
<row Id="10122" month="04" year="2015">
<line number="1" code="A2004"/>
</row>
</test>
Here is a much simpler, shorter and almost completely in " push " style solution :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kRowById" match="row" use="Id"/>
<xsl:template match="/*">
<test>
<xsl:apply-templates/>
</test>
</xsl:template>
<xsl:template match="row[generate-id() = generate-id(key('kRowById', Id)[1])]">
<row id="{Id}">
<xsl:apply-templates select="key('kRowById', Id)" mode="inGroup"/>
</row>
</xsl:template>
<xsl:template match="row" mode="inGroup">
<line Number="{Number}" code="{code}"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<table>
<row>
<month>03</month>
<year>2016</year>
<Id>10101</Id>
<Number>1</Number>
<code>A2004</code>
</row>
<row>
<month>03</month>
<year>2016</year>
<type>A</type>
<Id>10101</Id>
<Number>2</Number>
<code>A2004</code>
</row>
<row>
<month>04</month>
<year>2015</year>
<Id>10122</Id>
<Number>1</Number>
<code>A2004</code>
</row>
</table>
the wanted, correct result is produced :
<test>
<row id="10101">
<line Number="1" code="A2004"/>
<line Number="2" code="A2004"/>
</row>
<row id="10122">
<line Number="1" code="A2004"/>
</row>
</test>
Explanation :
Muenchian grouping.
Using the standard XSLT function generate-id()
for node identity comparison.
Using AVT (Attribute-Value Templates)
Using modes.
Using the XSLT built-in (aka "default") templates.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.