Use the right tool for the job. This here is nowhere near a full implementation of a ASCII renderer for MathML, but it's a start that will work for your one example document.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr +ansform"> <xsl:output method="text" encoding="us-ascii"/> <xsl:strip-space elements="*" /> <xsl:template match="@*|node()"> <xsl:apply-templates select="@*|node()" /> </xsl:template> <xsl:template match="ci|cn"> <xsl:value-of select="." /> </xsl:template> <xsl:template match="divide"> / </xsl:template> <xsl:template match="power"> ^ </xsl:template> <xsl:template match="apply"> <xsl:variable name="operator"> <xsl:apply-templates select="*[1]" /> </xsl:variable> <xsl:text>(</xsl:text> <xsl:apply-templates select="*[2]" /> <xsl:for-each select="*[position() > 2]"> <xsl:copy-of select="$operator" /> <xsl:apply-templates select="." /> </xsl:for-each> <xsl:text>)</xsl:text> </xsl:template> </xsl:stylesheet>

This translates your sample input to

(joule / (meter ^ 2))

Yes, XSLT is horribly verbose, but if you can see past the long-winded identifiers and the redundancy caused by closing tags, you'll find the basic modus operandi of the language to be a compact, elegant way of expressing tree manipulations. (I've often toyed with the idea of writing a strictly lexical translator that would allow transformations to be expressed in a less redundant, more human friendly syntax.)

The following could be used as a driver script if you need to do this from Perl.

#!/usr/bin/perl use strict; use warnings; use XML::LibXSLT; use XML::LibXML; my $parser = XML::LibXML->new(); my $mathml = $parser->parse_string( do { local $/; <> } ); my $xform_src = $parser->parse_fh( \*DATA ); my $xform = XML::LibXSLT->new()->parse_stylesheet( $xform_src ); my $results = $xform->transform( $mathml ); print $xform->output_string( $results ); __END__ paste stylesheet here...

Makeshifts last the longest.


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