MathML conversion functions

(Excerpt from "The MathML Handbook" by Pavi Sandhu)

Mathematica 4.2 contains several functions for converting between MathML and the boxes and expressions used internally to represent mathematical formulas. Some of the important MathML functions are listed in the following table.

Table: Important MathML functions.

Function Description
MathMLToExpression Converts a MathML string to a Mathematica expression.
ExpressionToMathML Converts a Mathematica expression to a MathML string.
MathMLToBoxes Converts a MathML string to a box expression.
BoxesToMathML Converts a box expression into MathML.

These functions are all found in the context XML`MathML`. Hence, to evaluate any of these functions, you must add this context name as a prefix to the name of the function. Here is an example that shows how these functions work:

In[1]:= XML`MathML`ExpressionToMathML[x^2]
Out[1] = <math xmlns='http://www.w3.org/1998/Math/MathML'>
  <semantics>
    <msup>
      <mi>x</mi>
      <mn>2</mn>
    </msup> 
    <annotation-xml encoding='MathML-Content'>
      <apply>
        <power/>
        <ci>x</ci>
        <cn type='integer'>2</cn>
      </apply>
    </annotation-xml>
  </semantics>
</math>

You can specify any of the export conversion options listed under Exporting MathML from Mathematica directly in the functions that produce MathML. For example, the function XML`MathML`ExpressionToMathML yields both presentation and content markup enclosed in a semantics tag. But you can get either presentation or content markup alone by specifying the "Formats" conversion option. Here is an example:

In[2]:= XML`MathML`ExpressionToMathML[x+1, "Formats" -> "PresentationMathML"]
Out[2]= <math xmlns='http://www.w3.org/1998/Math/MathML'>
  <msup>
    <mi>x</mi>
    <mn>2</mn>
  </msup> 
</math>

You can add a namespace prefix to the output using the option "NamespacePrefixes", as shown here:

In[3]:= XML`MathML`ExpressionToMathML[x+1, "Formats" -> "ContentMathML" "NamespacePrefixes" -> {"http://www.w3.org/1998/Math/MathML"->"mml"}]
Out[3] = <math xmlns='http://www.w3.org/1998/Math/MathML'>
  <m:apply>
    <m:power/>
    <m:ci>x</m:ci>
    <m:cn m:type='integer'>2</m:cn>
  </m:apply>
</math>

The % command represents the result of the last calculation that you did in the notebook. The following command converts the MathML string back to a Mathematica expression:

In[4]:= XML`MathML`MathMLToExpression[%]
Out[4] = x2

The following command converts a Mathematica box expression into the corresponding presentation MathML:

In[5]:= XML`MathML`BoxesToMathML[SuperscriptBox["x","2"]]
Out[5] = <math xmlns='http://www.w3.org/1998/Math/MathML'>
  <msup>
    <mi>x</mi>
    <mn>2</mn>
  </msup>
</math>

You can transform the MathML back into a box expression as shown here:

In[3]:= XML`MathML`MathMLToBoxes[%]
Out[3] = FormBox[TagBox[SuperscriptBox[x,2], MathMLPresentationTag, AutoDelete -> True], TraditionalForm]

These examples demonstrate how closely MathML support is integrated into Mathematica. You can seamlessly and automatically convert mathematical expressions from Mathematica's syntax to MathML and vice versa. This makes it easy to integrate Mathematica with other applications using MathML as the format for exchanging mathematical information. Under Doing computations on the server, we shall see some specific examples of how Mathematica and MathML can be combined to do computations in a Web browser.

The functions outlined before, for importing, exporting, and transforming MathML, are a special case of the more general XML processing capabilities available in Mathematica 4.2. You can import any arbitrary XML documents into Mathematica and then transform them using SymbolicXML. This is a format for representing XML documents as Mathematica expressions while preserving their tree structure. The advantage of converting XML documents into SymbolicXML is that you can apply to XML data any of Mathematica's built-in functions for numerical, symbolic, and graphical computations as well as functional programming. The MathML conversion functions discussed are all implemented internally using SymbolicXML. The combination of SymbolicXML and Mathematica programming provides a useful alternative to other techniques for manipulating XML documents, such as XSLT transformations or the SAX or DOM APIs used with a low-level programming language such as Java.

   

<< back next >>

 

 

 

Tipp der data2type-Redaktion:
Zum Thema MathML bieten wir auch folgende Schulungen zur Vertiefung und professionellen Fortbildung an:

 

Copyright © CHARLES RIVER MEDIA, INC., Massachusetts (USA) 2003
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "The MathML Handbook" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


CHARLES RIVER MEDIA, INC., 20 Downer Avenue, Suite 3, Hingham, Massachusetts 02043, United States of America