MathML conversion functions

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

Maple (Version 7 or later) contains a number of functions for converting Maple expressions to different types of MathML and vice versa. These functions are all contained in a package called MathML.

Each Maple package consists of a set of specialized functions that deal with a specific topic. Unlike the top-level functions (such as integrate or sin, which can be evaluated if you just specify the function name), package functions must be prefaced with the name of the package.

For example, the MathML package contains a function called Export, which converts a Maple expression into MathML. Hence, to evaluate this function, the Maple command would be:

MathML[Export](arg);

This is called the long form of the function name. Alternatively, you can first load the package using the with command. You can then refer to functions in the package using their short names; that is, without using the package name as a prefix. Doing this is useful when you intend to use one or more functions from a package repeatedly in the same Maple session.

The following two commands are equivalent to the single command just given:

with(MathML); Export(arg);

Converting Maple syntax to MathML

The MathML package contains three functions for converting a Maple expression into a MathML:

  • MathML[Export](expr): converts a Maple expression into a MathML string. The resulting MathML is in the form of combined markup with three separate encodings enclosed in parallel in a semantics tag: presentation markup followed by content markup enclosed in an annotation-xml followed by a textual representation of the Maple syntax for the input expression enclosed in an annotation element. This allows Maple to recover the original input if it encounters the same MathML expression.
  • MathML[ExportContent](expr): converts a Maple expression into content MathML. This is faster and more efficient than using the MathML[Export] function, which must produce three separate encodings of the same expression.
  • MathML[ExportPresentation](expr): converts a Maple expression into presentation MathML. This works by first generating a full parallel encoding for the specified expression and then stripping off the content MathML and Maple annotations. Hence, it is not more efficient or faster than using MathML[Export].

Here are some examples showing how these functions work.

The following command produces combined markup, with both presentation and content markup representations of the Maple expression, enclosed in a semantics tag:

MathML[Export](x^2+sqrt(x));
"<math mlns='http://www.w3.org/1998/Math/MathML'><semantics><mrow xref='id6'><mrow xref='id3'><msup><mi xref='id1'>x</mi><mn xref='id2'>2</mn></msup></mrow><mo>+</mo><mrow xref='id5'><msqrt><mi xref='id4'>x</mi></msqrt></mrow></mrow><annotation-xml encoding='MathML-Content'><apply id='id6'><plus/><apply id='id3'><power/><ci id='id1'>x</ci><cn id='id2' type='integer'>2</cn></apply><apply id='id5'><root/><ci id='id4'>x</ci></apply></apply></annotation-xml><annotation encoding='Maple'>x^2+x^(1/2)</annotation></semantics></math>" 

Notice that the output elements contain the id and xref attributes for fine-grained cross-referencing. As explained under Using cross-references, the purposes of these attributes is to establish a correspondence between the content and presentation markup for each subexpression of the overall expression.

The following command produces content markup only:

MathML[ExportContent](x^2+sqrt(x));
"<math xmlns='http://www.w3.org/1998/Math/MathML'><apply id='id6\'><plus/><apply id='id3'><power/><ci id='id1'>x</ci><cn id\='id2' type='integer'>2</cn></apply><apply id='id5'><root/><ci id='id4'>x</ci></apply></apply></math>"

The following command produces presentation markup only:

MathML[ExportPresentation](x^2+sqrt(x));
"<math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><msqrt><mi>x</mi></msqrt></mrow></math>"

You can use the Print function contained in the XMLTools package to print the output in a tab-indented form in the worksheet, or XMLTools[PrintToFile](filename, %) to print the tab-indented output to a file. The % symbol is the Maple notation to represent the result of the last calculation you did in the worksheet. Here is an example showing the output from XMLTools:

XMLTools[Print](%)
<math xmlns='http://www.w3.org/1998/Math/MathML'>
  <mrow>
    <msup>
      <mi>x</mi>
      <mn>2</mn>
    </msup>
    <mo>+</mo>
    <msqrt>
      <mi>x</mi>
    </msqrt>
  </mrow>
</math>

Converting MathML to Maple syntax

The MathML package contains two functions for converting a MathML expression into a Maple expression. Both functions take as their argument a MathML expression specified as a string. The first is:

MathML[Import] (mmlstring) –

This converts into a Maple expression parallel combined markup that contains both the presentation MathML and content MathML representation of an expression. This function will also attempt a conversion on presentation markup in cases where it is possible to do so unambiguously. If the conversion fails, the function returns an error message. Here is an example showing the effect of MathML[Import]:

MathML[Import ("<math><msup><mi>x</mi><mn>2</mn></msup></math>");
x2

The second function is:

MathML[ImportContent] (mmlstring) –

This converts a content MathML expression into a Maple expression. If the conversion fails, Maple produces an error message. Here is an example showing the effect of MathML[ImportContent]:

MathML[ImportContent ("<math><apply><power/><ci>x</ci><cn>2</cn></apply></math>");
x2

   

<< back next >>

 

 

 


 

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