Link to home
Start Free TrialLog in
Avatar of TexSoft
TexSoft

asked on

XSLT Count() usage

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!
Avatar of dualsoul
dualsoul

Add predicate in [] to indicate particular conditions. You can specify it dynamicly in for-each loop, or staticly as in example below:

<xsl:value-of select="count(//job_code[.='123'])"/>
<xsl:value-of select="count(//job_code[.='456'])"/>
Avatar of TexSoft

ASKER

This syntax works staticly, as you suggest -- thanks.  But since I don't know what the job code are going to be, I need to figure out how to do this based on whatever the log contains, which can be different every time.  I assume that means that I need to put this code in the for-each loop, but how do I dynamically refer to each job code?

Thanks!
<xsl:for-each select="fax">
  <!-- some processing you like -->

   <!-- count job_codes with value the same as this fax/job_code -->
   <xsl:value-of select="count(//job_code[.=job_code])"/>

  <!-- additional processing -->
</xsl:for-each>
Avatar of TexSoft

ASKER

That just always returns zero.
ASKER CERTIFIED SOLUTION
Avatar of dualsoul
dualsoul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TexSoft

ASKER

Thanks much for your help.  This syntax is a bit mind-boggling!