Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2015-07-01 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (22 votes), past polls