I'm attempting to extract text from mixed content xml, modify that text, and then replace it back into an xml document at the correct position. The difficulty comes when setting text that contains mixed content...doing so eliminates child tags.
Example text: <paragraph> Some <bold>text</bold> here which may be any <bold>length< +/bold> and <bold>contain</bold> a number of child tags.</paragraph>
Here is the code I am developing. The &choiceReplace sub simply searches through the passed argument string and replaces matched words (which will most always alter the length of the string).
use XML::Twig; my $twig = new XML::Twig(TwigHandlers => { 'bold' => \&bold, 'p' => \ +&paragraph, 'li' => \&ordered_list},TwigRoots => {body => 1}); sub bold { my ($twig, $bold,) = @_; my $bold_text = $bold->text; &choiceReplace($bold_text,$file); $bold->set_text($bold_text); } sub paragraph { my ($twig, $para,) = @_; my $para_text = $para->text; &choiceReplace($para_text,$file); $bold->set_text($bold_text); }
The child text from the paragraph tag may contain replaced search words. When this is true, the set_text command replaces the <bold> tags with just a text string. I understand why this occurs, but is there a better method of cycling through the <paragraph> children text without affecting the <bold> tags? It seems to me that I need to move through each individual child tag by tag. Is this possible without knowing what tags/text I might encounter? Will the mixed content allow me to alter just the <paragraph> text? And still maintain the <bold> tags?

Any guidance is much appreciated.

In reply to Twig Mixed Content Child Text Replace Issues by unknown_varmit

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.