http://www.perlmonks.org?node_id=1052708


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.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re: How to write an xpath query including attributes for an XML with namespace
  • Download Code

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