XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Nathan LugertSubject: Double Grouping
Author: Nathan Lugert
Date: 22 Feb 2006 10:26 AM
Ok, heres the problem:

I need to transform the following XML file into batches by ship method and then into groups of 3.

<DetailList>
<details>
<name>Bob</name>
<address>Your House</address>
<merchant>Home Depot</merchant>
<productValue>50.00</productValue>
<shipMethod>RUSH</shipMethod>
</details>
<details>
<name>Jim</name>
<address>Your House</address>
<merchant>Home Depot</merchant>
<productValue>50.00</productValue>
<shipMethod>UPS</shipMethod>
</details>
......etc.
</DetailList>

I can group it by ship method with
<xsl:template match="/DetailList">
<Batches>
<xsl:for-each-group select="details" group-by="shipMethod">
<Batch id="{current-grouping-key()}">
<xsl:apply-templates select="current-group()"/>
</Batch>
</xsl:for-each-group>
</Batches>
</xsl:template>

<xsl:template match="*">
<xsl:copy-of select="."/>
</xsl:template>

And I can also group into groups of 3 with:

<xsl:param name="max" select="3"/>

<xsl:template match="/ALGGiftCard" name="number">
<BatchList>
<xsl:for-each select="details[position() mod $max = 1]">
<Batch>
<xsl:for-each select=". | following-sibling::details[position() &lt; $max]">
<xsl:call-template name="copyAll"/>
</xsl:for-each>
</Batch>
</xsl:for-each>
</BatchList>
</xsl:template>

<xsl:template name="copyAll" match="*">
<xsl:copy-of select="."/>
</xsl:template>

But I am at a lost when trying to combine them together. The resulting XML file needs to look something like:

<Batch id="RUSH_1">
<details>
<name>Bob</name>
<address>Your House</address>
<merchant>Home Depot</merchant>
<productValue>50.00</productValue>
<shipMethod>RUSH</shipMethod>
</details>
<details>
<name>Larry</name>
<address>Your House</address>
<merchant>Lowes</merchant>
<productValue>20.00</productValue>
<shipMethod>RUSH</shipMethod>
</details>
<details>
<name>Pappy</name>
<address>Your House</address>
<merchant>KMart</merchant>
<productValue>150,000.00</productValue>
<shipMethod>RUSH</shipMethod>
</details>
</Batch>
<Batch id="RUSH_2">
<details>
<name>Jim</name>
<address>Your House</address>
<merchant>Home Depot</merchant>
<productValue>50.00</productValue>
<shipMethod>RUSH</shipMethod>
</details>
<details>
<name>Ken</name>
<address>Your House</address>
<merchant>Lowes</merchant>
<productValue>20.00</productValue>
<shipMethod>RUSH</shipMethod>
</details>
<details>
<name>Jose</name>
<address>Your House</address>
<merchant>KMart</merchant>
<productValue>150,000.00</productValue>
<shipMethod>RUSH</shipMethod>
</details>
</Batch>

Any help would be Greatly Appreciated.


Nate



Posttop
Minollo I.Subject: Double Grouping
Author: Minollo I.
Date: 23 Feb 2006 01:29 PM
Nate,
something like this may help:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="max" select="3"/>
<xsl:key name="group" match="details" use="shipMethod"/>
<xsl:template match="/">
<AllBatches>
<xsl:for-each select="//details[generate-id() = generate-id(key('group', shipMethod)[1])]">
<xsl:variable name="shipMethod" select="shipMethod"/>
<Group>
<xsl:attribute name="shipMethod">
<xsl:value-of select="$shipMethod"/>
</xsl:attribute>
<xsl:for-each select="//details[shipMethod = $shipMethod and count(preceding-sibling::*[shipMethod = $shipMethod]) mod $max = 0]">
<Batch>
<xsl:attribute name="id">
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:variable name="listPosition" select="count(preceding-sibling::*[shipMethod = $shipMethod])"/>
<xsl:for-each select="//details[shipMethod = $shipMethod]">
<xsl:if test="position() &gt; $listPosition and position() &lt;= $listPosition + $max">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</Batch>
</xsl:for-each>
</Group>
</xsl:for-each>
</AllBatches>
</xsl:template>
</xsl:stylesheet>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.