473,388 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

XSLT problem

Hi!

I'm trying to design an XSLT in order to transform an XML document into another XML document.

The source XML looks something like this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Orders>
  3. <Order>
  4. <OrderHeader>
  5. <sCustomerNo>116442</sCustomerNo>
  6. <sOrderNo>8245673</sOrderNo>
  7. </OrderHeader>
  8. <OrderRows>
  9. <OrderRow>
  10. <sOrderLineNo>1</sOrderLineNo>
  11. <sArticleNo>102448</sArticleNo>
  12. <iQuantity>3</iQuantity>
  13. </OrderRow>
  14. <OrderRow>
  15. <sOrderLineNo>2</sOrderLineNo>
  16. <sArticleNo>102448</sArticleNo>
  17. <iQuantity>4</iQuantity>
  18. </OrderRow>
  19. </OrderRows>
  20. </Order>
  21. <Order>
  22. <OrderHeader>
  23. <sCustomerNo>116448</sCustomerNo>
  24. <sOrderNo>8245677</sOrderNo>
  25. </OrderHeader>
  26. <OrderRows>
  27. <OrderRow>
  28. <sOrderLineNo>1</sOrderLineNo>
  29. <sArticleNo>102448</sArticleNo>
  30. <iQuantity>7</iQuantity>
  31. </OrderRow>
  32. </OrderRows>
  33. </Order>
  34. </Orders>
  35.  
And I want the transformed XML to look like this:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Orders>
  3. <Order>
  4. <OrderNumber>8245673</OrderNumber>
  5. <Customer>116442</Customer>
  6. <OrderLine>
  7. <ArticleNumber>102448</ArticleNumber>
  8. <Quantity>7</Quantity>
  9. </OrderLine>
  10. </Order>
  11. <Order>
  12. <OrderNumber>8245677</OrderNumber>
  13. <Customer>116448</Customer>
  14. <OrderLine>
  15. <ArticleNumber>102448</ArticleNumber>
  16. <Quantity>7</Quantity>
  17. </OrderLine>
  18. </Order>
  19. </Orders>
  20.  
The challenge is to group all orderlines with the same article number and summarizing their quantities.

In order to acheive this I´ve found something called the Muenchian method: http://www.jenitennison.com/xslt/gro...muenchian.html

Expand|Select|Wrap|Line Numbers
  1. <xsl:key name="orderrows-by-articleno" match="OrderRows/OrderRow" use="sArticleNo" />
  2. ...
  3. <xsl:for-each select="OrderRows/OrderRow[count(. | key(''''orderrows-by-articleno'''', sArticleNo)[1]) = 1]">
  4. <xsl:variable name="sArticleNo">
  5. <xsl:value-of select="sArticleNo"/>
  6. </xsl:variable>
  7. <OrderLine>
  8. <ArticleNumber><xsl:value-of select="sArticleNo" /></ArticleNumber>
  9. <Quantity><xsl:value-of select="sum(../../OrderRows/OrderRow[sArticleNo = $sArticleNo]/iQuantity)"/></Quantity>
  10. </OrderLine>
  11. </xsl:for-each>
  12.  
But since the "orderrows-by-articleno"-key matches for all OrderRows/OrderRow in the entiry document and not per Order, I get this result:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Orders>
  3. <Order>
  4. <OrderNumber>8245673</OrderNumber>
  5. <Customer>116442</Customer>
  6. <OrderLine>
  7. <ArticleNumber>102448</ArticleNumber>
  8. <Quantity>7</Quantity>
  9. </OrderLine>
  10. </Order>
  11. <Order>
  12. <OrderNumber>8245677</OrderNumber>
  13. <Customer>116448</Customer>
  14. </Order>
  15. </Orders>
  16.  
Anyone got any idea how to make this grouping per order?
Nov 7 '07 #1
1 1180
jkmyoung
2,057 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. <xsl:key name="orderrows-by-articleno" match="OrderRows/OrderRow" use="concat(sArticleNo,generate-id(..))" />
Note this should really be generate-id(../..) to group on Order as well, but I've cheated a little and assumed that OrderRows only appear once per Order.

For-each becomes
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="OrderRows/OrderRow[count(. | key('orderrows-by-articleno', concat(sArticleNo,generate-id(..)))[1]) = 1]">
Also, why not use the key in summing? You've already created it.

Expand|Select|Wrap|Line Numbers
  1. <xsl:value-of select="sum(key('orderrows-by-articleno', concat(sArticleNo,generate-id(..)))/iQuantity)"/>
Nov 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Ola Natvig | last post by:
Hi all I'm working with a long running, threaded server which serves HTTP requests with content which are passed through a XSLT processor. The XSLT processor I'm using is the Pyana processor. ...
6
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
0
by: Mike | last post by:
I'm generating an XSLT document programatically in VB.Net. I'm then trying to apply that XSLT against a cXML document to generate my own internally developed XML document. I'm using RichTextBox...
4
by: Moogy | last post by:
I'm pulling my hair out here. First, I'm new to XML, so that doesn't help, but none of this makes any sense to me. All I'm trying to do is take a simple source XML file and translate it with an...
2
by: chris | last post by:
Hi there, I create an XML file from a dataset like this: System.IO.StreamWriter xmlSW = new System.IO.StreamWriter(FILENAME); dsUserData1.WriteXml(xmlSW, XmlWriteMode.WriteSchema);...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
3
by: thomas.porschberg | last post by:
Hi, I want to read records from a database and export it in an arbitrary format. My idea was to feed a class with a String array fetched from the database and let this class fire SAX events as...
3
by: RC | last post by:
Let's say: if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { // Now I got an XML object here var xmlDocument = XMLHttpRequestObject.responseXML; // next I have...
0
by: ManWithNoName | last post by:
The following question is related to this thread: XSLT, document() function, filename, read non-standard/English characters (like µ) I have a XML file with a non-English character in its name...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.