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.


In reply to XML::Rules parsing inside out? by bfdi533

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.