Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: LibXML, Namespaces, xpath expression - This should be simple.

by FalseVinylShrub (Chaplain)
on Sep 03, 2011 at 15:59 UTC ( #924006=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: LibXML, Namespaces, xpath expression - This should be simple.
Select or Download Code
Re^2: LibXML, Namespaces, xpath expression - This should be simple.
by ikegami (Pope) on Sep 03, 2011 at 16:02 UTC

    Creating a new xpc for every findnodes is not the way to go.

    my $inner_xpc = XML::LibXML::XPathContext->new($nodeybits); $inner_xpc->registerNs( theNS => 'http://www.wow.com/BlahML'); $inner_xpc->findnodes(...)

    can be written as

    $xpc->findnodes(..., $nodeybits)
      Hi All, Thank-you so much for your help with this.

      I apologise for the time taken to reply but I had assumed the site would email me by default if I got a reply..and as I hadn't, assumed no one had responded. (Possible server issue my end)

      Setting up the namespace in the way shown worked perfectly. This site should show PayPal options for each submit. I'd happily send each Monk a dollar or two for the mental sanity you afforded me.

      Thank you so much.

      PS: Matt Seargent was right. LibXML is super quick when used properly.

      • My Data Pool using XPath - Parsing took 3 hours
      • My Data Pool using LibXML - Parsing takes 20 minutes. - Fantastic!

        PerlMonks doesn't send emails.

        Unless it's changed, PerlMonks has no expenses — hosting is donated by Pair — so receiving money would actually be a problem. On the other hand, the Perl Foundation does take donations, I believe. Some of that money is going towards fixing the more complicated bugs in Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://924006]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-11-27 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (187 votes), past polls