Solved

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

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now