Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

XML::Rules Parsing Order

by feiiiiiiiiiii (Acolyte)
on Feb 09, 2015 at 19:30 UTC ( #1116093=perlquestion: print w/replies, xml ) Need Help??

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

Hi! I recently came across a problem about XML::Rules parsing order.

Say I have some code like this

my %rules = ( _default => sub {$_[0] => trim($_[1]->{_content})}, parent_tag => sub {...}, child_tag => sub {...}, )

the structure of the xml file is like

<parent_tag> ... <child_tag> ... </child_tag> </parent_tag>

Will parent_tag or child_tag be parsed first? i.e. Does the parsing happen when a start tag is met, or when an end tag is met? I don't know why the order seemed to be quite random

Is there a way to always have parent_tag to be parsed first? Thanks!

+++++UPDATE+++++

Thanks for all the replies. You are amazing~~~

The reason why I want to parse the parent tag first is that I need to use the content (attributes) in the parent tag to decide if the content in the child tag should be store into the database. So if I prepend "^" to parent_tag, I can parse the parent tag first, but I cant get the content of the parent_tag but only its attributes, right?

So the only solution is to store in content of the child tag is some buffer, and then analyze it when the ending parent_tag is met and parsed?

Thanks again:)

Replies are listed 'Best First'.
Re: XML::Rules Parsing Order
by runrig (Abbot) on Feb 09, 2015 at 19:40 UTC
    If you have start rules, then parent_tag would be parsed first, and attributes (but not other contents) of the parent tag would be available in the rule. With regular rules as you have, the end tag must be parsed before the rule is invoked so that both the attributes and contents of the tag are available in the rule, therefore the child_tag rules of each parent will be invoked before each parent tag.
Re: XML::Rules Parsing Order
by BrowserUk (Pope) on Feb 09, 2015 at 19:40 UTC
    Will parent_tag or child_tag be parsed first? i.e. Does the parsing happen when a start tag is met, or when an end tag is met?

    The docs say:

    These rules are evaluated/executed whenever a tag if fully parsed including all the content and child tags and they may access the content and attributes of the specified tag plus the stuff produced by the rules evaluated for the child tags.

    So, child tags are parsed before parent tags; so that the parent tag rules have access to whatever is produced from the parsing of the child tags.

    Is there a way to always have parent_tag to be parsed first?

    Doesn't look like it. But why would you want to?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      Is there a way to always have parent_tag to be parsed first?
      Doesn't look like it. But why would you want to?
      E.g., maybe you're inserting parent/child records into the database, and in order to maintain referential integrity, you need to insert the parent records first. Assuming that all parent-specific data is available in the attributes of the parent tag, this would be possible with a start rule on the parent tag. That's one reason why I might want to...as to why the OP wants to, I wouldn't know :-)

        Okay. But if you do it when the parent tag is parsed, you have all the information from the child record available also, so you can do both in which ever order makes sense.

        But if do the parent before you parsed the child(ren), the child tag might be missing or corrupted, and you'd have then to try and back out the parent record from the DB.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        Yes I need to use some content (not attributes) in the parent tag to decide if the content in the child tag should be written to the db or not. So looks like I can only store the content of the child tag in a buffer first, and then analyze it when the ending parent tag is parsed? Thanks!
Re: XML::Rules Parsing Order
by Jenda (Abbot) on Oct 13, 2015 at 11:30 UTC

    You can have both a "^parent_tag" and a "parent_tag" rule. The first will be called before the children (and may decide to skip the whole branch, but it only has access to the attributes), the second will be called after all children.

    my %rules = ( _default => sub {$_[0] => trim($_[1]->{_content})}, '^parent_tag' => sub {...}, # called after parsing <parent_tag> parent_tag => sub {...}, # called after parsing </parent_tag> child_tag => sub {...}, # called after parsing </child_tag> )

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1116093]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-03-02 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favorite kind of desktop background is:











    Results (36 votes). Check out past polls.

    Notices?