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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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>

   
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.