Maybe what you want is to dispatch to a sub-handler based on context? Something like:

use strict; use warnings; use XML::Twig; my $xml = <<'XML'; <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> XML 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, }, ); $twig->parse ($xml); sub uml_class_start { my ($twig, $elt) = @_; my $xmi_id = $elt->att( 'xmi.id' ); my $name = $elt->att( 'name' ); } sub uml_package_start { } sub uml_taggedvalue { my ($twig, $elt) = @_; if ($elt->parent (qr/UML:Attribute/)){ uml_taggedvalue_attr (@_); } elsif ($elt->parent (qr/^UML:Class$/)) { print "Ignoring class taggedvalue\n"; } else { uml_taggedvalue_def (@_); } } sub uml_generalization { } sub uml_attribute { } sub uml_taggedvalue_attr { print "uml_taggedvalue_attr\n"; } sub uml_taggedvalue_def { print "uml_taggedvalue_def\n"; }

Prints:

Ignoring class taggedvalue Ignoring class taggedvalue uml_taggedvalue_attr uml_taggedvalue_attr uml_taggedvalue_def uml_taggedvalue_def

Perl is environmentally friendly - it saves trees

In reply to Re: Creating context-specific handlers in XML::Twig by GrandFather
in thread 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.