in reply to XML Twig Handler Triggers Syntax

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>

Replies are listed 'Best First'.
Re^2: XML Twig Handler Triggers Syntax
by Mr.Churka (Sexton) on Dec 26, 2007 at 17:45 UTC
    The Syntax that fixed the issue was  my $twig= new XML::Twig(TwigRoots => {'oa:ItemID[@agencyRole="Prefix_Number"]' => \&STOCKnumber, The code you provided produces a warning while using strict that an explicit declaration of package name is required for @agencyrole. I am still unable to extract the text of the child element though. Now Perl is claiming that my $Item is undefined.