Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: XML::LibXML findnodes fails when using namespaces

by gellyfish (Monsignor)
on Jun 13, 2006 at 12:31 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://555018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-25 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found