#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; use XML::Twig; $/="\n\n"; my $doc= ; my $expected=; my $t= my_twig->new( pretty_print => 'indented', elt_class=> 'my_elt', # so I can write $elt->fix_style ) ->parse( $doc) ->fix_style( 'p', 'ft') ; is( $t->sprint, $expected, 'all tests'); package my_twig; use base 'XML::Twig'; sub fix_style { my( $t, $para, $ftnote)= @_; # the get_xpath here gets all footnaotes in the proper context foreach my $elt ($t->get_xpath( "$para/$ftnote")) { $elt->fix_style(); } return $t; } package my_elt; use base 'XML::Twig::Elt'; sub fix_style { my( $ftnote)= @_; if( my $prev_text= $ftnote->prev_sibling_is( '#PCDATA')) { # mark will put the text matched in a new elt # and return the list of newly created elements if( my @new_elt= $prev_text->mark( qr/(?next_sibling_is( '#PCDATA')) { # there is text after the ftnore, prefix it with the punctuation $next_elt->prefix( $punct->text); } else { # no text after the ftnote, move the text (#PCDATA) of the # newly created element after the ftnote $punct->first_child( '#PCDATA')->move( after => $ftnote); } # no need to keep the new element around, it's been used $punct->delete; } } } package main; __END__

text.more text

nothing to see here

this.

text,more text

text...more text

text T.E.X.T.more text

text.more text

nothing to see here

this.

text,more text

text...more text

text T.E.X.T.more text