Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: XML::LibXML findnodes fails when using namespaces

by gellyfish (Monsignor)
on Jun 13, 2006 at 12:31 UTC ( #555018=note: print w/replies, xml ) Need Help??


in reply to XML::LibXML findnodes fails when using namespaces

You need to register the namespace, this is made easy with XML::LibXML::XPathContext:

use XML::LibXML; use XML::LibXML::XPathContext; + my $xml = q{<?xml version="1.0" encoding="iso-8859-1"?> <Tag1 xmlns="urn:wow"> <Foo>content</Foo> </Tag1> }; + + + my $doc = XML::LibXML->new->parse_string($xml); my $xc = XML::LibXML::XPathContext->new($doc); $xc->registerNs('foo', 'urn:wow'); + my ($foo) = $xc->findnodes('//foo:Tag1/foo:Foo'); + print "Foo in xml: ".$foo->textContent()."\n";
You can register any arbitrary prefix to your namespace URI to use in your XPath expression but note I have changed your namespace to an absolute URI as you get an error from XML::LibXML otherwise.

/J\

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2021-06-16 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (74 votes). Check out past polls.

    Notices?