Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: MathML 2 ascii?

by mirod (Canon)
on Jan 27, 2005 at 08:49 UTC ( [id://425505]=note: print w/replies, xml ) Need Help??


in reply to MathML 2 ascii?

Below is how I would do it.

It should be extensible to handle other units. It assumes apply is only used with 2 arguments, but this shouldn't be a problem, if it is used with more I would assume that it is for multiplication, which doesn't need any symbol to be applied.

If I were you I would first create a version that would just output the unit -> ascii conversion, and run it on all documents, or on a set of documents. Something like removing the twig_print_outside_roots, instead of printing the content of unit store it in a hash $_->sprint => $_->text, and dump the content of that hash once you're done. This will give you all units in the document, so you can check the transformation. If you are not happy with the results and working at XML level is too much of a pain you can also add a final transformation there, with a simple hash initial_result => what_you_want.

BTW are you really limited to pure ascii? Displaying J/m² could be a nice touch.

#!/usr/bin/perl -w use strict; use XML::Twig; my %unit2symbol= ( joule => 'J', meter => 'm'); XML::Twig->new( twig_roots => { unit => sub { print $_->text } }, twig_handlers => { divide => sub { $_->set_text( '/') +}, ci => sub { $_->set_text( $unit +2symbol{$_->text}); }, apply => sub { $_->child( 0)->move +( after => $_->child( 1)); }, }, twig_print_outside_roots => 1, ) ->parse( \*DATA); __DATA__ <doc> <p>10 <unit> <math> <apply> <divide/> <ci>joule</ci> <apply> <power/> <ci>meter</ci> <cn>2</cn> </apply> </apply> </math> </unit></p> </doc>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2025-02-15 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found