Solved

XSL to count elements meeting criteria

Posted on 2004-09-17
5
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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