Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Replacing a text in XML.

by toolic (Bishop)
on Apr 05, 2013 at 16:26 UTC ( #1027165=note: print w/ replies, xml ) Need Help??

in reply to Replacing a text in XML.

I can see 2 problems. Your XML does not have an element named "href". You need to change elements named "subfield". Your regex is incorrect:

my $twig = XML::Twig->new( twig_handlers => { subfield => \&editHref, }, pretty_print => 'indented', ); $twig->parse($xml); $twig->print; sub editHref { my ( $twig, $href ) = @_; my $text = $href->text(); my $orig = "(OSt)16"; $text =~ s/\Q$orig/(OSt)20/; $href->set_text($text); }

Or, just avoid the regex...

sub editHref { my ( $twig, $href ) = @_; my $text = $href->text(); $href->set_text('(OSt)20') if $text eq '(OSt)16'; }

Comment on Re: Replacing a text in XML.
Select or Download Code
Replies are listed 'Best First'.
Re^2: Replacing a text in XML.
by bendon (Novice) on May 21, 2013 at 15:36 UTC
    Thanks for the reply, Also I have another question to ask, If I want to search through the same XML file for this subfield. <subfield code="0">(OSt)18</subfield> from the database how do I go by it? Thanks.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2015-10-10 13:19 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (257 votes), past polls