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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.