in reply to LibXML, Namespaces, xpath expression - This should be simple.
Hi
There was something wrong with you example XML, the MyImportantNode elements were closed before defining the attributes.
Assuming that was an error in posting, I think that your confusion comes from a number of reasons:
In 1 it is something to do with the way you are chaining findnodes and string_value. If you replace it with:
say "[1a]" . $nodeybits->findvalue('//@GeneralID');
you'll get Random1Random2Random3. If you want to deal with each value, you'll need a loop:
say "[1b]" . $_->findvalue('.') foreach $nodeybits->findnodes('*/@GeneralID');
Having said that, it's a bit confusing that you're using nodeybits (the MiddleTag node) but then running XPath expressions beginning with "//", which will start at the root.
2 does what I'd expect, given the above: the first tag that matches that will be the root element, and calling string_value on that will return the entire text of the file.
The remaining ones are all because you're using $nodeybits. This is not an XPathContext object. For the examples you've given, you could use $xpc. But presumably this is cut down from a bigger program where you're doing XPath relative to the node you're looking at in the loop.
EDIT see ikegami's reply
my $inner_xpc = XML::LibXML::XPathContext->new($nodeybits); $inner_xpc->registerNs( theNS => 'http://www.wow.com/BlahML'); # [3] print $inner_xpc->findnodes('//theNS:CannotGetTagA')->string_value . " +\n";
Worked for me and you could use $inner_xpc for your remaining queries, being careful to be consistent about using the namespace prefix (in 5, you have theNS:RootTag but miss it off for all the other element names).
HTH
FalseVinylShrub
Disclaimer: Please review and test code, and use at your own risk... If I answer a question, I would like to hear if and how you solved your problem.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: LibXML, Namespaces, xpath expression - This should be simple.
by ikegami (Patriarch) on Sep 03, 2011 at 16:02 UTC | |
by alittlebitdifferent (Initiate) on Oct 26, 2011 at 02:20 UTC | |
by ikegami (Patriarch) on Oct 26, 2011 at 06:56 UTC |