I was trying to edit an existing XML file based on the node attribute name value.
For example:
<books>
<book name = 'Pride and Prejudice' value='not available' price='10'/>
<book name = 'To kill a Mockingbird' value='available' price='8'/>
<book name = 'The Face of a Stranger' value='not available' price='16'/>
</books>
The goal is if name attribute value matches then change the value attribute. For example, if the name "Pride and Prejudice" matches the search then change the value attribute to “available”.
The output should look like:
<books>
<book name = 'Pride and Prejudice' value='available' price='10'/>
<book name = 'To kill a Mockingbird' value='available' price='8'/>
<book name = 'The Face of a Stranger' value='not available' price='16'/>
</books>
I was able to read the XML file and match the name values but couldn’t figure out how to change the attribute value.
Here is my code:
my $xml_data = XML::LibXML->load_xml(location => a.xml);
for my $nodes ( $xml_data->findnodes('/Books/Book') ) {
my $att_name = $nodes->findvalue('@name');
my $att_val = $nodes->findvalue('@value');
if ( $searchString eq $att_name ) {
<how do I change the attribute value?>
}
}