nick has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I've recently been charged with the task of manipulating some XHTML (XML compliant HTML). I've gotten a hang of changing certain fields with the attr function, but I'm having trouble inserting new fields into the existing tree.

For example, lets say my XHTML looks like this:
<html>
 <body>
   What is Foo?
 </body>
</html>

Now let's say I wanted to do two things. One is to insert a form tag which would make anything that is currently the child of 'body', to be a child of 'form' and make 'form' a child of 'body'. This can be done like this:

my $xml = XML::Twig->new(
              twig_roots => { 
                  'body' => \&insert_form_tags, 
              },
          );
$xml->parsefile('index.html');
$xml->print;

sub insert_form_tags() {
    my ($t, $body) = @_;
    $body->insert(
        form => {
            method => "Post",
            action => "submit.cgi"
            },
        );
}

The outputted XHTML will have the form tags inserted.
<html>
 <body>
  <form method="Post" action="submit.cgi">
   What is Foo?
  </form>
 </body>
</html>

However, the other thing I'd like to do is insert a stand alone field. Something with no children. In the case of this example, insert a form field <input type="text" /> right after form. Notice the ending / in the tag. (So the text 'What is Foo?' is not a child of the input tag.

I'd like my html to look like this:
<html>
 <body>
  <form method="Post" action="submit.cgi">
   <input type="text" name="foo_is" />
   What is Foo?
  </form>
 </body>
</html>

Can someone help me with the code to do this? I've looked at much XML::Twig documentation but haven't found any examples of people needing to do this (although I'm sure it's a common practice).

All help greatly appreciated.

- Nick
  • Comment on XHTML with XML::Twig - tree manipulation problem

Replies are listed 'Best First'.
Re: XHTML with XML::Twig - tree manipulation problem
by mirod (Canon) on Feb 12, 2005 at 19:53 UTC

    You need to get the form element, which is easy, it is returned by the insert method. Then you can insert a new element as first child of this element. You can use XML::Twig::Elt->new and then paste that element, or simply call $form->insert_new_elt, the arguments are the position (first_child), then the same arguments as for new: the element tag then a hasref of attributes. The code is below.

    2 quick comments: if you are dealing with XHTML you want to use keep_spaces_in => to avoid XML::Twig messing up the whitespaces in pre tags, and you will not get exactly the output format you want, the input tag will be on the same line as the form one. If What is Foo? was in a p element (or any other element for what matters), then you would get the formating you want (and I suspect a better chance to get valid XHTML).

    #!/usr/bin/perl -w use strict; use XML::Twig; my $xml = XML::Twig->new( keep_spaces_in => [ 'pre' ], # safer, other tags migh +t need to be included pretty_print => 'indented', twig_roots => { 'body' => \&insert_form_tags, }, ); $xml->parsefile('index.html'); $xml->print; sub insert_form_tags() { my ($t, $body) = @_; my $form= $body->insert( form => { method => "Post", action => "submit.cgi" }, ); $form->insert_new_elt( first_child => input => { type => "text", n +ame => "foo_is" }); }