Thanks for replies.

So I'm attempting to alter the string text of <paragraph> without affecting the context of the <bold> tags. Right now, the '->text' command retrieves the text of all sub-elements. Hence, when I set the <paragraph> text, the code is replacing the <bold> elements with a string. I lose the <bold> tag.

I need to maintain the integrity of the <bold> tags while making string substitutions within the <paragraph> element. I will not always know how the text and text children of the <paragraph> element will look. So the content will vary.

I need to be able to find all of the text children of a mixed content <paragraph> element and substitute within the text string without disturbing other child elements.

Example:
Input: <paragraph> Some <bold>text</bold> here which may be any <bold>length< +/bold> and <bold>contain</bold> a number of child tags.</paragraph> Expected output: <paragraph> Some <bold>text</bold> here which may be any <bold>length< +/bold> and <bold>contain</bold> a quantity of child tags.</paragraph> Code: use XML::Twig; my $file = '<paragraph> Some <bold>text</bold> here which may be any < +bold>length</bold> and <bold>contain</bold> a number of child tags.</ +paragraph>'; my $twig = new XML::Twig(TwigHandlers => {'paragraph' => \&paragraph} +,TwigRoots => {paragraph => 1}); $twig->parse($file); $twig->print; sub paragraph { my ($twig, $para,) = @_; my $para_text = $para->text; &choiceReplace($para,$para_text); } sub choiceReplace { my ($para,$para_text) = @_; my $search = "number"; #setting search and replace for example my $replace = "quantity"; #locate each occurrence of search term and prompt user to replace foreach ($para_text =~ /$search/) { my $new_version = $para_text; my $offset = 0; my $new_offset = 0; my $result = index($para_text, $search); my $new_result = index($new_version, $search); $offset = $result; $new_offset = $new_result; my $l = length ($search); #loop through string search results while found while (($result != -1) && ($new_result != -1)) { #create visuals for user to accept/deny match print "\n\nCurrent Version:\n $para_text"; my $replace_match = "**[[$replace]]**"; substr($new_version,$new_offset,$l) = $replace_match; print "\n\nMatched Version:\n $new_version"; print "\nWould you like to make this change (y or n)? "; chomp($change=<STDIN>); if ($change eq "n"){ my $nm_result = rindex($new_version, $replace_match); my $nm_l = length ($replace_match); my $no_match = "[DENIED]"; substr($new_version,$nm_result,$nm_l) = $no_match; } elsif ($change eq "y") { my $nm_result = rindex($new_version, $replace_match); my $nm_l = length ($replace_match); my $no_match = "[CHANGED]"; substr($new_version,$nm_result,$nm_l) = $no_match; substr($para_text,$offset,$l) = $replace; } #update search starting point $result = index($para_text, $search, $offset + 1); $offset = $result; $new_result = index($new_version, $search, $new_offset); $new_offset = $new_result; } #set text for <paragraph> @_[0]->set_text($para_text); } }

In reply to Re^2: Twig Mixed Content Child Text Replace Issues by unknown_varmit
in thread 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.