Re: [xsl] Urgent please folks, need help wih 'paging' and nested grouping-by

Subject: Re: [xsl] Urgent please folks, need help wih 'paging' and nested grouping-by
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 03 Jan 2002 09:20:48 -0500
(RETRY due to date problems ... sorry if you receive a duplicate .... Ken)

At 2002-01-03 13:44 +0200, Manos Batsis wrote:
I have a flat file of <row> elements like this:

<row Ent_ID="1" Ent_1="Manos" Ent_2="Batsis" Ent_3="developer">
<row Ent_ID="1" Ent_1="Helen" Ent_2="Crown" Ent_3="secretary">
<row Ent_ID="1" Ent_1="Manos" Ent_2="Dollan" Ent_3="programmer">
<!-- [...] -->
<row Ent_ID="10" Ent_1="Mickael" Ent_2="Crown" Ent_3="copyrighter">

My result file should devide the flat file to mixed <page> and <GroupBy
> elements. These can be nested within eachother; the <page> element is
used to hold up to 10 records,

You don't say if this number 10 is a hard number, or an artifact of the input.


while the <GroupBy > elements are used to
devide the records into (what else?) groups. These groups can be based
on any attribute of the records and can contain more levels of grouping,
as well as other <page> elements (if the members of a group are more
than ten):

I'm sorry I can't understand what you mean for page, but I can help you with the grouping.


I've attached below a grouping solution that does not accommodate the page counting you mentioned, since I'm not sure what you want, but you may be able to modify what I've given you to help.

I described the hybrid use of variables for grouping earlier:

http://www.biglist.com/lists/xsl-list/archives/200110/msg00943.html

Essentially, rather than building key tables as in the Muenchian Method (especially given that you are passing the attribute name as a parameter), build the node tables in variables and then do the grouping in the variable using what we learned from the Muenchian Method. This hybrid approach allows you to easily do groups within groups.

So, we have multiple sorting and paging on a hard deadline. I receive
the attributes I base my grouping upon as parameters, let's say there
are three of them and their priority is as $param1, $param2, $param3.

Not a problem given the approach shown below ... note how I compare the node name of the attribute node with the value in the parameter. I hope the mailer doesn't corrupt the legibility of the nesting.


BTW, I sorted the resulting groups which you may not wish to do.

Any help towards the right direction, would be much, much appreciated.

I hope this helps.


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

p.s. those of you who have purchased the online book, the sorting chapter has been updated in the 10th edition to include a section on grouping with variables; your purchase entitles you to your free update. This will also be found in the online CBT version going live at the end of the month, and in the 3-day live version being delivered next month.

t:\ftemp>type manos.xml
<rows>
<row Ent_ID="1" Ent_1="Manos" Ent_2="Batsis" Ent_3="developer"/>
<row Ent_ID="1" Ent_1="Helen" Ent_2="Crown" Ent_3="secretary"/>
<row Ent_ID="1" Ent_1="Manos" Ent_2="Dollan" Ent_3="programmer"/>
<row Ent_ID="2" Ent_1="Manos" Ent_2="Batsis" Ent_3="developer1"/>
<row Ent_ID="2" Ent_1="Helen" Ent_2="Crown" Ent_3="secretary"/>
<row Ent_ID="2" Ent_1="Manos" Ent_2="Dollan" Ent_3="programmer"/>
<row Ent_ID="1" Ent_1="Manos" Ent_2="Batsis" Ent_3="developer2"/>
<row Ent_ID="1" Ent_1="Helen" Ent_2="Crown" Ent_3="secretary"/>
<row Ent_ID="1" Ent_1="Manos" Ent_2="Dollan" Ent_3="programmer"/>
<row Ent_ID="10" Ent_1="Mickael" Ent_2="Crown" Ent_3="copyrighter"/>
</rows>
t:\ftemp>type manos.xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:param name="a1" select="'Ent_ID'"/>
<xsl:param name="a2" select="'Ent_1'"/>
<xsl:param name="a3" select="'Ent_2'"/>

<xsl:template match="/*">
<page>
<xsl:variable name="id-rows" select="row"/>
<xsl:for-each select="$id-rows">
<xsl:sort select="@*[name(.)=$a1]"/>
<xsl:if test="generate-id(.)=
generate-id($id-rows[@*[name(.)=$a1]=current()/@*[name(.)=$a1]])">
<GroupBy GroupByFields="{$a1}">
<xsl:variable name="e1-rows"
select="$id-rows[@*[name(.)=$a1]=current()/@*[name(.)=$a1]]"/>
<xsl:for-each select="$e1-rows">
<xsl:sort select="@*[name(.)=$a2]"/>
<xsl:if test="generate-id(.)=
generate-id($e1-rows[@*[name(.)=$a2]=current()/@*[name(.)=$a2]])">
<GroupBy GroupByFields="{$a2}">
<xsl:variable name="e2-rows"
select="$e1-rows[@*[name(.)=$a2]=current()/@*[name(.)=$a2]]"/>
<xsl:for-each select="$e2-rows">
<xsl:sort select="@*[name(.)=$a3]"/>
<xsl:if test="generate-id(.)=
generate-id($e2-rows[@*[name(.)=$a3]=current()/@*[name(.)=$a3]])">
<GroupBy GroupByFields="{$a3}">
<xsl:copy-of select="$e2-rows[@*[name(.)=$a3]=current()/@*[name(.)=$a3]]"/>
</GroupBy>
</xsl:if>
</xsl:for-each>
</GroupBy>
</xsl:if>
</xsl:for-each>
</GroupBy>
</xsl:if>
</xsl:for-each>
</page>
</xsl:template>


</xsl:stylesheet>

t:\ftemp>xt manos.xml manos.xsl manos.out

t:\ftemp>type manos.out
<?xml version="1.0" encoding="utf-8"?>
<page>
<GroupBy GroupByFields="Ent_ID">
<GroupBy GroupByFields="Ent_1">
<GroupBy GroupByFields="Ent_2">
<row Ent_ID="1" Ent_1="Helen" Ent_2="Crown" Ent_3="secretary"/>
<row Ent_ID="1" Ent_1="Helen" Ent_2="Crown" Ent_3="secretary"/>
</GroupBy>
</GroupBy>
<GroupBy GroupByFields="Ent_1">
<GroupBy GroupByFields="Ent_2">
<row Ent_ID="1" Ent_1="Manos" Ent_2="Batsis" Ent_3="developer"/>
<row Ent_ID="1" Ent_1="Manos" Ent_2="Batsis" Ent_3="developer2"/>
</GroupBy>
<GroupBy GroupByFields="Ent_2">
<row Ent_ID="1" Ent_1="Manos" Ent_2="Dollan" Ent_3="programmer"/>
<row Ent_ID="1" Ent_1="Manos" Ent_2="Dollan" Ent_3="programmer"/>
</GroupBy>
</GroupBy>
</GroupBy>
<GroupBy GroupByFields="Ent_ID">
<GroupBy GroupByFields="Ent_1">
<GroupBy GroupByFields="Ent_2">
<row Ent_ID="10" Ent_1="Mickael" Ent_2="Crown" Ent_3="copyrighter"/>
</GroupBy>
</GroupBy>
</GroupBy>
<GroupBy GroupByFields="Ent_ID">
<GroupBy GroupByFields="Ent_1">
<GroupBy GroupByFields="Ent_2">
<row Ent_ID="2" Ent_1="Helen" Ent_2="Crown" Ent_3="secretary"/>
</GroupBy>
</GroupBy>
<GroupBy GroupByFields="Ent_1">
<GroupBy GroupByFields="Ent_2">
<row Ent_ID="2" Ent_1="Manos" Ent_2="Batsis" Ent_3="developer1"/>
</GroupBy>
<GroupBy GroupByFields="Ent_2">
<row Ent_ID="2" Ent_1="Manos" Ent_2="Dollan" Ent_3="programmer"/>
</GroupBy>
</GroupBy>
</GroupBy>
</page>




-- Training Blitz: 3-days XSLT/XPath, 2-days XSLFO - Feb 18-22, 2002

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                        Definitive XSLT & XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:   2002-01-10,11,16,18,02-11,12,13,15,18,21,
-                                03-11,14,15,18,19,04-08,09,10,12


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread