query the depth of the lowest node?

Discussion in 'XML' started by Phlip, Mar 26, 2009.

  1. Phlip

    Phlip Guest

    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/
    Phlip, Mar 26, 2009
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Mar 26, 2009
    #2
    1. Advertising

  3. 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/
    Martin Honnen, Mar 26, 2009
    #3
  4. Phlip

    Phlip Guest

    > 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...
    Phlip, Mar 26, 2009
    #4
  5. Phlip

    Phlip Guest

    > 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!
    Phlip, Mar 27, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,448
  2. Replies:
    1
    Views:
    357
    dar7yl
    Mar 11, 2005
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,423
    Dimitre Novatchev
    Aug 24, 2006
  4. datamodel
    Replies:
    6
    Views:
    683
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=
    Jan 16, 2007
  5. Jason Shohet

    want a treeview that only postbacks on lowest node

    Jason Shohet, Dec 15, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    93
    Jason Shohet
    Dec 15, 2003
Loading...

Share This Page