Solved

XSLT forEach loop

Posted on 2010-09-01
4
678 Views
Last Modified: 2013-11-18
Hello Experts,
Is it possible to create a XSLT forEach loop when your dataset has multiple distinct values. The dataset i'm referring looks like this
CustID  CustName   CustAddress  Phone1       Order
10         ABC            Add1              1234568     ABC1
10         ABC            Add1              1234568     ABC2
10         ABC            Add1              1234568     ABC3
20         BCD            Add2               0123456     BCD1
20         BCD            Add2               0123456     BCD2
20         BCD            Add2               0123456     BCD3
ANd the output XMl should look like this...

- <CustOrders>
  - <Customer>
      <CustName>ABC</CustName>
      <CustAddress>Add1</CustAddress>
         - <Orders>
             <Order>ABC1</Order>
             <Order>ABC2</Order>
            <Order>ABC3</Order>
            </Orders>
   </Customer>
  - <Customer>
      <CustName>BCD</CustName>
      <CustAddress>Add2</CustAddress>
         - <Orders>
             <Order>BCD1</Order>
             <Order>BCD2</Order>
            <Order>BCD3</Order>
            </Orders>
     </Customer>
  </CustOrders>
0
Comment
Question by:jsuanque
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 250 total points
Comment Utility
You can use Muenchian grouping for that

Please post your XML source with your question.
I made up an XML sample

<CustOrders>
    <Customer>
        <CustID>10</CustID>
        <CustName>ABC</CustName>
        <CustAddress>Add1</CustAddress>
        <Order>ABC1</Order>
    </Customer>    
    <Customer>
        <CustID>10</CustID>
        <CustName>ABC</CustName>
        <CustAddress>Add1</CustAddress>
        <Order>ABC2</Order>
    </Customer>
    <Customer>
        <CustID>20</CustID>
        <CustName>DEF</CustName>
        <CustAddress>Add2</CustAddress>
        <Order>DEF1</Order>
    </Customer>
</CustOrders>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:key name="customer" match="Customer" use="CustID"/>
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="CustOrders">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:for-each select="Customer[generate-id() = generate-id(key('customer', CustID)[1])]">
                <Customer>
                    <xsl:apply-templates select="CustName | CustAddress"/>
                    <Orders>
                        <xsl:for-each select="key('customer', CustID)">
                            <xsl:apply-templates select="Order"></xsl:apply-templates>
                        </xsl:for-each>
                    </Orders>
                </Customer>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
This should work, provided the XML is similar to yours. If not, please provide the source XML

In order to understand the solution, please read this article
http://www.jenitennison.com/xslt/grouping/muenchian.xml
0
 

Author Comment

by:jsuanque
Comment Utility
Hello Gertone,
Sorry for a late reply, anyway my data set is not in XML but rather its an output from a storedprocedure. I'm having a read with the link provided and slowly trying to pick up the pieces.
0
 

Author Closing Comment

by:jsuanque
Comment Utility
Thanks this is the solution. Anyway, i'll post another question since my other requirements which will somehow use the same context is somehow more complicated than this one.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now