Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

XPATH problems

by gam3 (Curate)
on Jan 09, 2010 at 03:40 UTC ( #816445=perlquestion: print w/ replies, xml ) Need Help??
gam3 has asked for the wisdom of the Perl Monks concerning the following question:

I have spent a few hours trying to write a program in perl that will find a node in one file using XPath and then find a node in a second file using an XPath. Then replace the first node with the second and write out the modified file.

I did not see how to replace the node with XML::XPath and I could not get XML::Twig to find my paths. I gave up at one point and wrote it in ruby:

#!/usr/bin/ruby -w require 'rexml/document' include REXML xmlfile1 = File.new("wiki.html") xmldoc1 = Document.new(xmlfile1) xmlfile2 = File.new("timeline.html") xmldoc2 = Document.new(xmlfile2) node1 = XPath.first(xmldoc1, "//div[@id=\"metanav\"]") node2 = XPath.first(xmldoc2, "//div[@id=\"metanav\"]") node1.replace_with(node2) print xmldoc1.to_s;
This is a bit slow but works and was easy to find and get to work.

Maybe there are just too many XML packages available in Perl and that makes it hard to find the right one.

Anyway I would love to find a solution to this problem with a perl package.

Thanks in advance.

-- gam3
A picture is worth a thousand words, but takes 200K.

Comment on XPATH problems
Download Code
Re: XPATH problems
by ikegami (Pope) on Jan 09, 2010 at 04:24 UTC
    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);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://816445]
Front-paged by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-10-22 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (113 votes), past polls