This will replace <mml:mi>s</mml:mi> with <mml:mi>S</mml:mi>

#!/opt/perl/bin/perl use Modern::Perl; use XML::Twig; my $xml = do { local $/; <DATA> }; my $twig=XML::Twig->new(TwigHandlers => {'mml:mi' => \&mmlmi}); $twig->parse($xml); $twig->print(pretty_print => 'indented'); sub mmlmi { my ($fromtwig, $mmlmifrom) = @_; my $mmlmi = $mmlmifrom->text; if ($mmlmi eq 's') { $mmlmifrom->set_text('S'); } } __DATA__ <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> <mml:mstyle mathsize="1em" displaystyle="true" mathcolor="#0000ff" f +ontfamily="serif"> <mml:mrow> <mml:msubsup> <mml:mstyle displaystyle="true"> <mml:mo>&#x222b;</mml:mo> </mml:mstyle> <mml:mn>0</mml:mn> <mml:mn>4</mml:mn> </mml:msubsup> </mml:mrow> <mml:msqrt> <mml:mrow> <mml:mrow> <mml:msup> <mml:mi>s</mml:mi> <mml:mn>2</mml:mn> </mml:msup> </mml:mrow> <mml:mo>+</mml:mo> <mml:mi>s</mml:mi> </mml:mrow> <mml:mspace width="0em" height="1.2ex" /> </mml:msqrt> <mml:mo maxsize="1">(</mml:mo> <mml:mn>2</mml:mn> <mml:mi>s</mml:mi> <mml:mo>+</mml:mo> <mml:mn>1</mml:mn> <mml:mo maxsize="1">)</mml:mo> <mml:mspace width="0.167em" /> <mml:mi>d</mml:mi> <mml:mi>s</mml:mi> </mml:mstyle> </mml:math>

You could use parsefile instead of parse to read from the file system, and print_to_file instead of the print to write it back to the file system

20110510 @ 11:32 UTC Added print_to_file & parsefile

20110510 @ 12:52 UTC Added ! to first line. Doh!


In reply to Re: Replace MathML content using Twig by cjb
in thread Replace MathML content using Twig by renovatio

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.