http://qs321.pair.com?node_id=1142804


in reply to Want to fetch inner most child element first

Welcom dharan,

And the code that you are having a problem with is where?

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

  • Comment on Re: Want to fetch inner most child element first

Replies are listed 'Best First'.
Re^2: Want to fetch inner most child element first
by dharan (Initiate) on Sep 23, 2015 at 13:39 UTC
    Thanks flex for swift reply, below is the code and not sure how to proceed further.
    use XML::XPath; my $xp = XML::XPath->new(filename => 'mathml.txt'); my $nodeset = $xp->find('/mml:math/*'); foreach my $node ($nodeset->get_nodelist) { $l = XML::XPath::XMLParser::as_string($node), }

      The XPath expression that you have written merely matches the outermost node.   What you might want to use here are axes.

      Try something like this:   (extemporaneous answer, check it yourself)

      //xml:msub[count(descendant::xml:msub) = 0] to retrieve all msubs who do not have any descendants of the same type.   These must be the leaves of the structure.

      Now, for each of these, perform a separate query for ancestor::xml:msub which will return an ordered list (from closest to farthest, or is it the other way around?) of all ancestors back to the top of the tree.