Solved

XSLT forEach loop

Posted on 2010-09-01
4
689 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
ID: 33584035
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
ID: 33584043
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
ID: 33608369
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
ID: 33608823
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to count occurrences of each item in an array.

680 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