#!/bin/perl -w

use strict;
use XML::Twig;

my $twig= new XML::Twig( twig_handlers => 
                { doc            => \&doc,
                 '/doc/title'    => \&doc_title,     # full path
                 'section/title' => \&section_title, # partial path
                  section        => \&section,
                }
                       ); 

$twig->parsefile( "simple_doc.xml");                  # parse the twig
$twig->print;                                         # print the modified twig


sub doc_title
  { my( $t, $doc_title)= @_;
    $doc_title->set_gi( 'h1');                        # just change the tag to h1
  }

sub section_title
  { my( $t, $section_title)= @_;
    $section_title->set_gi( 'h2');                    # just change the tag to h2
  }

sub section
  { my( $t, $section)= @_;
    $section->erase;                                  # erase just erases the tags
  }

sub doc
  { my( $t, $doc)= @_;
    $doc->set_gi( 'html');                            # set the gi to html
    my $doc_title= $doc->first_child( 'h1')->text;    # the title is now a h1 element
    $doc->insert( 'body');                            # create the body
    my $header= new XML::Twig::Elt( 'header');        # create the header
    $header->paste( $doc);                            # paste it 
    my $title= $header->insert( 'title');             # insert the title
    $title->set_text( $doc_title);                    # with the appropriate content
  }



