Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XSLT forEach loop

Posted on 2010-09-01
4
Medium Priority
?
701 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
[X]
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
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

609 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