I will let mirod handle the XML::Twig version, as i have not progressed to that module yet. I recently bought Perl & XML and am enjoying it immensely. Here is an 'event stream' version that uses XML::Parser and XML::Writer to replace all <foo> elements with the element <struggle>. Input is from the DATA file handle and output is STDOUT:
use strict; use XML::Parser; use XML::Writer; my $writer = XML::Writer->new(); my $parser = XML::Parser->new( Handlers => { Init => \&handle_Init, Start => \&handle_Start, Char => \&handle_Char, End => \&handle_End, Final => \&handle_Final, } ); # i could have also made these $parser attributes # such as $parser->{from} and $parser->{to} our $from = 'foo'; our $to = 'struggle'; my $data = do {local $/;<DATA>}; $parser->parse($data); # called once at the beginning sub handle_Init { $writer->xmlDecl('UTF-8'); $writer->doctype('xml'); } # called each time a start element is encountered sub handle_Start { my($self,$name,%atts) = @_; $name = $to if $name eq $from; $writer->startTag($name,%atts); } # called each time non-markup data is encountered sub handle_Char { my($self,$text) = @_; $writer->characters($text); } # called each time an end element is encountered sub handle_End { my($self,$name) = @_; $name = $to if $name eq $from; $writer->endTag($name); } # called once at the end of the document sub handle_Final { $writer->end(); } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <xml> <foo class="life or death"> <opponent>wolf</opponent> <opponent>ant</opponent> </foo> <foo class="life or death"> <opponent>pantomime goose</opponent> <opponent>Terrance Rattigan</opponent> </foo> </xml>

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: XML Search and Replace by jeffa
in thread XML Search and Replace by coreolyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.