Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: MathML 2 ascii?

by tall_man (Parson)
on Jan 25, 2005 at 00:53 UTC ( [id://424756]=note: print w/replies, xml ) Need Help??


in reply to MathML 2 ascii?

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";

Log In?
Username:
Password:

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

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

    No recent polls found