Solved

XSLT forEach loop

Posted on 2010-09-01
4
691 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
website file permissions 4 87
How to redirect all users but me properly to another page with htaccess 2 166
How to write an English digest paper 6 141
MediaWiki - Move a site 4 54
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…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

737 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