er20 has asked for the wisdom of the Perl Monks concerning the following question:

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>

Replies are listed 'Best First'.
Re: XML::Twig "unrecognized expression in handler:"
by mirod (Canon) on Jul 28, 2007 at 01:42 UTC

    The development version of XML::Twig will let you use all of these expressions.It is pretty stable (it passes all of the tests) so you should be able to use it without problem.

    I know I should have released a version, but I have been in "just one more obscure bug to squash" mode for quite a while now. Sorry.

      Thank you mirod! I will try out the development version.

      Eric