Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Issue configuring a dropdown selector on a donation form. 13 68
Problem to file 3 67
cookies analysis tools 2 67
Re-position the objects 7 96
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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 …

919 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

22 Experts available now in Live!

Get 1:1 Help Now