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 1st, 2007, 11:15 PM
Registered User
 
Join Date: Oct 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Grouping

Hi all

I have a problem with sequential processing of some xml and grouping by ID. I have a source with a Trans node followed by TransItem nodes. I am able to read the preceding sibling value of these but I do not know how to group and wrap them as seperate transactions in my output. Each time I try something it loses the sequential order of the source file.

Can anyone suggest a solution?

Many thanks

Simon

************************************************** ******************
Source XML***

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="C:\Documents and Settings\scowley\My Documents\SAP\XI\XML\MobilStandard.xslt"?>
<n1:MobilStandard xmlns:n1="urn:simon.com:co:xi:process:fuel">
<Trans>
<Docket>1234</Docket>
<Fee>Fee</Fee>
<Amount>10</Amount>
</Trans>
<TransItem>
<ID/>
<Product>ULP</Product>
<Amount>10</Amount>
</TransItem>
<TransItem>
<ID/>
<Product>ULP</Product>
<Amount>50</Amount>
</TransItem>
<Trans>
<Docket>1235</Docket>
<Fee>Fee1</Fee>
<Amount>10</Amount>
</Trans>
<TransItem>
<ID/>
<Product>ULP</Product>
<Amount>70</Amount>
</TransItem>
</n1:MobilStandard>


************************************************** ****************
XSLT mapping ***

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:n1="urn:simon.com:co:xi:process:fuel" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xsi xs n1 fn">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<n1:MobilStandard>
<xsl:apply-templates/>
</n1:MobilStandard>
</xsl:template>
<xsl:template match="n1:MobilStandard/Trans">
<Item>
<ID>
<xsl:value-of select="Docket"/>
</ID>
<Product>
<xsl:value-of select="Fee"/>
</Product>
<Amount>
<xsl:value-of select="Amount"/>
</Amount>
</Item>
</xsl:template>
<xsl:template match="n1:MobilStandard/TransItem">
<Item>
<ID>
<xsl:value-of select="preceding-sibling::Trans[1]/Docket"/>
</ID>
<Product>
<xsl:value-of select="Product"/>
</Product>
<Amount>
<xsl:value-of select="Amount"/>
</Amount>
</Item>
</xsl:template>
</xsl:stylesheet>


************************************************** **************
Current output***

<?xml version="1.0" encoding="UTF-8"?>
<n1:MobilStandard xmlns:n1="urn:simon.com:co:xi:process:fuel">
<Item>
<ID>1234</ID>
<Product>Fee</Product>
<Amount>10</Amount>
</Item>
<Item>
<ID>1234</ID>
<Product>ULP</Product>
<Amount>10</Amount>
</Item>
<Item>
<ID>1234</ID>
<Product>ULP</Product>
<Amount>50</Amount>
</Item>
<Item>
<ID>1235</ID>
<Product>Fee1</Product>
<Amount>10</Amount>
</Item>
<Item>
<ID>1235</ID>
<Product>ULP</Product>
<Amount>70</Amount>
</Item>
</n1:MobilStandard>

************************************************** *****************
Desired output***

<?xml version="1.0" encoding="UTF-8"?>
<n1:MobilStandard xmlns:n1="urn:simon.com:co:xi:process:fuel">
<Trans>
<Item>
<ID>1234</ID>
<Product>Fee</Product>
<Amount>10</Amount>
</Item>
<Item>
<ID>1234</ID>
<Product>ULP</Product>
<Amount>10</Amount>
</Item>
<Item>
<ID>1234</ID>
<Product>ULP</Product>
<Amount>50</Amount>
</Item>
</Trans>
<Trans>
<Item>
<ID>1235</ID>
<Product>Fee1</Product>
<Amount>10</Amount>
</Item>
<Item>
<ID>1235</ID>
<Product>ULP</Product>
<Amount>70</Amount>
</Item>
</Trans>
</n1:MobilStandard>
 
Old October 2nd, 2007, 02:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You almost answer the problem yourself on the first line, or at least diagnose the problem.

You're trying to do grouping in a sequential way. Since you appear to be using XSLT 2.0 why not try doing grouping using the xsl:for-each-group element. I'd recommend investigating the group-starting-with attribute.

The XSLT Specification has a fairly good example on grouping with the group-starting-with attribute which is very similar to your requirements:

http://www.w3.org/TR/xslt20/#grouping-examples


/- Sam Judson : Wrox Technical Editor -/
 
Old October 2nd, 2007, 03:59 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I was never very good at those spot-the-difference competitions, but I can't actually see where your current output and desired output differ.

But I think your error is here:

<xsl:template match="n1:MobilStandard/TransItem">
<Item>
<ID>
<xsl:value-of select="preceding-sibling::Trans[1]/Docket"/>

... a TransItem doesn't have a preceding sibling called Trans, you need to do "../preceding-sibling::Trans[1]/Docket"

As Sam says, this is a lot easier if you use xsl:for-each-group. If you need a 1.0 solution, look at Muenchian grouping: http://www.jenitennison.com/xslt/grouping

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem of grouping in XSLT LeoMathew XSLT 2 September 11th, 2008 09:38 AM
Grouping with XSLT 1.0 rodmcleay XSLT 1 January 14th, 2008 11:42 PM
XSLT 1.0 Grouping kwilliams XSLT 0 January 11th, 2006 06:30 PM
XSLT Grouping Help Missy XSLT 0 December 14th, 2005 10:28 PM
Grouping Elements using XSLT mathuranuj XSLT 2 June 21st, 2005 02:56 AM





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