Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-05-21 17:55 GMT
Find Nodes?
    Voting Booth?