Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 25th, 2012, 06:44 PM
Registered User
 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default muenchian sum based on multiple grouping

I want to obtain a better xml file using some xslt and xml below.

<?xml version="1.0" encoding="UTF-8"?>

<Pallets><Pallet>

<PalletNumber>2</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>1</Product>
<Quantity>4</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2</PalletNumber>
<Product>2</Product>
<Quantity>1</Quantity>
</Pallet>
<Pallet>
<PalletNumber>3</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>4</PalletNumber>
<Product>2</Product>
<Quantity>2</Quantity>
</Pallet></Pallets>

I need to sum up quantity per pallet number and product(see first node below) .Also sequence number is needed in the right part of palletnumber value based on first appearance in the xml.Results should look like :

<?xml version="1.0" encoding="UTF-8"?>

<Pallets><Pallet>
<PalletNumber>2_1</PalletNumber>
<Product>1</Product>
<Quantity>6</Quantity>
</Pallet>
<Pallet>
<PalletNumber>2_2</PalletNumber>
<Product>2</Product>
<Quantity>1</Quantity>
</Pallet>
<Pallet>
<PalletNumber>3_1</PalletNumber>
<Product>1</Product>
<Quantity>2</Quantity>
</Pallet>
<Pallet>
<PalletNumber>4_1</PalletNumber>
<Product>2</Product>
<Quantity>2</Quantity>
</Pallet></Pallets>

Please provide XSLT 1.0 support .Thanks a lot.
 
Old November 2nd, 2012, 10:28 AM
Registered User
 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Solution

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:apply-templates select="Pallets"/>
</xsl:template>
<xsl:template match="Pallets">
<Pallets>
<xsl:for-each-group select="Pallet" group-by="PalletNumber">
<xsl:for-each-group select="current-group()" group-by="Product">
<xsl:variable name="count" select="count(current-group())"/>
<Pallet>
<xsl:variable name="number" select="PalletNumber"/>
<PalletNumber>
<xsl:value-of select="concat($number,'_',$count)"/>
</PalletNumber>
<xsl:copy-of select="Product"/>
<Quantity>
<xsl:value-of select="sum(current-group()/Quantity)"/>
</Quantity>
</Pallet>
</xsl:for-each-group>
</xsl:for-each-group>
</Pallets>
</xsl:template>
</xsl:stylesheet>

Last edited by shil; November 2nd, 2012 at 11:23 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
help with Muenchian method grouping and html table mickhughes XSLT 3 May 1st, 2008 06:24 AM
help with Muenchian method grouping... agentdz015 XSLT 1 April 7th, 2008 04:53 PM
Muenchian grouping amhicraig XSLT 1 December 5th, 2007 06:43 PM
Muenchian Grouping Chamkaur XSLT 1 June 21st, 2006 10:51 AM
Advanced Grouping Using the Muenchian Method Bodiam XSLT 0 August 8th, 2005 11:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.