I have an incomplete Perl script with the following content:
use XML::LibXML;
use XML::LibXSLT;
my $xml_mess = "
<activity>
<ref1></ref1>
<ref2>id_119604</ref2>
<ref3>id_342432</ref3>
</activity>";
my $parser = XML::LibXML->new();
my $xml_mess_obj = $parser -> parse_string($xml_mess);
my $ref1 = $xml_mess_obj -> getDocumentElement -> findNodes("/activity/ref1") -> [0] -> to_literal();
my $ref2 = $xml_mess_obj -> getDocumentElement -> findNodes("/activity/ref2") -> [0] -> to_literal();
my $ref3 = $xml_mess_obj -> getDocumentElement -> findNodes("/activity/ref3") -> [0] -> to_literal();
I would like to parse the $xml_mess and make the following changes:
- When
ref3has a value, then I wantref1to have the same value. - When
ref3does not have a value, then I wantref1to have the same value asref2.
I have been searching for examples on how to do this with LibXML, but can't figure out how to conditionally update nodes.
-> getDocumentElementisn't necessary. Document elements providefindNodes-> findNodes(...) -> [0] -> to_literal()can be replaced with-> findvalue(...)