in reply to XML::XPath Question

->findnodes() returns an XML::XPath::NodeSet, not a (Perl) list of nodes.

See ->get_nodelist of that object.

Replies are listed 'Best First'.
Re^2: XML::XPath Question
by ikegami (Patriarch) on Nov 10, 2010 at 20:26 UTC

    That's not what the docs say, and that's not what the error message says. (It mentions "Node", not "NodeSet".)

    The error message is actually spot on. The OP is trying to call the "exists" method of a node, but nodes don't have an "exists" method. The correct usage is

    $tree->exists('path', $node)

      I was looking at <a href="http://search.cpan.org/~msergeant/XML-XPath-1.13/XPath.pm#findnodes%28$path,_$context%29">->findnodes, and there it says that.

      But I remember hours of staring at various parts of the documentation, trying to piece together a whole, so there might be other parts that say something else.

        I don't get it.. a tree is still basically a node, and vice versa... what sets them apart?

      I'm sorry, but I don't get it, but a node is a tree, and vice-versa.. what is the distinction?

Re^2: XML::XPath Question
by TendulkarIsGod (Acolyte) on Nov 10, 2010 at 20:29 UTC

    Actually, another question.. the doc says that findnodes

    "Returns a list of nodes found by $path, optionally in context $context. In scalar context returns an XML::XPath::NodeSet object."

    I had taken that to mean that since I am invoking the method inside the braces for a foreach loop

    foreach my $node ($tree->findnodes())

    It would return a list of nodes.. Isn't that a list contest? Why would it be a scalar context?

      Indeed, it should return the list of nodes in your case. I think I was just barking up the wrong tree, sorry.

Re^2: XML::XPath Question
by TendulkarIsGod (Acolyte) on Nov 10, 2010 at 20:22 UTC

    thats what i thought too, and tried these changes:

    foreach my $node($tree->findnodes('path')->get_nodelist(){ if ($node->exists('path')){

    and alternatively

    my $node_list = $tree->find('path'); foreach my $node($node_list->get_nodelist()){ if ($node->exists('path')){

    I still get the same error..