omegaweaponZ has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE: This has been resolved. Thanks @choroba

I have an issue when trying to parse out child nodes from an XML using XPath Context. I can get attributes fine but it seems to have issues drilling down to grab the child nodes. Can someone explain why? Here's an example:
XML:
<parent_node xmlns="my_context"> <wrapper att1="abc"> <childnodeA>stuff</childnodeA> </wrapper> <wrapper att1="abcd"> <childnodeA>stuff2</childnodeA> </wrapper> </parent_node>

Code:
$xpath = XML::LibXML::XPathContext->new($xml->documentElement); $xpath->registerNs('x', 'my_context'); foreach $key($xpath->findnodes('x:wrapper')) { $att1 = $key->getAttribute('att1'); $childnodeA = $key->findnodes('./childnodeA'); }

Replies are listed 'Best First'.
Re: XML::LibXML::XPathContext and Child Nodes
by choroba (Cardinal) on Aug 27, 2014 at 13:15 UTC
    The childnodes still live in the x: namespace. You have to find them in the same way you find the wrapper, but specify the context node to be $key:
    my $childnodeA = $xpath->findnodes('x:childnodeA', $key);
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Update: Thanks, nevermind, I found the error in my code. Was referencing something prior that conflicted.
      Thanks again!