Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by choroba (Abbot)
on Sep 06, 2013 at 12:59 UTC ( #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.

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


Comment on Re: How to write an xpath query including attributes for an XML with namespace
Download Code
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
Node Status?
node history
Node Type: note [id://1052708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (16)
As of 2014-10-30 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls