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

Hello,

I'm trying to use Twig to parse an XML document that contains prefixes associated with namespaces. I don't know how create the path without hardcoding the prefix. Any help would be appreciated.

My XML document looks like this:

<prefix:monastery xmlns:prefix="http://perlmonks.org"> <prefix:foo> <prefix2:bar xmlns:prefix2="http://perlmonks.org/blah"> <prefix2:monk>Larry Wall</prefix2:monk> </prefix2:bar> </prefix:foo> </prefix>

But my script doesn't work unless I have it like this:

my $trigger = "prefix:monastery/prefix:foo/prefix2:bar/prefix2:monk"; my $twig = XML::Twig->new(TwigHandlers => { $trigger => \&function });

Since I cannot relay on the name of prefix and prefix2, I would like to see have something like this:

my $trigger = "*:monastery/*:foo/*:bar/*:monk"; my $twig = XML::Twig->new(TwigHandlers => { $trigger => \&function });

I tried that, and it complains that it's not a correct trigger. Has anyone encountered this issue with XML::Twig?

Thanks!


Update

I found how to do it. I just needed to read more the documentation :-)
my $xml_ns = { "http://perlmonks.org" => prefix, "http://perlmonks.org/blah" => prefix2 }; my $trigger = "prefix:monastery/prefix:foo/prefix2:bar/prefix2:monk"; my $twig = XML::Twig->new( map_xmlns => $xml_ns, TwigHandlers => { $trigger => \&function } );

UPDATE 2 - Didn't work!

Sorry, I spoke too soon.

Could someone shed a light?

This handler nevers gets called.

use XML::Twig; use strict; my $twig = XML::Twig->new( map_xmlns => { "http://perlmonks.org" => 'aaa', "http://perlmonks.org/blah"=> 'bbb' }, TwigHandlers => { '/aaa:monastery/aaa:foo/bbb:bar/bbb:monk' => sub { warn "match +ed!"; $_[0]->purge; } } ); my $xml =<<XML; <?xml version="1.0" encoding="UTF-8"?> <prefix:monastery xmlns:prefix="http://perlmonks.org"> <prefix:foo> <prefix2:bar xmlns:prefix2="http://perlmonks.org/blah"> <prefix2:monk>Larry Wall</prefix2:monk> </prefix2:bar> </prefix:foo> </prefix:monastery> XML $twig->parse($xml);

But this one works.

use XML::Twig; use strict; my $twig = XML::Twig->new( TwigHandlers => { '/prefix:monastery/prefix:foo/prefix2:bar/prefix2:monk' => sub + { warn "matched!"; $_[0]->purge; } } ); my $xml =<<XML; <?xml version="1.0" encoding="UTF-8"?> <prefix:monastery xmlns:prefix="http://perlmonks.org"> <prefix:foo> <prefix2:bar xmlns:prefix2="http://perlmonks.org/blah"> <prefix2:monk>Larry Wall</prefix2:monk> </prefix2:bar> </prefix:foo> </prefix:monastery> XML $twig->parse($xml);

Replies are listed 'Best First'.
Re: Path with prefix as Twig trigger?- updated 2 - didn't work - help!
by mirod (Canon) on Aug 30, 2004 at 13:53 UTC

    I think you found a bug :--(

    The code that checks if a handler should be applied only does the namespace relacing for "simple" handlers, not for handlers with a path. I will have a look at this and should have a fixed version by tomorrow.

    Then maybe this will be the final straw and I will start working on cleaning up the code that triggers handlers, which has grown quite a bit in the last few years and is now quite a mess.

    Thanks

Re: Path with prefix as Twig trigger?- updated 2 - didn't work - help!
by mirod (Canon) on Aug 30, 2004 at 16:06 UTC

    An updated version of the module is on The XML::Twig Page, let me know if it fixes all of your problems (it works fine on your example).

      Thanks a lot for the quick fix.