Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-01-28 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?