Erm ... why don't you test the value of the attribute within the ITEMnumber and return if it doesn't match whatever you were looking for?

I don't know XPath myself, but I would not expect to see "my" within any or having them unquoted within a Perl script. Try something like

my $twig_handlers = { q{oa:ItemID[@agencyRole='Prefix_Number']} => \&ITEMnumber };

Update: Your solution (if it worked) as well as the first runrig's solution has one problem, it's very picky. It would break as soon as the order of the <oa:ItemID>s changed or (your's only) even if there was just another inserted in between. If that's OK with you, OK. I would feel uneasy about that. The second runrig's solution does not have this problem, but it starts to look too complex. Especially due to the use of the lexical variables shared by the handlers.

I think this can be better solved by XML::Rules ... the code does basicaly the same, but it doesn't have to worry about keeping the data:

use strict; use XML::Rules; my $parser = XML::Rules->new( start_rules => { 'oa:ItemID' => sub {return $_[1]->{agencyRole} =~ /^(?:Prefix_Numb +er|Stock_Number_Butted)$/} # this filters the <oa::ItemID>s we are interested in }, rules => { 'oa:ID' => 'content', 'oa:ItemID' => sub { return $_[1]->{agencyRole} => $_[1]->{'oa:ID'} }, 'us:ItemMasterHeader' => sub { print "$_[1]->{Prefix_Number}$_[1]->{Stock_Number_Butted} \n"; return; } }, ); $parser->parse(\*DATA); __DATA__ <us:ItemMaster> <us:ItemMasterHeader> <oa:ItemID agencyRole="Product_Number"> <oa:ID>0123456</oa:ID> </oa:ItemID> <oa:ItemID agencyRole="Prefix_Number"> <oa:ID>AAA</oa:ID> </oa:ItemID> <oa:ItemID agencyRole="Stock_Number_Butted"> <oa:ID>01234</oa:ID> </oa:ItemID> </us:ItemMasterHeader> </us:ItemMaster>

In reply to Re: XML Twig Handler Triggers Syntax by Jenda
in thread XML Twig Handler Triggers Syntax by Mr.Churka

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.