in reply to Creating context-specific handlers in XML::Twig

How can I tell XML::Twig to ignore UML::TaggedValue tags in Classes, handle them one way in Attributes, and handle them a different way if they are not in either Classes or Attributes?

Ignoring them is just a question of handling them by doing nothing. So you really only set a twig_handler to look at UML:TaggedValue elements, and within that, use the path method to look at your ancestor chain to determine whether you unleash the Attributes treatment or else the not_a_Class treatment.

Is that enough for you to get going or do you need a snippet?

• another intruder with the mooring in the heart of the Perl

  • Comment on Re: Creating context-specific handlers in XML::Twig

Replies are listed 'Best First'.
Re^2: Creating context-specific handlers in XML::Twig
by bobf (Monsignor) on Nov 09, 2007 at 05:17 UTC

    Thanks for the reply and for the pointer to path(). I ran a few tests and I think I could make that work.

    Is this really the best way to approach this problem, though? I am not trying to be argumentative; I am just a little surprised because it was not what I was expecting.

    This approach requires that I write a lot of

    if( $child_of_some_element ) { # do this } elsif( $child_of_some_other_element ) { # do something else }
    in several different handlers, which seems pretty high maintenance - if the structure of a Class element (for example) changes I have to make sure all of the handlers are changed appropriately.

    I was hoping there would be a way to approach this that is a little more OO-like, where the child element wouldn't have to know what the parent element was in order to do the Right Thing. Ideally, I'd like to think about parsing this file in chunks: parse Class elements this way, parse Attribute elements that way, etc, and each chunk (think "object") could have its own TaggedValue handler (for example).

    Perhaps I am thinking about this in the wrong way and over-complicating matters, or my expectation for how to solve this problem is a Bad Idea.

    Thoughts?