Jump to content

XSLT Count() usage


andreathedove

Recommended Posts

I am writing a simple report given an XML file that is a fax log. In each fax, there is a job code, and I want to give a summary of counts by job code. I cannot figure out how to write that -- all I can manage is a total for all faxes.Here's a sample of the log entries:- <xml>- <fax> <job_id>99999998</job_id> <job_code>123</job_code> <fax_number>5551212</fax_number> <sender>xxxxxxxx</sender> <status>Successful</status> <pages>1</pages> <duration>42</duration> <fax_timestamp>2005-01-01 17:00:00</fax_timestamp> </fax>- <fax> <job_id>99999999</job_id> <job_code>456</job_code> <fax_number>5551212</fax_number> <sender>xxxxxxxx</sender> <status>Successful</status> <pages>1</pages> <duration>42</duration> <fax_timestamp>2005-01-01 18:00:00</fax_timestamp> </fax></xml>I want to be able to show a count of 1 for job code 123 and a count of 1 for job code 456. All I have been able to get to work so far is this:<xsl:value-of select="count(//job_code)"/>which just gives me the total count of 2.So how do I get to where I can show a total for each job code in the log?Thanks!

Link to comment
Share on other sites

You're going to need an XSLT extension or XSLT 2.0 for that I'm afraid. It's like asking for the distinct-values() function or the set:distinct() one.If memory serves, you were using ASP.NET right? If so, get SAXON for .NET and install it. How to you'll find at SAXON's site.If for whatever reason you can't upgrade and are thus stuck with MSXML, use the XSLT template implementation i.e. include it in your file and call it as specified on the documentation on set:distict.If you can upgrade to XSLT 2.0, it becomes as simple as

<xsl:value-of select="count(distinct-values(//job_code))"/>

with the template, it's more like

<xsl:variable name="distinct_job_codes"><xsl:call-template name="set:distinct">   <xsl:with-param name="nodes" select="//job_code" /></xsl:call-template></xsl:variable><xsl:value-of select="count($distinct_job_codes)"/>

Link to comment
Share on other sites

XSLT 2.0 or extensions are not needed.This should do what you want. Set up a key to index the job_code nodes and use the text node (the value of the node) as the search value. This will establish the unique items we are looking for. The for-each then builds a nodeset of the unique job_code nodes. It displays the value and the total count of all foundhope it helps.

<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	version="1.0">	<xsl:output method="html"/>	<xsl:key name="jobcode" match="job_code" use="text()" />	<xsl:template match = "*">		<xsl:for-each select="//job_code[count(. | key('jobcode', text())[1]) = 1]">			<p>				job_code: <xsl:value-of select="text()"/>				count: <xsl:value-of select="count(key('jobcode',text()))"/>			</p>		</xsl:for-each>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

XSLT 2.0 or extensions are not needed.This should do what you want. Set up a key to index the job_code nodes and use the text node (the value of the node) as the search value. This will establish the unique items we are looking for. The for-each then builds a nodeset of the unique job_code nodes. It displays the value and the total count of all foundhope it helps
<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	version="1.0">	<xsl:output method="html"/>	<xsl:key name="jobcode" match="job_code" use="text()" />	<xsl:template match = "*">		<xsl:for-each select="//job_code[count(. | key('jobcode', text())[1]) = 1]">			<xsl:variable name="jobcode" select="."/>			<p>				job_code: <xsl:value-of select="text()"/>				count: <xsl:value-of select="count(key('jobcode',text()[1]))"/>			</p>		</xsl:for-each>	</xsl:template></xsl:stylesheet>

I use ASP and not ASP.NET.Thanks,Andrea
Link to comment
Share on other sites

This will show the count on each job, but won't show the amount of unique job codes. What I showed will count "2" as in "two unique codes". I thought the task was the later, sorry. My bad :) .

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...