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>

In reply to Re: MathML 2 ascii? by mirod
in thread MathML 2 ascii? by mikeyo

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.