I have this XML file and I need to access specific nodes one at a time. Below is a sample of my XML along with my sample code.
My code is working fine except that I loop through all of the Message/Content tags instead of just getting the specific Message/Content tag under the current Message tag. For example, I would get back 3 Message/Content tags when the current Message tag is being processed (the one with refid="123991123") when I only want 1 returned (). Hope this is making sense. Any help here would be appreciated.
Code:
my $twig = XML::Twig->new(
twig_handlers => {
Selection => sub {
foreach my $message ($_->findnodes('./Contents/Message')) {
if($message->att('custom')){
$Message_custom = $message->att('custom');
foreach my $Content ($_->findnodes('./Contents/Message/Content')) {
print $Selection_id.": ".$Message_refid.": ".$TotalContents++."\n";
if($Content->att('language') eq "en"){
if($Content->att('imagelibraryid')){
$Message_Content_language_en_imagelibraryid = $Content->att('imagelibraryid');
}else{
$Message_Content_language_en = substr($message->field('Content'), 0, 20);
}
}
}
}
}
},
}
);
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Selection id="54008473">
<Name>Master</Name>
<Contents>
<Message refid="125796458" suppress="true" status="Unchanged"/>
<Message refid="123991123" suppress="true" status="Unchanged">
<Content language="en" imagelibraryid="5492396"/>
</Message>
<Message refid="128054778" custom="true" status="New">
<Content language="en"><![CDATA[<p>Some English content</p>]]></Content>
<Content language="fr"><![CDATA[<p>Some French content</p>]]></Content>
</Message>
</Contents>
</Selection>
<Selection id="54008475" datavaluerefid="54008479">
<Name>RMBC</Name>
<Contents>
<Message refid="125796458" sameasparent="true" parentrefid="54008473" status="Unchanged"/>
<Message refid="123991123" sameasparent="true" parentrefid="54008473" status="Unchanged"/>
<Message refid="128054778" custom="true" status="New">
<Content language="en"><![CDATA[<p>ada</p>]]></Content>
</Message>
</Contents>
</Selection>
</Root>
Here is a first attempt to try to understand what your code is supposed to do, based on the structure of the XML:
Selectionnodes looks for childrenContentnodes with attributelanguage == 'en'underMessagenodes underContentnodes./Contents/Message/Content[@language='en']imagelibraryid, store the value of thatCDATAcontent of the first childrefidto the attribute value from parentMessagenodeSelectionnodeTest run: