473,396 Members | 1,938 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,396 software developers and data experts.

Node Counting Problems

I've spent quite a bit of time (way more than I'd like) today looking
for an answer to what I thought would be a simple question. I simply
need to get the count of items within some nodes up to the current node
(previous or previous-sibling related). For example, here is some
sample XML I have to work with:

<ITEM>
<CHILD>
<ITEM>
<CHILD />
<CHILD />
</ITEM>
<ITEM>
<CHILD />
<CHILD />
<CHILD />
</ITEM>
<ITEM>
<CHILD /> <<-- current node
</ITEM>
<ITEM>
<CHILD />
</ITEM>
</CHILD>
</ITEM>

Let's say the current node is as noted above. How do I get the count of
the previous CHILD nodes? In this case the number I'm looking for is 5.
Any help would be greatly appreciated!

Jul 20 '05 #1
15 3592

count(preceding-sibling::CHILD) + count(../preceding-sibling::ITEM/CHILD)

David
Jul 20 '05 #2
count(preceding::CHILD)
Jul 20 '05 #3
Hey,

Serious thanks for the fast reply. For some reason
"count(../preceding-sibling::ITEM/CHILD)" returns 0 and my count starts
over every time the template executes. Also, will this example work
only if the current node was the 3rd node or will it work at anytime in
the set?

Jul 20 '05 #4
"count(../preceding-sibling::ITEM/CHILD)" returns 0 ` Then your structure isn't as you showed.
Also, will this example work only if the current node was the 3rd node o

as you can see there is nothing special about 3 in teh expression it
will work on any node.

Your description was a little vague, I see someone else suggested
count(preceding::CHILD)
which might also be what you want (although it won't in general give the
same answer as the expression I gave).

David
Jul 20 '05 #5
Hi David,

You are correct in that I provided a slightly incorrect XML example.
Sorry. Your example works perfectly with the sample I originally
posted. For some reason I can't seem to get it working on my actual XML
though. I tried playing around with the statement but I still can't get
it working. Can you please help me with the same statement for the
following XML?

<?xml version="1.0" encoding="UTF-8"?>
<MENU>
<ITEM>
<MENU>
<ITEM />
<ITEM />
<ITEM />
<ITEM />
</MENU>
</ITEM>
<ITEM>
<MENU>
<ITEM />
<ITEM />
<ITEM />
</MENU>
</ITEM>
<ITEM>
<MENU>
<ITEM />
</MENU>
</ITEM>
<ITEM>
<MENU>
<ITEM />
<ITEM />
<ITEM />
<ITEM />
</MENU>
</ITEM>
</MENU>

Jul 20 '05 #6
Hi David,

You are correct in that I provided a slightly incorrect XML example.
Sorry. Your example works perfectly with the sample I originally
posted. For some reason I can't seem to get it working on my actual XML
though. I tried playing around with the statement but I still can't get
it working. Can you please help me with the same statement for the
following XML?

<?xml version="1.0" encoding="UTF-8"?>
<ITEM>
<CHILD>
<ITEM>
<CHILD />
<CHILD />
<CHILD />
<CHILD />
</ITEM>
</CHILD>
<CHILD>
<ITEM>
<CHILD />
<CHILD />
<CHILD />
</ITEM>
</CHILD>
<CHILD>
<ITEM>
<CHILD />
</ITEM>
</CHILD>
<CHILD>
<ITEM>
<CHILD />
<CHILD />
<CHILD />
<CHILD />
</ITEM>
</CHILD>
</ITEM>

Jul 20 '05 #7
Hi Paul,

For some reason this returns an extremely large number. Maybe because
there are parent nodes named the same?

Jul 20 '05 #8
<ITEM>
<MENU>
<ITEM />
<ITEM />
<ITEM />
<ITEM />
</MENU>
</ITEM>
<ITEM>
<MENU>
<ITEM />
<ITEM />
<ITEM />
</MENU>
</ITEM>
<ITEM>

so, the ITEM's you want to count (the leaf ones) are second cousins not
first cousins:
count(../preceding-sibling::ITEM) + count(../../preceding-sibling::ITEM/MENU/ITEM)
Or if all this going up and down is too tiresome (and you are doing this
in xslt not in some Xpath-API) you could just use

<xsl:number level="any" count="ITEM[not(*)]"/>

which will generate a running count of all the leaf ITEM nodes to the
current position.
David
Jul 20 '05 #9
David,

count(../preceding-sibling::ITEM) +
count(../../preceding-sibling::ITEM/MENU/ITEM)

Perfect! Thanks so much! One last question:

Let's say I would now like to count the same ITEM nodes, but this time
only count the ITEM nodes that have MENU/ITEM children themselves. I
tried this and it doesn't work:

count(../preceding-sibling::ITEM/MENU/ITEM) +
count(../../preceding-sibling::ITEM/MENU/ITEM/MENU/ITEM)

Again, thanks so much for your help!

Jul 20 '05 #10

Let's say I would now like to count the same ITEM nodes, but this time
only count the ITEM nodes that have MENU/ITEM children themselves. I
tried this and it doesn't work:

count(../preceding-sibling::ITEM/MENU/ITEM) +
count(../../preceding-sibling::ITEM/MENU/ITEM/MENU/ITEM)
/ selects (using the child axis) so
.../preceding-sibling::ITEM/MENU/ITEM
is not the number of items that have ITEM grandchildren, it is the
number of ITEM grandchildren.

"that have" is written "[" in Xpath so you want

count(../preceding-sibling::ITEM[MENU/ITEM]) +
count(../../preceding-sibling::ITEM/MENU[ITEM/MENU/ITEM])

David
Jul 20 '05 #11
Hmmm, it returns the same number as before, which doesn't make sense.
It seems as if your example should work, but for some reason it
doesn't take into account the [MENU/ITEM], it just counts the ITEM
nodes again.

Any ideas?

Jul 20 '05 #12
Here is example XML for this problem:

<MENU>
<ITEM>
<MENU>
<ITEM>
<MENU>
<ITEM/>
<ITEM/>
<ITEM/>
</MENU>
</ITEM>
<ITEM/>
<ITEM>
<MENU>
<ITEM/>
<ITEM/>
</MENU>
</ITEM>
<ITEM/> <<--current node
</MENU>
</ITEM>
</MENU>

So from the current node, I'd want it to return 2, meaning 2 ITEM nodes
before it contained MENU/ITEM...

Jul 20 '05 #13

t seems as if your example should work, but for some reason it
doesn't take into account the [MENU/ITEM], it just counts the ITEM
nodes again.

Any ideas?
the nodes selected by foo and foo[MENU/ITEM] will be different (and so
the count() will be different) unless all the foo nodes have MENU/ITEM
children so if you don't see a difference, you didn't do what you think
you did.

David
Jul 20 '05 #14
David,

Exactly right. I got it working. Thanks a ton for the help!

Chris

Jul 20 '05 #15
Good point. Whoops.
Jul 20 '05 #16

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

Similar topics

1
by: gotdough | last post by:
Cluster services gives the high availability needed - that is great. But I have never seen any discussion about what happens when a node fails - what do you do to get everything back to the...
2
by: fremenusul | last post by:
I orginally asked this question on friday, and made some progress, but now I am getting an error. What is the type of xmlProductName (check the runtime type in the debugger)? The debug output...
0
by: Kalle Rutanen | last post by:
Hello I implemented reference counting in my program, and found out many problems associated with it. I wonder if the following problems can be solved automatically rather manually ? 1. ...
6
by: Eddy C | last post by:
Hi, I'm trying to get the value of another node using the position of another node or the name of the tag. Such that the current node is one of the contacts child nodes sec or prim and doing...
8
by: chris yoker via DotNetMonster.com | last post by:
hiya, I succesfully return a "nodeList" thru the "xmlDoc.SelectNodes" method. This nodeList is taken from a repetitive, uniform xml doc. I can successfully append a child node at the correct...
18
by: ChadDiesel | last post by:
I appreciate the help on this group. I know I've posted a lot here the last couple of weeks, but I was thrown into a database project at my work with very little Access experience. No other...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
2
by: mathon | last post by:
hello, i thought i create a new topic for that because i deal with a certain problem. I have implemented all methods for a LinkdeList (for my sequence class) and tested all correct. Now i only...
1
by: ahoway | last post by:
I am having problems deleting a node from a link list. I need to delete the node which contains the number six. This is what I have so far..... Thank you in advance. #include <iostream>...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.