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

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

Posted on 2008-10-17
4
511 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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