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);

In reply to Path with prefix as Twig trigger?- updated 2 - didn't work - help! by amonroy

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.