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

I am coding with Perl on a Window 7 machine. I am able to extract data from the XML using the XPath code below

use strict; use warning; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($newfile); my $query = "/tradenet/message/header/unique_ref_no/date/ +text( )"; my($node) = $doc->findnodes($query); $node->setData("$file_seq_number");

However, when i use the same code on a different XML, the xpath from the second document looks as below:

    /TradenetResponse/OutboundMessage/out:OutwardPermit/out:Declaration/out:Header/cac:UniqueReferenceNumber/cbc:SequenceNumeric

Together with the Perl code, this is what the extraction code looks like:

my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($newfile); my $query = "/TradenetResponse/OutboundMessage/out:OutwardPer +mit/out:Declaration/out:Header/cac:UniqueReferenceNumber/cbc:Sequence +Numeric/text( )"; my($node) = $doc->findnodes($query); $node->setData("$file_seq_number");

Using the second code, I am unable to retrieve the data from the second XML. I receive this error "Can't call method "setData"on an undefined value at Perl.pl line 5". Does the ":" character in the second XPATH affecting the code?

thanking you all in advance for any advise or help that you may render. Have a nice day!

Replies are listed 'Best First'.
Re: Using PERL to extract XPATH with ":" character
by choroba (Cardinal) on Aug 22, 2012 at 08:59 UTC
    Crossposted at StackOverflow. It is considered polite to inform about crossposting so people not attending the other site do not waste their time on solving a problem already solved at the other end of the internet.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Using PERL to extract XPATH with ":" character
by Anonymous Monk on Aug 22, 2012 at 09:21 UTC

    In addition to registering namespace (the answer from stackoverflow ) you can also match using

    /TradenetResponse/OutboundMessage/ *[ name ="OutwardPermit" ]/*[ name = "Declaration" ]...

    However powerful xml/xpath are, they are just as dumb, maybe even twice that