http://qs321.pair.com?node_id=1205065

bfdi533 has asked for the wisdom of the Perl Monks concerning the following question:

I have been scratching my head trying to figure out what is wrong with my parsing rules and I find put in some print statements in the rules and found that XML::Rules is parsing the XML "inside out". I am reading through the XML top to bottom to develop my rules and logic and find I am missing some info. Now I see why.

take the following example:

<?xml version="1.0"?><xmltest xmlns="http://localhost/nothing"> <summary> <item> <value>1.0</value> </item> </summary> <detail1> <item> <value>2.0</value> </item> </detail1> <detail2> <item> <value>3.0</value> </item> </detail2> </xmltest>
I have been attacking this (seemingly) incorrectly with rules like the following:
my @rules_lpo = ( default => sub { $_[0] => $_[1]->{_content}; print "$_[0] : $_[1]->{_content}\n"; }, 'summary' => sub { $insummary = 1; }, 'detail1' => sub { $ind1 = 1; $insummary = 0; }, 'detail2' => sub { $ind2 = 1; $ind1 = 0; }, 'item' => sub { if ($insummary) { $sumvalue = $_[1]->{value}; } if ($ind1) { $d1value = $_[1]->{value}; } if ($ind2) { $d2value = $_[1]->{value}; } } );

This does not work. value is processed before item which is processed before the outside tag so the flags get set AFTER the section has already processed the guts. I only saw this from the print statement in '_default' rule which I added out of sheer frustration.

The result is $sumvalue is equal to d1 value, $d1value = d2 value and $d2value is never set. I have been losing my mind over this.

How do I properly handle duplicate tags in different sections if I cannot flag them due to the way the XMl is processed in this inside out manner???

Update: Updated the XML to have proper wrapper.