stylechief has asked for the wisdom of the Perl Monks concerning the following question:
Greetings Honorable Monks,
I need to replace certain <tm> elements in an XML document with the value of the <tm> element. That is, I need to eliminate the element but retain its content in-place.
For example, If I find: <tm tmclass="ibm" tmowner="IBM Corporation" tmtype="reg" trademark="AIX">AIX</tm>;
I need to replace the entire <tm>...</tm> element with the text AIX.I am using XML::LibXML to detect the @tmowner, and trigger the above function. For example:
use strict; use File::Find; use XML::LibXML; my ($file, $parser, $doc, $query, $node, $val, $tmtext); @ARGV=('.'); # search every file in this dir and all subdirs find (\&search, @ARGV); sub search{ $file=$_; if (grep -f && /\.xml$/i, $file){ $parser = XML::LibXML->new(); $doc = $parser->parse_file($file); $query = "//tm"; foreach $node ($doc->findnodes($query)){ $val = $node -> findvalue('@tmowner'); $tmtext = $node -> textContent(); # if we don't own the trademark, replace it with the text if ($val !~ /my_company/i){ ### REPLACE THE TM ELEMENT WITH THE TEXT CONTENT ### print " Replacing trademark element with $tmtext\n"; } } # foreach node } # if grep } # sub search
I have not had any luck with the replaceNode or replaceChild methods for this purpose. I know I could do this with regexps, but that might get messy with all of the preformatting necessary to manage multiple trademarks on one line.
Tips and ideas are appreciated. Thanks for your time.
|
|---|