#!/usr/local/bin/perl -w use strict; use XML::Twig; my $xml_twig = XML::Twig->new( pretty_print => 'indented', NoLWP => 1, discard_spaces => 1, EltClass => 'my_elt', ); $xml_twig->parse ( \*DATA ) || die "\nError parsing data $@\n"; # here I split on the 'split' attribute, but any other # way of getting the list of elements to split on is OK foreach my $elt ($xml_twig->findnodes( '//*[@split]')) { print "Element: ", $elt->name, "\n\n", $elt->sub_doc, "\n\n"; $elt->delete; } package my_elt; use base 'XML::Twig::Elt'; sub sub_doc { my $elt= shift; return $elt->start_tags . $elt->sprint . $elt->end_tags; } sub start_tags { my $elt= shift; return join '', map { $_->start_tag } reverse $elt->ancestors; } sub end_tags { my $elt= shift; return join '', map { $_->end_tag } reverse $elt->ancestors; } package main; __DATA__ hello1 hello2 hello3 hello4