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
Solved

XSL to count elements meeting criteria

Posted on 2004-09-17
5
205 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 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