Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Replace string in one file using input from another file

by Jenda (Abbot)
on Feb 08, 2018 at 23:25 UTC ( [id://1208764]=note: print w/replies, xml ) Need Help??


in reply to Replace string in one file using input from another file

I have to repeat what the previous node already said, DO NOT TREAT XML FILES AS TEXT. Use a library that actually understands the format.

In this case one of the options is XML::Rules in the filtering mode. You would read the CSV into a hash and then process the XML file with something like

use XML::Rules; my $filter = XML::Rules->new(style => 'filter', rules => { 'XREF' => sub { return $references{$_[1]->{_content}} ?? "Unknown reference $_[1]- +>{_content}"; } }); $filter->filterfile($source_path, $result_path);
If you do not want to process all <XREF> tags, but only those within <LinkValue> you can change the code to something like this:
my $filter = XML::Rules->new(style => 'filter', rules => { 'XREF' => { qr{/LinkValue$} => sub { return $references{$_[1]->{_content}} ?? "Unknown reference $_[ +1]->{_content}"; } # or # qr{/Property/LinkValue$} => sub { # return $references{$_[1]->{_content}} ?? "Unknown reference $_[ +1]->{_content}"; # } # for only <Property><LinkValue><XREF> } });

Jenda
Enoch was right!
Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found