Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: XPATH problems

by ikegami (Pope)
on Jan 09, 2010 at 04:24 UTC ( #816446=note: print w/ replies, xml ) Need Help??


in reply to XPATH problems

use strict; use warnings; use XML::LibXML; my $doc1 = XML::LibXML->new->parse_file('wiki.html'); my $doc2 = XML::LibXML->new->parse_file('timeline.html'); my ($node1) = $doc1->findnodes('//div[@id="metanav"]'); my ($node2) = $doc2->findnodes('//div[@id="metanav"]'); $node1->replaceNode($node2); print $doc1->toString(0);

But the XPath you are using looks wrong. Don't you want to match {http://www.w3.org/1999/xhtml}div elements (i.e. XHTML div elements), not div elements with no namespace?

use strict; use warnings; use XML::LibXML; my $xpc = XML::LibXML::XPathContext->new; $xpc->registerNs('h', 'http://www.w3.org/1999/xhtml'); my $doc1 = XML::LibXML->new->parse_file('wiki.html'); my $doc2 = XML::LibXML->new->parse_file('timeline.html'); my ($node1) = $xpc->findnodes('//h:div[@id="metanav"]', $doc1); my ($node2) = $xpc->findnodes('//h:div[@id="metanav"]', $doc2); $node1->replaceNode($node2); print $doc1->toString(0);


Comment on Re: XPATH problems
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-08-29 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (288 votes), past polls