Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

OK, so here the problem is that the filter opions do not operate when using the twig_print_outside_roots. At least they are not applied to the data outside the roots. Which makes sense as you are supposed to only care about what's inside the roots, that's the whole point of the "roots mode".

Below is a way of solving your problem. I must say I would not use it though. Instead I would do a pre or post processing of the data, using regexps or maybe Text::Unidecode, to replace the characters you don't want. I assume you want to replace those characters everywhere in the document, so most likely you don't need to use an XML aware tool.

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $t = XML::Twig->new( twig_handlers => { hl1 => \&fix_hl1, hl2 => \&fix_hl2, _all_ => \&fixup, }, keep_encoding => 1, keep_spaces => 1, ) ->parse( \*DATA); sub fixup { my( $t, $elt)= @_; foreach my $pcdata ($elt->children( '#PCDATA')) { $pcdata->set_text( conv_chars( $pcdata->text)); } $elt->flush unless( $_->in( 'hl1') || $_->in( 'hl2')); } sub conv_chars { my $t= shift; $t=~ s{&#8212;}{--}g; return $t; } sub fix_hl1 { $_->set_tag( 'new_hl1')->prefix( 'fixed hl1:'); } sub fix_hl2 { $_->set_tag( 'new_hl2')->prefix( 'fixed hl2:'); } __DATA__ <!DOCTYPE nitf [ <!ENTITY foo "bar"> ]> <nitf><s><hl1>hl1 with a &foo; and an other &foo; and &#8212; and &#82 +12;</hl1> <p>text, no foo</p> <p>text, no foo but &#8212;</p> <p>text, with &foo;</p> <hl2>hl2 with a &foo; and an other &foo; and &#8212; and &#82 +12;</hl2> <hl2>hl2 no foo </hl2> <hl2>hl2 with a bold <b>&foo;</b>, <b>&#8212;</b> and an othe +r &foo;</hl2> </s> </nitf>

In reply to Re^3: XML::Twig question and problem by mirod
in thread XML::Twig question and problem by BenHopkins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-16 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found