Ah good. I don't use XML::Twig, so I don't have a deep knowledge of it.

If it consistently ignored namespaces when map_xmlns isn't used, it would be a great shortcut despite being non-standard since there is rarely need to deal with namespace conflicts. (The module never claimed them to be a real XPaths.) Unfortunately, it doesn't consistently ignore namespaces.

On the plus side, it works according to standard when map_xmlns is used. (Well, I'm not sure how namespaces interact with attributes, so I'm simply commenting on elements.)

use strict; use warnings; use XML::Twig qw( ); my $xml = <<'__EOI__'; <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <root xmlns:foo="uri:foo"> <ele id="a" /> <ele id="b" xmlns="uri:foo"/> <foo:ele id="c" /> </root> __EOI__ { my $seen = ''; my $t = XML::Twig->new( twig_handlers => { 'ele' => sub { $seen .= $_->att('id') }, }, ); $t->parsestring($xml); print("$seen\n"); print($seen eq 'a' ? "Standard\n" : "Not standa +rd\n"); print($seen eq 'a' || $seen eq 'abc' ? "Consistent\n" : "Not consis +tent\n"); } print("\n"); { my $seen_null = ''; my $seen_foo = ''; my $t = XML::Twig->new( map_xmlns => { 'uri:foo' => 'f', }, twig_handlers => { 'ele' => sub { $seen_null .= $_->att('id') || $_->att('f: +id') }, 'f:ele' => sub { $seen_foo .= $_->att('id') || $_->att('f: +id') }, }, ); $t->parsestring($xml); print("$seen_null:$seen_foo\n"); print($seen_null eq 'a' ? "Standard\n" : " +Not standard\n"); print($seen_null eq 'a' || $seen_null eq 'abc' ? "Consistent\n" : " +Not consistent\n"); print($seen_foo eq 'bc' ? "NS working\n" : " +NS broken\n"); }
ab Not standard Not consistent a:bc Standard Consistent NS working

In reply to Re^3: XML::Twig Text replacement by ikegami
in thread XML::Twig Text replacement by Gizmo

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.