Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

String substituion with XML::Twig

by slugger415 (Monk)
on Jul 26, 2014 at 01:12 UTC ( [id://1095127]=perlquestion: print w/replies, xml ) Need Help??

slugger415 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, still trying to learn XML::Twig, in this case string substitution.

If I have a block of XML, e.g.:

<p>String AAA <b>varname</b> and string AAA and BBB</p>

How do I go about replacing string AAA without clobbering the b element? I know I can do this (while evaluating the p element):

my $sub = $elt->text(); $sub =~ s/AAA/ZZZ/g; $elt->set_text($sub);

But that will remove the inner element:

String ZZZ varname and string ZZZ and BBB

I've looked at getting children and descendants but nothing seems to give me just the text elements that I can manipulate separately.

Sorry if its a naive question, and thanks for the advice. Scott

Replies are listed 'Best First'.
Re: String substituion with XML::Twig
by poj (Abbot) on Jul 26, 2014 at 05:48 UTC
    Try $elt->subs_text( qr'AAA','ZZZ' );
    poj

      wonderful, thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1095127]
Approved by kevbot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-03-29 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found