Here is an example of how I would do this: I would use get_xpath to find the footnotes (in the proper context if need be), the mark method to create an element holding the punctuation, and then prefix to add the text to the next element (in the case where there is no next text element I effectively create one).
Hopefully the code below can be adapted to your problem. Note that it is not perfect, for example it would trip on i.e. and e.g., to mention a problem that seems to have caused some concer on p5p.;--)
#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; use XML::Twig; $/="\n\n"; my $doc= <DATA>; my $expected=<DATA>; 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/(?<![A-Z])([.,?:!]+)$/)) { my $punct= shift @new_elt; # punt is a 'p' element if( my $next_elt= $ftnote->next_sibling_is( '#PCDATA')) { # there is text after the ftnore, prefix it with the p +unctuation $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 => $ftnot +e); } # no need to keep the new element around, it's been used $punct->delete; } } } package main; __END__ <doc> <p>text.<ft/>more text</p> <p>nothing <ft/> to see here</p> <p>this.<ft/></p> <p>text,<ft/>more text</p> <p>text...<ft/>more text</p> <p>text T.E.X.T.<ft/>more text</p> </doc> <doc> <p>text<ft/>.more text</p> <p>nothing <ft/> to see here</p> <p>this<ft/>.</p> <p>text<ft/>,more text</p> <p>text<ft/>...more text</p> <p>text T.E.X.T.<ft/>more text</p> </doc>
In reply to Re: Moving a tag within text with XML::Twig
by mirod
in thread Moving a tag within text with XML::Twig
by skillet-thief
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |