Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

XSLT forEach loop

Posted on 2010-09-01
4
688 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS: Element name is not scoping properly 4 77
Form submit issue 11 86
Animated .jpg? 13 97
Where does legacy ASP.NET initialize language? 2 25
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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