Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.

Re: XML::Twig generating invalid XHTML output??

by Tanktalus (Canon)
on Feb 14, 2005 at 17:26 UTC ( [id://430867]=note: print w/replies, xml ) Need Help??

in reply to XML::Twig generating invalid XHTML output??

This is not quite how I normally use XML::Twig (I avoid the handlers, but that's just me ;-}), but here's a potential solution. Change your constructor to:

my $xml = XML::Twig->new( keep_spaces_in => [ 'pre' ], pretty_print => 'indented', twig_roots => { 'body' => \&insert_form_tags, 'html' => sub {}, }, twig_print_outside_roots => 1, );

That seemed to solve it here.

Replies are listed 'Best First'.
Re^2: XML::Twig generating invalid XHTML output??
by nick (Sexton) on Feb 14, 2005 at 17:31 UTC
    Thanks very much for such a quick response. That seemed to do the trick for me as well. Any ideas why? What changes triggering an empty subrouting to be called for the html tags?
    - Nick

      Could it be that you're suppose to use twig_handlers instead of twig_roots, since you don't want body to become a root node? The example from the module's documentation is: (irrelevant details omitted)

      my $twig=XML::Twig->new( twig_handlers => { title => sub { $_->set_gi('h2') }, # Change title tags to h2. para => sub { $_->set_gi('p') }, # Change para to p. hidden => sub { $_->delete; }, # Remove hidden elements. list => \&my_list_process, # Process list elements. }, pretty_print => 'indented', # Output will be nicely formatted. );
        Looks like that works too! Hmmm. I've read places which suggest using handlers, and others that suggest roots, wonder why/which is better?
        - Nick

      The answer to your question of "why" depends on how deep of an answer you're looking for.

      On the deepest level, I'm not even going to pretend to look at the XML::Twig code (or any of the miriad of levels of code under it). So, no, no ideas why from that level.

      From a higher, XML::Twig-is-a-black-box level, how I got this answer was from playing with the code. First thing I did was comment out the twig_print_outside_roots => 1, line. That showed me the reason why you put it in in the first place: all of a sudden, everything outside of the body tag stopped being printed. That gave me the idea to try adding a new root such that everything would be in it - then you don't have anything outside of the roots (so that line is no longer really needed... I think). Since 'html' is the root of everything, I figured it would be the appropriate root to use. At this point, the question is: what do we want to do with that root. The answer is simple: nothing. And that's what I told perl: do nothing.

      Does that help answer the question?

        Sure did! Thanks again!
        - Nick

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-07-22 11:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.