Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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=""> <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!

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903950]
[Corion]: Meh. SQL window functions would even make pagination easy/trivial (but not performant), as rank() over (partition by user order by timestamp) / 10 as page would give me a page number for each item, with 10 items per page.
[Corion]: Of course, the query performance for "all items on page 10" is likely worse than rank() between 100 and 109 , but if that means I can write 15 lines of SQL instead of needing to think about how to partition things and how to encode the page size...
[Corion]: ... that would be nice. But alas, I'm currently tied to SQLite as minimum implementation, and it doesn't implement window functions :-(

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2018-03-22 12:15 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (274 votes). Check out past polls.