1
<result name="test">
   <cpu_time>  45346.23 </cpu_time>
   <max_mem>      1104 MB</max_mem>
</result>
<result name="error_test">
   <cpu_time>   5300.80 </cpu_time>
   <max_mem>      1059 MB</max_mem>
</result>

I have a the above XML file, I need to count the number of times <cpu_time> entries which have values less than 3600 in XSLT. Here is the XSLT so far:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="iso-8859-1" indent="no"/>
  <xsl:template match="/">
    <xsl:value-of select="count(result/cpu_time) &lt; 3600" />
  </xsl:template>
</xsl:stylesheet>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Szed
  • 11
  • 2
  • The XML sample you provided is broken (not well-formed). Unless you fix it, all answers will be vague. – Tomalak Jul 23 '12 at 20:04

2 Answers2

1

Try something along the lines of

<xsl:value-of select="count(result[number(cpu_time) &lt; 3600])" />
Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

This works for me (on www.xslfiddle.net)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="iso-8859-1" indent="no"/>
    <xsl:template match="root">
        <xsl:value-of select="count(result/cpu_time[. &lt; 3600])" />
    </xsl:template>
</xsl:stylesheet>

Note that you need a single root element for XML, and your node names are mismatched (I added a root element named "root", and made all of the child elements 'result' for testing.)

royh
  • 793
  • 1
  • 5
  • 12
  • Thanks for the input, this helps me. How do I code something like which could provide the count the entries in between certain value : Example like cpu_time &lt 7200 and &gt 3600. – Szed Jul 24 '12 at 15:17
  • You can use infix `and`, like this: `count(result/cpu_time[. < 7200 and . > 3600])` – royh Jul 24 '12 at 15:33
  • Or do I need to use something like "count(result/cpu_time[. < 7200])" - "count(result/cpu_time[. < 3600])" – Szed Jul 24 '12 at 15:33