Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

by Jenda (Abbot)
on Feb 08, 2018 at 23:25 UTC ( #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> } });

Enoch was right!
Enjoy the last years of Rome.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-08-16 12:40 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (167 votes). Check out past polls.