Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > query the depth of the lowest node?

Reply
Thread Tools

query the depth of the lowest node?

 
 
Phlip
Guest
Posts: n/a
 
      03-26-2009
xmlists:

Here's a chestnut from graph theory:

What's the minimum XPath(s) - hence the most efficient way - to query the depth
of the lowest node?

All I can think of is calling count(ancestor::*) on every node...

--
Phlip
http://www.zeroplayer.com/
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-26-2009
Phlip wrote:

> What's the minimum XPath(s) - hence the most efficient way - to query
> the depth of the lowest node?
>
> All I can think of is calling count(ancestor::*) on every node...


With XQuery 1.0 or XPath 2.0 you can do
max(for $el in descendant::* return count($el/ancestor-or-self::*))
That assumes you are interested in element nodes but could obviously be
changed to select other node types.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-26-2009
Martin Honnen wrote:
> Phlip wrote:
>
>> What's the minimum XPath(s) - hence the most efficient way - to query
>> the depth of the lowest node?
>>
>> All I can think of is calling count(ancestor::*) on every node...

>
> With XQuery 1.0 or XPath 2.0 you can do
> max(for $el in descendant::* return count($el/ancestor-or-self::*))


max(for $el in descendant::*[not(*)] return count($el/ancestor-or-self::*))

should be slightly more efficient.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-26-2009
> max(for $el in descendant::*[not(*)] return count($el/ancestor-or-self::*))

While I go smack around the author of Nokogiri to let me return a count instead
of a nodeset...

Does the extant libxml2 do XPath 2.0? And can anyone think of a version that
returns a nodeset?

Until then, I will just use 'descendant::*[not(*)]' to get a nodeset with each
leaf node, then use 'count(ancestor-or-self::*)'. That's better than ON time,
because it's just On where n is the number of leaves...
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      03-27-2009
> Until then, I will just use 'descendant::*[not(*)]' to get a nodeset
> with each leaf node, then use 'count(ancestor-or-self::*)'. That's
> better than ON time, because it's just On where n is the number of
> leaves...


def maximum_depth
@builder.doc.xpath('descendant::*[not(*)]').
map{|e| e.xpath('ancestor-or-self::*').length }.
sort.last
end

Yup worked like a charm the first time. Thanks, guys!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why are Microsoft-trained people the lowest-paid or unemployed? Microsoft Certification 3 05-14-2004 07:03 AM
New Zealand, on the other hand, has one of the lowest growth rates and one of the lowest levels of broadband penetration in the world. punlic NZ Computing 6 05-02-2004 10:28 AM
Re: Website for lowest fare by region? Imya Rek Cisco 0 02-16-2004 01:48 AM
Lowest cost Cisco router? Marv Cisco 15 12-17-2003 09:27 PM
Lowest series Catalyst to do layer 3 routing? Illusion Cisco 2 07-09-2003 05:23 PM



Advertisments