Hello Monks, I've been getting a lot of use out of XML Twig, but have been sticking primarily to calling handlers based on element tags. Now I have a block of XML where the naming conventions caused duplicate tags for distinct elements. After poking around the CPAN docs for both twig and xpath, the XML::Twig website, and the Perl monks archives, I still can't figure out the appropriate syntax for calling a handler on an attribute/value pairing. Here's a snippet of the XML I'm dealing with.
<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>
I can't simply call a handler on the ItemID tag because of the duplication. The number I'm looking for is a concatenation of the prefix and stock number. Here's what I'm trying.
use strict; use warnings; use xml::twig; use utf8; my $file= "itemsANDstuff.xml"; my $twig_handlers = {oa:ItemID[my @agencyRole='Prefix_Number']=> \&ITE +Mnumber}; my $twig= new XML::Twig(TwigRoots => {'us:ItemMasterHeader' => 1}, + TwigHandlers => $twig_handlers); $twig->parsefile($file); sub ITEMnumber{ my ($twig, $Item)= @_; my $prefix=($Item->first_child->text); my $stock = (next_sibling->first_child->text); my $itemNUMBER = $prefix.$stock; print "$itemNUMBER \n";};
The line raising an exception is my $twig_handlers = {oa:ItemID[my @agencyRole='Prefix_Number']=> \&ITEMnumber}; I've tried a variety of fixes on this line, including using * between the element tag and the [, escaping the @ using double quotes and single quotes, single quotes and double quotes on the element tag alone, omitting the element tag in favor of an *, praying and kicking my machine. None of these have helped much. Any help would be greatly appreciated.

In reply to 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.