Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello,
I've been working on an XHTML parser, and for the most part I really like XML::Twig, it's very powerfull yet still fairly easy to use. However, there is still some weirdness that I cannot figure out how to fix.

For example, if I load in a valid XHTML document, add a field to it (such as a form tag), then output the parsed data. I would expect the output would be valid XHTML as well, but it isn't. See example:

This is my valid XHTML document:
<html> <head> <title>example</title> </head> <body> <text>Foo Bar</text> </body> </html>

Here is my code, which will insert a form tag into the body.
#!/usr/bin/perl -w use strict; use XML::Twig; sub main() { my $filename = $ARGV[0] ? $ARGV[0] : die "specify input filena +me.\n"; my @tags = ('input', 'textarea', 'checkbox'); my $xml = XML::Twig->new( keep_spaces_in => [ 'pre' ], pretty_print => 'indented', twig_roots => { 'body' => \&insert_form_tags, }, twig_print_outside_roots => 1, ); $xml->parsefile("$filename"); $xml->print; } sub insert_form_tags() { my ($xml, $body) = @_; my $form_group = 'process_group'; my $form_name = 'process_requirements'; my $form = $body->insert( form => { method => "Post", action => "submit.cgi", }, ); } &main();

This is simple code to input form tags, and it works, the form tags are inserted, however the output of XML::Twig generates invalid XHTML. See output:
<html> <head> <title>example</title> </head> </html> <html> <body> <form action="submit.cgi" method="Post"> <text>Foo Bar</text> </form> </body> </html>

Notice the closing, and then re-opening of the html tag right after the head, and before the body. When I try to parse this outputed file with XML::Twig it gets an error right at that spot. Removing the offending lines resolves the problem.

So my question is, how do I get XML::Twig to output valid XHTML after adding this form tag?

Thanks in advance for any help!

- Nick

In reply to XML::Twig generating invalid XHTML output?? by nick

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.
  • 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 taking refuge in the Monastery: (8)
    As of 2024-09-16 11:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The PerlMonks site front end has:





      Results (22 votes). Check out past polls.

      Notices?
      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.