Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to write an xpath query including attributes for an XML with namespace

by choroba (Cardinal)
on Sep 06, 2013 at 12:59 UTC ( [id://1052708]=note: print w/replies, xml ) Need Help??


in reply to How to write an xpath query including attributes for an XML with namespace

To use a namespace in an XPath expression, you have to register it first. The following works for me:
#!/usr/bin/perl use warnings; use strict; use XML::LibXML; my $file_dati = '/path/to/file.xml'; my $parser = 'XML::LibXML'->new(); my $doc = $parser->parse_file($file_dati) || die('dead1'); my $xc = 'XML::LibXML::XPathContext'->new($doc); $xc->registerNs('mw', 'http://myweb.com'); my $nodetobefound = '//mw:comment[@timestamp="20130903225513"]'; print "To be found: $nodetobefound\n"; my $commento = $xc->findnodes($nodetobefound) || die('dead3'); print $commento->get_node(0)->toString();

As Corion suggested, I changed the name of the element and added // for recursive search.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: How to write an xpath query including attributes for an XML with namespace
by topola1882 (Initiate) on Sep 06, 2013 at 13:46 UTC

    Thanks Choroba, it worked!!

Re^2: How to write an xpath query including attributes for an XML with namespace
by Anonymous Monk on Sep 07, 2013 at 07:40 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-19 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found