Here is a rough approach, done by recursing through the MathML expression tree, converting from prefix to infix notation.
use strict; use XML::Twig; my %binop = qw(divide / power ^); my %down1 = qw(unit 1 math 1 apply 1); my %units = qw(joule J meter m); # Recurse through the ML tree, finding the binary operators. sub recurse { my ($e) = shift; my $tag = $e->gi; if (exists $binop{$tag}) { my $p1 = $e->next_sibling; my $p2 = $p1->next_sibling if defined($p1); recurse($p1) if defined($p1); print $binop{$tag}; recurse($p2) if defined($p2); } elsif (exists $down1{$tag}) { my $child = $e->first_child; recurse($child) if defined($child); } elsif ($tag eq "cn") { print $e->text; } elsif ($tag eq "ci") { my $txt = $e->text; if ($units{$txt}) { print $units{$txt}; } else { print $txt; } } else { print $tag; } } # Uncomment if you really don't want to see an infix operator for the +power. # $binop{power} = ""; my $t= XML::Twig->new(); my $txt = q{ <unit> <math> <apply> <divide/> <ci>joule</ci> <apply> <power/> <ci>meter</ci> <cn>2</cn> </apply> </apply> </math> </unit> }; $t->parse($txt); my $root= $t->root; recurse($root); print "\n";

In reply to Re: MathML 2 ascii? by tall_man
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.