Hello great purveyors or wisdom. I come seeking help from you regarding XML::Twig. Please look at the code below. There are 5 calls to testPath(), each passing 5 different paths. The first 2 work, the last 3 do not. The output for each of these errors I have posted as comments inside the code, which appear shortly after the calls to testPath(). I'm trying to make the last 3 calls work, but have provided the first two for completeness. Each call to testPath() is commented with the desired result in English, so I wont list them here. I have tried to be clear with my examples, but if you need further clarification, please feel free to ask.

I am asking three questions:

1) How can I specify the path to accomplish the desired matches as they are describe above each call?

2) If this is impossible, what is the most efficient way to manage such functionality (that is, cascading filters of nodes based upon attributes)?

3) If this is impossible, might I expect to see this type of functionality in the future? (I'm looking at you mirod ) :)

Once again... many many thanks in advance

Eric
#!/usr/bin/perl use XML::Twig; while(<DATA>) { chop;$data.= $_; } #get all <class>s with the attribute "name='Item'" $path = "class[\@name='Item']"; print "PATH: " . $path . "\n"; testPa +th($path); #works #get all <function>s who's parents are <class> $path = "class/function"; print "PATH: " . $path . "\n"; testPath($pa +th); #works #get all <function>s who's parents are <class> with the attribute "nam +e='Item'" $path = "class[\@name='Item']/function"; print "PATH: " . $path . "\n +"; testPath($path); #doesn't work #get all <function>s that have the attribute "name='SAVE_ITEM'" and wh +o's parents are <class> $path = "class/function[\@name='SAVE_ITEM']"; print "PATH: " . $path +. "\n"; testPath($path); #doesn't work #get all <function>s that have the attribute "name='SAVE_ITEM'" and wh +o's parents are <class> with the attribute "name='Item'" $path = "class[\@name='Item']/function[\@name='SAVE_ITEM']"; print "PA +TH: " . $path . "\n"; testPath($path); #doesn't work print "\n\nD0NE"; # the 3rd, 4th, and 5th calls to testPath() produce the following erro +rs, respectively: # " unrecognized expression in handler: 'class[@name='Item']/function' + at testTwig.pl line 32 " # " unrecognized expression in handler: 'class/function[@name='SAVE_IT +EM']' at testTwig.pl line 32 " # " unrecognized expression in handler: 'class[@name='Item']/function[ +@name='SAVE_ITEM']' at testTwig.pl line 32 " sub testPath { print "\nOUTPUT START_______________________\n"; my $p = XML::Twig->new(PrettyPrint => 'record',twig_handlers = +>{$path => sub { handler($_); } } ); $p->parse( $data ); print "\nOUTPUT END_______________________\n\n"; } sub handler { ($node) = @_; $node->print; } __END__ <objects> <class name="Item" > <function name="LOAD_ITEM" > </function> <function name="SAVE_ITEM" > </function> </class> <class name="Item2" > <function name="LOAD_ITEM" > </function> </class> </objects>

In reply to XML::Twig "unrecognized expression in handler:" by er20

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.