in reply to MathML 2 ascii?
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: MathML 2 ascii?
by halley (Prior) on Jan 25, 2005 at 16:47 UTC | |
by Anonymous Monk on Jan 25, 2005 at 16:55 UTC |