Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: MathML 2 ascii?

by Aristotle (Chancellor)
on Jan 25, 2005 at 03:46 UTC ( #424783=note: print w/replies, xml ) Need Help??


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
    This translates your sample input to (joule / (meter ^ 2)). 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.

    Yes. Please do. I am of the firm opinion that any format that is entirely ASCII or Unicode should be human-readable, and just the right balance between terseness and verbosity to make it human-editable.

    Not every XML application is quite so bloated, but this one is a good example of what makes people reject XML. This is exactly the same situation where over-clever over-crammed Perl code turns people off of the whole Perl language.

    --
    [ e d @ h a l l e y . c c ]

      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.
      Yes. Please do.

      Parsimonious XML Shorthand Language

      <q>PXSL ("pixel") is a convenient shorthand for writing markup-heavy XML documents.</q>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-11-30 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?