As usually, TIMTOWTDI. For example, you can pre-hash the names from the CSV by the coordinates, then process the XML node by node picking the new name from the hash:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Text::CSV_XS; use XML::LibXML; my %name; my $csv = 'Text::CSV_XS'->new({allow_whitespace => 1, auto_diag => 1, binary => 1}); open my $in, '<:encoding(UTF-8)', 'test.csv' or die $!; $csv->header($in); while (my $row = $csv->getline($in)) { $name{$row->[0]}{$row->[1]} = $row->[2]; } my $dom = 'XML::LibXML'->load_xml(location => 'test.osm'); for my $node ($dom->findnodes('/osm/node')) { my $tag = $node->findnodes('tag[@k="name"]')->[0]; if (my $name = $name{ $node->{lat} }{ $node->{lon} }) { $tag->{v} = $name; } else { warn "No name found for $tag->{v}: @$node{qw{ lat lon }}\n"; } } $dom->toFile('new.osm');

Or, you can load the XML into a DOM, then read the CSV line by line, searching the DOM for the corresponding node and renaming it:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Text::CSV_XS; use XML::LibXML; my $dom = 'XML::LibXML'->load_xml(location => 'test.osm'); my $csv = 'Text::CSV_XS'->new({allow_whitespace => 1, auto_diag => 1, binary => 1}); open my $in, '<:encoding(UTF-8)', 'test.csv' or die $!; $csv->header($in); my $xpath_template = '/osm/node[@lat="%"][@lon="%"]'; while (my $row = $csv->getline($in)) { my $i = 0; my $xpath = $xpath_template =~ s/%/$row->[$i++]/gr; my $node = $dom->findnodes($xpath)->[0]; if ($node) { my $tag = $node->findnodes('tag[@k="name"]')->[0]; $tag->{v} = $row->[2]; } else { warn "@$row missing in XML\n"; } } $dom->toFile('new.osm');

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: Update XML Values using two primary keys by choroba
in thread Update XML Values using two primary keys by pratikpooja

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.