I am still trying to wrap my head around XML::Twig and I could really use a nudge in the right direction. The docs just aren't making it through the fog and into my head today.

I am trying to parse an XMI file that has the following overall structure (greatly simplified, of course):

<XMI xmlns:UML="omg.org/UML1.3" xmi.version="1.1" timestamp="2007- +06-26..."> <XMI.content> <UML:Class name="Address" xmi.id="EAID_009DB5F9_3A9D_44d0_ +88BD_..."> <UML:ModelElement.taggedValue> <UML:TaggedValue tag="isSpecification" value="false" / +> <UML:TaggedValue tag="ea_stype" value="Class" /> </UML:ModelElement.taggedValue> <UML:Classifier.feature> <UML:Attribute name="city" changeable="none" > <UML:ModelElement.taggedValue> <UML:TaggedValue tag="description" value="City" /> <UML:TaggedValue tag="ea_guid" xmi.id="EAID_661614 +4D_..." /> </UML:ModelElement.taggedValue> </UML:Attribute> </UML:Classifier.feature> </UML:Class> <UML:TaggedValue tag="created" xmi.id="EAID_17DA671B_9257_42f1_8AA8_0EF5F305DBD0" modelElement="EAID_31EB028E_30B2_430a_9168_1010A2A7B851" / +> <UML:TaggedValue tag="ea_stype" xmi.id="EAID_FAF2F308_9EB4_4f83_90F6_BE8ABC10087C" modelElement="EAID_31EB028E_30B2_430a_9168_1010A2A7B851" / +> </XMI.content> </XMI>

The handlers I created are as follows:

my $twig = XML::Twig->new( start_tag_handlers => { 'UML:Class' => \&uml_class_start, 'UML:Package' => \&uml_package_start, }, twig_handlers => { # purge at the end of each class section 'UML:Class' => sub { $_[0]->purge }, 'UML:Attribute' => \&uml_attribute, 'UML:Generalization' => \&uml_generalization, 'UML:TaggedValue' => \&uml_taggedvalue, }, );

I need to get the value of different tags, but I suspect that I need different handlers for each, depending on the context. For example,

  1. I need to get the name and xmi.id for each class, which I do in uml_class_start() using
    my $xmi_id = $elt->att( 'xmi.id' ); my $name = $elt->att( 'name' );
    I don't need any of the UML:TaggedValue tags.
  2. I need the name and xmi.id for each attribute. Getting the name is done in uml_attribute() (which is easy, see the class example, above), but note that the xmi.id is one of the UML:TaggedValue tags (tag="ea_guid").
  3. At the end of the file there are a bunch of UML:TaggedValue tags that are not located within a Class or Attribute block. I need to grab the tag, xmi.id, value (not shown in the XMI snippet), and modelElement values for each (or, more specifically, for a given set of modelElement values). I do that in uml_taggedvalue() using the att() method, as in the class example.

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? (There are a couple of additional cases, too, but I can solve those by generalizing the solution to this question.)

Thanks very much, in advance.


In reply to Creating context-specific handlers in XML::Twig by bobf

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.