Solved

XSL to count elements meeting criteria

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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, …
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. 
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now