Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: XML::Twig and handles on regex/xpath

by mirod (Canon)
on Apr 28, 2011 at 09:13 UTC ( [id://901757]=note: print w/replies, xml ) Need Help??


in reply to XML::Twig and handles on regex/xpath

If you want to use a regexp in the condition, the only way to do it is to have the condition be a regexp, that will be applied to the tag itself (ie you can't apply it to the whole path):

#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { qr/^tag[1-3]/ => sub { print $_->ta +g, ": ", $_->text, "\n"; } }) ->parse( \*DATA); __DATA__ <result> <target type="aim"> <tag1>123</tag1> <tag2>456</tag2> <nottag>789</nottag> </target> </result>

It would be nice to at least be able to apply the regexp to the path, so you could write qr{/result/target/tag[1-3]}. I'll look into it. Further than that, I don't think XML::Twig can do better, at least as it is currently implemented. Allowing the "xpath-like" interpreter to deal with XPath regexp syntax (start-with and the likes) would be a bit difficult, and Perl's regexp syntax and the XPath syntax collide ( [...] is a character class for Perl and a predicate for XPath), so not much hope there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://901757]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-19 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found