Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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'; }

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-02-24 04:44 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.