Hello I'm trying to create grouping in the pdf file
and I have to use grouping inside another grouping. f.e. group by date, then group by name, then by city...
xml:
<record>
<name>Palace1</name>
<info>
<date>2012-01-01</date>
<city>Random1</city>
</info>
<info>
<date>2012-01-01</date>
<city>SuperRandom</city>
</info>
<info>
<date>2012-01-02</date>
<city>Random22</city>
</info>
...
</record>
<record>
<name>Palace2</name>
<info>
<date>2012-01-01</date>
<city>Random99</city>
</info>
<info>
<date>2012-01-02</date>
<city>Random1</city>
</info>
...
</record>
...
So lets say we need to group our records by date from 2012-01-01 to 2012-01-01 and the group them by name
Date 2012-01-01
Place1
Random1
SuperRandom
Palace2
Random99
Date 2012-01-02
Palace1
Random22
Palace2
Random1
SO I was using
<xsl:for-each-group select="dt:record" group-by="dt:info/dt:date">
<xsl:sort select="dt:date" order="ascending"/>
<fo:block font-weight="bold"> Date: <xsl:value-of select="format-dateTime(dt:date,'[Y0001].[M01].[D01]','en',(),'lt')"/></fo:block>
<xsl:for-each select="current-group()"> //here im guessing we should put another for- each-group
<xsl:for-each-group select="parent::dt:info/dt:record" group-by="dt:name">
<fo:block>Place1 <xsl:value-of select="dt:name"><fo:block>
<xsl:for-each select="current-group()">
<fo:block> <xsl:value-of select="dt:info/dt:city"></fo:block>
</xsl:for-each>
</xsl:for-each-group>
</xsl:for-each>
</xsl:for-each-group>
but this doesn't work... for some reason I get way more record names then I should
Here is how to do such groupings:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:for-each-group select="*/info" group-by="date">
<xsl:sort select="date"/>
Date: <xsl:value-of select="date"/>
<xsl:for-each-group select="current-group()" group-by="../name">
<xsl:value-of select="concat('
 ',../name)"/>
<xsl:for-each-group select="current-group()" group-by="city">
<xsl:value-of select="concat('
 ', city)"/>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the well-formed XML document derived from the non-wellformed provided XML:
<t>
<record>
<name>Palace1</name>
<info>
<date>2012-01-01</date>
<city>Random1</city>
</info>
<info>
<date>2012-01-01</date>
<city>SuperRandom</city>
</info>
<info>
<date>2012-01-02</date>
<city>Random22</city>
</info> ...
</record>
<record>
<name>Palace2</name>
<info>
<date>2012-01-01</date>
<city>Random99</city>
</info>
<info>
<date>2012-01-02</date>
<city>Random1</city>
</info> ...
</record>
</t>
the wanted, correctly-grouped result is produced:
Date: 2012-01-01
Palace1
Random1
SuperRandom
Palace2
Random99
Date: 2012-01-02
Palace1
Random22
Palace2
Random1
Here is an example, it outputs HTML, no XSL-FO but the grouping is of course the same:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="root">
<xsl:for-each-group select="record/info" group-by="xs:date(date)">
<xsl:sort select="current-grouping-key()"/>
<div>
<h2><xsl:value-of select="current-grouping-key()"/></h2>
<xsl:for-each-group select="current-group()" group-by="parent::record/name">
<div>
<h3><xsl:value-of select="current-grouping-key()"/></h3>
<ul>
<xsl:for-each select="current-group()">
<li>
<xsl:value-of select="city"/>
</li>
</xsl:for-each>
</ul>
</div>
</xsl:for-each-group>
</div>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
When applied on
<root>
<record>
<name>Palace1</name>
<info>
<date>2012-01-01</date>
<city>Random1</city>
</info>
<info>
<date>2012-01-01</date>
<city>SuperRandom</city>
</info>
<info>
<date>2012-01-02</date>
<city>Random22</city>
</info>
...
</record>
<record>
<name>Palace2</name>
<info>
<date>2012-01-01</date>
<city>Random99</city>
</info>
<info>
<date>2012-01-02</date>
<city>Random1</city>
</info>
...
</record>
</root>
I get
<div>
<h2>2012-01-01</h2>
<div>
<h3>Palace1</h3>
<ul>
<li>Random1</li>
<li>SuperRandom</li>
</ul>
</div>
<div>
<h3>Palace2</h3>
<ul>
<li>Random99</li>
</ul>
</div>
</div>
<div>
<h2>2012-01-02</h2>
<div>
<h3>Palace1</h3>
<ul>
<li>Random22</li>
</ul>
</div>
<div>
<h3>Palace2</h3>
<ul>
<li>Random1</li>
</ul>
</div>
</div>