Solved

XSL to count elements meeting criteria

Posted on 2004-09-17
5
204 Views
Last Modified: 2012-06-27
I need a way to get the total number of val elements and the number of them that are between lo-val and hi-val inclusive. In the following example the answers should be 9 total with 4 in-between.

Thanks!

<doc>
<parms>
<lo-val>100</lo-val>
<hi-val>120</hi-val>
</parms>
<val>130</val>
<val>120</val>
<val>100</val>
<val>10</val>
<val>110</val>
<val>11</val>
<val>140</val>
<val>115</val>
<val>160</val>
</doc>
0
Comment
Question by:MajorBigDeal
5 Comments
 
LVL 4

Expert Comment

by:Kaarthick
ID: 12090363
0
 
LVL 11

Author Comment

by:MajorBigDeal
ID: 12090580
Thanks, I looked at it and tried to seach for an explanation of how it works but couldn't find one. If this is a good approach, can you (or anyone) please  relate it to the example I posted? I'm sorry I don't understand it but I'm new at XSL.

Thanks again!
0
 
LVL 19

Accepted Solution

by:
ramazanyich earned 300 total points
ID: 12090861
Use following xsl stylesheet:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
Total: <xsl:value-of select="count(doc/val)"/>
In between: <xsl:value-of select="count(/doc/val[node() &gt;= ../parms/lo-val and node() &lt;= ../parms/hi-val]  )"/>
</xsl:template>
</xsl:stylesheet>
0
 
LVL 7

Expert Comment

by:J_Mak
ID: 12091419
This would most certainly give you the answer. Try it out.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <!-- this will give you the total number of val elements -->
      <xsl:value-of select="count(//val)"/>
      <!-- this will give you the number of val elements that fall inside the bracket -->
      <xsl:value-of select="count(//val[child::*[1] &lt;= //hi-val] and child::*[1] &gt;= //lo-val)"/>
   </xsl:template>
</xsl:stylesheet>

Hope that helps
0
 
LVL 11

Author Comment

by:MajorBigDeal
ID: 12092781
ramazanyich, that worked, I'll try to apply it for real on Monday - Thanks!

J Mak - your version gave me the following error. Maybe due to an incompatibility in parsers.  

Expression does not return a DOM node. -->count(//val[child::*[1] <= //hi-val] and child::*[1] >= //lo-val)<--

Thanks all for your help!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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