Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Replace MathML content using Twig

by cjb (Friar)
on May 10, 2011 at 11:05 UTC ( #903950=note: print w/ replies, xml ) Need Help??


in reply to Replace MathML content using Twig

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!


Comment on Re: Replace MathML content using Twig
Select or Download Code
Re^2: Replace MathML content using Twig
by renovatio (Initiate) on May 10, 2011 at 12:20 UTC

    Thank you for GrandFather, cjb and choroba!

    These responses are very valuable and helpful for me.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903950]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-07-23 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (152 votes), past polls