Szed
Szed

Reputation: 11

XML frequency count

<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>

Upvotes: 1

Views: 393

Answers (2)

royh
royh

Reputation: 793

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.)

Upvotes: 1

Tomalak
Tomalak

Reputation: 338376

Try something along the lines of

<xsl:value-of select="count(result[number(cpu_time) &lt; 3600])" />

Upvotes: 1

Related Questions