473,395 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Select nodes based on count of values

I am trying to use XPath (XSLT 1.0), EXSLT 1.1 (.Net) to select the
nodelist consisting of the 'top n' nodes. THe counter
is the count of item/value[ fieldName].

Using a traditional for-each logic, the psuedo code would look
something like this

counter=0
maxItems=4
nodelist = nothing

for each root/item
counter = counter + count(value/fieldName)
if counter < maxItems
nodelist.add (current() ) //adds this node to the nodelist
else
exit for
end if

next item
What I would expect of the above code is to return a nodeset consisting
of <item>s id 1 & 2. item id=3 was not included because it's children
made the counter exceed maxItems. Note, the item id value is only used
for reference.

Any help would be greatly appreciated by "One Less Bald Guy" :-)

Thanks-
Larry

=================================================
<root>
<item id="1">
<value name="fieldName" instance="1">Alpha</value>
<value name="fieldName" instance="2">Beta</value>
</item>
<item id="2">
<value name="fieldName" instance="1">Charlie</value>
</item>
<item id="3">
<value name="fieldName" instance="1">Delta</value>
<value name="fieldName" instance="2">Echo</value>
</item>
</root>

Sep 28 '05 #1
4 1692
Larry R wrote:
I am trying to use XPath (XSLT 1.0), EXSLT 1.1 (.Net) to select the
nodelist consisting of the 'top n' nodes. THe counter
is the count of item/value[ fieldName].

Using a traditional for-each logic, the psuedo code would look
something like this

counter=0
maxItems=4
nodelist = nothing

for each root/item
counter = counter + count(value/fieldName)
if counter < maxItems
nodelist.add (current() ) //adds this node to the nodelist
else
exit for
end if

If you want to use the items in their document order, then the
XSLT below will do this.

If you want to use the items in a sorted order it gets rather
more complex.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="text"/>

<xsl:variable name="maxval">
<xsl:text>4</xsl:text>
</xsl:variable>

<xsl:template match="/">
<xsl:apply-templates
select="root/item
[sum(value/@instance)+
sum(preceding::value/@instance) &lt;= $maxval]"/>
</xsl:template>

<xsl:template match="item">
<xsl:value-of select="@id"/>
</xsl:template>

</xsl:stylesheet>

///Peter


What I would expect of the above code is to return a nodeset consisting
of <item>s id 1 & 2. item id=3 was not included because it's children
made the counter exceed maxItems. Note, the item id value is only used
for reference.

Any help would be greatly appreciated by "One Less Bald Guy" :-)

Thanks-
Larry

=================================================
<root>
<item id="1">
<value name="fieldName" instance="1">Alpha</value>
<value name="fieldName" instance="2">Beta</value>
</item>
<item id="2">
<value name="fieldName" instance="1">Charlie</value>
</item>
<item id="3">
<value name="fieldName" instance="1">Delta</value>
<value name="fieldName" instance="2">Echo</value>
</item>
</root>


Sep 29 '05 #2
Peter- Thank you for the quick reply.
OK, what I was looking for was to return the nodes that were less than
count(item/value[@name='fieldName']) < $maxval. I was wondering if the
exslt set:leading() function might be of help here, but I do not have
enough experience with the sets.

Sep 29 '05 #3
Upon review, the 'best' solution would probably be to find the
position() of the node where the total number of preceding
value[@name='fieldName'] = $maxItems. THAT would be some cool XSL in my
world :)

Sep 29 '05 #4
Larry R wrote:
Peter- Thank you for the quick reply.
OK, what I was looking for was to return the nodes that were less than
count(item/value[@name='fieldName']) < $maxval.


But that is exactly what the XPath expression in my example does:

root/item
[sum(value/@instance)+
sum(preceding::value/@instance) &lt;= $maxval]

For your data, this nodeset contains the first two "item" element nodes.

If you wanted the "value" element nodes contained within them instead,
just add "/value" to the expression.

///Peter

Sep 29 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Reiche | last post by:
Question on XSL expression Got this XML: <Body> <Page> <Line no="9" detail="true"> <onefield>onefieldstext</onefield> <twofield>twofieldstext</twofield> </Line>
6
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. ...
8
by: Rich | last post by:
My table looks like this: char(150) HTTP_REF, char(250) HTTP_USER, char(150) REMOTE_ADDR, char(150) REMOTE_HOST, char(150) URL, smalldatetime TIME_STAMP There are no indexes on this table...
19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
3
by: someone | last post by:
Here is my code: TreeNodeCollection nodes = tvwFolders.Nodes; if (nodes.Count > 0) { // Select the root node tvwFolders.SelectedNode = nodes; } I hope there is an easier way. I looked at...
4
by: praveen | last post by:
I have a form with treeview control loaded from xml document,text box, two buttons named "Find" and "FindNext" and my treeview which looks like below. Details |__ policy status |__ created by...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
22
by: Rickster66 | last post by:
As Instructed this is a new thread regarding my original post: "Select Only 10 Columns Going Back" I'm sorry for the late response. I've been gathering up information and carefully with as much...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.