Solved

How do you count and loop in XSLT.It seems a trick is required.

Posted on 2008-10-17
4
513 Views
Last Modified: 2013-11-18
I need to process a summary of these XML Records

<ROWSET>
   <ROW num="1">
      <CNAM>Company 1</CNAM>
      <ISSUENO>1744</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>CPE</RESRCH>
      <STATUSDT>9/22/2008 10:41:59</STATUSDT>
   </ROW>
   <ROW num="2">
      <CNAM>Company 2</CNAM>
      <ISSUENO>1334</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>SF</RESRCH>
      <STATUSDT>9/22/2008 10:42:33</STATUSDT>
   </ROW>
   <ROW num="3">
      <CNAM>Company 3</CNAM>
      <ISSUENO>1585</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>CPE</RESRCH>
      <STATUSDT>9/22/2008 10:42:50</STATUSDT>
   </ROW>
   <ROW num="4">
      <CNAM>Company 4</CNAM>
      <ISSUENO>1569</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>CPE</RESRCH>
      <STATUSDT>9/22/2008 10:43:20</STATUSDT>
   </ROW>
   <ROW num="5">
      <CNAM>Company 5</CNAM>
      <ISSUENO>9139</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>CPE</RESRCH>
      <STATUSDT>9/22/2008 10:44:23</STATUSDT>
   </ROW>
   <ROW num="6">
      <CNAM>Company 6</CNAM>
      <ISSUENO>9059</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>SR</TMRKT>
      <RESRCH>CPE</RESRCH>
      <STATUSDT>9/22/2008 10:47:49</STATUSDT>
   </ROW>
   <ROW num="7">
      <CNAM>Company 7</CNAM>
      <ISSUENO>9139</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>SR</TMRKT>
      <RESRCH>AGR</RESRCH>
      <STATUSDT>9/22/2008 10:49:46</STATUSDT>
      <STATUS_WAS>A</STATUS_WAS>
   </ROW>
   <ROW num="8">
      <CNAM>Company 8</CNAM>
      <ISSUENO>9125</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>SR</TMRKT>
      <RESRCH>NP</RESRCH>
      <STATUSDT>9/22/2008 10:57:32</STATUSDT>
   </ROW>
   <ROW num="9">
      <CNAM>Company 9</CNAM>
      <ISSUENO>2052</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>SR</TMRKT>
      <RESRCH>MLA</RESRCH>
      <STATUSDT>9/22/2008 10:59:21</STATUSDT>
   </ROW>
   <ROW num="10">
      <CNAM>Company 10</CNAM>
      <ISSUENO>1585</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>SR</TMRKT>
      <RESRCH>CPE</RESRCH>
      <STATUSDT>9/22/2008 11:1:14</STATUSDT>
   </ROW>
   <ROW num="11">
      <CNAM>Company 11</CNAM>
      <ISSUENO>9152</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>SR</TMRKT>
      <RESRCH>EIM</RESRCH>
      <STATUSDT>9/22/2008 11:1:54</STATUSDT>
   </ROW>
   <ROW num="12">
      <CNAM>Company 12</CNAM>
      <ISSUENO>1569</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>SR</TMRKT>
      <RESRCH>CD</RESRCH>
      <STATUSDT>9/22/2008 11:4:1</STATUSDT>
   </ROW>
   <ROW num="13">
      <CNAM>Company 13</CNAM>
      <ISSUENO>5002</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>SF</RESRCH>
      <STATUSDT>9/22/2008 11:6:18</STATUSDT>
   </ROW>
   <ROW num="14">
      <CNAM>Company 14</CNAM>
      <ISSUENO>1611</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>EIM</RESRCH>
      <STATUSDT>9/22/2008 11:7:6</STATUSDT>
   </ROW>
   <ROW num="15">
      <CNAM>Company 15</CNAM>
      <ISSUENO>7108</ISSUENO>
      <STATUS>V</STATUS>
      <TMRKT>JRH</TMRKT>
      <RESRCH>CPE</RESRCH>
      <STATUSDT>9/22/2008 11:7:57</STATUSDT>
   </ROW>
</ROWSET>

I want my output to look like this after 1 transform.

<Report>
  <Item>
    <Name>JRH</Name>
    <Count>8</Count>
  </Item>
  <Item>
    <Name>SR</Name>
    <Count>7</Count>
  </Item>  
</Telemarketers>

I can't figure out the looping and counting in XSLT? Is there a simple solution.
0
Comment
Question by:Marek_Zyskowski
  • 3
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22745333

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="row" match="ROW" use="TMRKT"/>
    <xsl:output indent="yes"/>
    <xsl:template match="ROWSET">
        <Report>
            <xsl:for-each select="ROW[generate-id() = generate-id(key('row', TMRKT)[1])]">
                <Item>
                    <Name><xsl:value-of select="TMRKT"/></Name>
                    <Count><xsl:value-of select="count(key('row', TMRKT))"/></Count>
                </Item>
            </xsl:for-each>
        </Report>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22745349
Looping in XSLT is generally simple.
You just push out the nodes using xsl:apply-templates or use an xsl:for-each

The difficulty in this little task however is looping over ROW elements with distinct TMRKT
That is grouping, for which you could use the muenchian technique, as I did in my solution,
explained remarkably well here
http://www.jenitennison.com/xslt/grouping/muenchian.html
0
 
LVL 1

Author Closing Comment

by:Marek_Zyskowski
ID: 31507309
I tried the solution and it looks like it works perfectly. Thanks.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22757708
welcome
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting Started on web Development for an experienced C++ developer 7 133
xml attribute DTD attribute differences 3 79
Specific format 21 216
Fulfillment API php code sample 1 72
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

713 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