Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-17
4
Medium Priority
?
525 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
[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
  • 3
4 Comments
 
LVL 60

Accepted Solution

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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

722 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