Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Node depth?

Status
Not open for further replies.

chiph

Programmer
Jun 9, 1999
9,878
US
Does anyone know of a better way to determine the depth of an XML node tree than iterating through the node list and keeping count?

TIA.
Chip H.
 
>> depth of an XML node tree

>> keeping count?

Which is it, depth or count?
 
Count of the depth. ;-)

In this case, I don't really care how many elements are in the document, just how deep they're nested. It has to do with some legacy code I've inherited [sadeyes], and making sure imported data doesn't exceed an artificial limit the original developers put in.

If there's no easy (or relatively easy) XPath query to do this (I've been going through the MS XML 4 docs), plan "B" would be load the document into a DOM and use the childNodes collection to find out how far it nests (nasty, but do-able).

Chip H.
 
Chip, check this out...

use "//*" and u will get back the root element - ONE level

use "//*/*" and u will get back the collection of children of root - TWO levels

... etc. until you get an empty NodeList

What do you think?

-pete
 
It's certainly a lot easier to code than plan "B" would be. Is it faster? Probably, since I can do a binary search (sort of) by doubling the number of levels each time until it fails, and then I can narrow down using a further binary search.

2^0 = //*/*
2^1 = //*/*/*
2^2 = //*/*/*/*/*
2^3 = //*/*/*/*/*/*/*/*/* (fails)

then do binary search between them:
//*/*/*/*/*/*/* (fails)
//*/*/*/*/*/* (succeeds)

so I know that the depth is 5

Thanks for the help.

Chip H.
 
try this:

count(ancestor::node())

which will give you a count of all ancester nodes including the root. much easier :)

matt
 
matt,

i understand how u can use that in a transformation to place that number in the output of the transformation, but...

when ur using a DOM, like Xerces, and u have ur Document object i don't undestand how u can use a XPath expression to return the count since it is a number not a Node object.

do you know how? -There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
The number returned from an xpath expression in Jaxen will be a Document/Element object.

There are evaluation functions supplied with JAXEN that will return the correct object from XPATH. eg
....
Document xml;
..
// load xml garbage
..

org.jaxen.XPath path;
org.jdom.Element element;
path = new org.jaxen.jdom.JDOMXPath("count(ancester:node())");

element = (org.jdom.Element)path.selectSingleNode(xml);

[either]

Number num = path.numberValueOf(element);

[or]

Integer integer = (Integer)path.evaluate(element);

[whichever you want :)]

I dont know what XPATH engine you're using so it may be different, but its basically the same principal...


its a bit tricky but hey. ;)


matt
 
thanks matt,

using xalan in Java. xpath is in org.apache.xpath.*

i have not dug that deep yet so i will do some research. u may have pointed me in a direction that i could not previously see.

also i have used MSXML in C++. now there i don't see any parallel functionality in this regard. perhaps it's time to switch to Xalan in C++ as well. -There are only 10 types of people in the world, those who understand binary and those who don't-

-pete
 
MSXML has its bonuses.. you can use the DOMDocumentObject model to perform transforms in xpath with out an additional xpath engine like JAXEN ... I did some MSXML but I cannot recall ever having to do something like this :)

Another way to do it would be to create a v. small stylesheet with this operation in it that creates another XML document with a result of that node in it, then select that nodes text value and parse it as an int or sommit..

I believe that if i ever had to do something like this i'd probably do what you were doing before, but nothing hurts in learning a new way eh?

glad to be of service :)

matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top