Using MathML as input

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

All the examples of MSP scripts shown so far require you to specify input in Mathematica syntax. However, you can also use webMathematica to perform computations on input specified as MathML. This allows you, for example, to copy a MathML expression from another application, paste it into a form in a Web page, and then perform some computation on it (such as differentiating the expression or generating a plot).

The basic procedure for doing this is to convert the MathML string supplied as input into the equivalent Mathematica expression. You can do this by using the function XML`MathML`MathMLToExpression, which is the converse of the XML`MathML`ExpressionToMathML function used in the example An HTML document for converting an expression into MathML. Once a Mathematica expression is obtained, you can perform a wide variety of computations on it by applying any of the Mathematica functions for algebra, calculus, graphics, and so on. The following example shows an MSP script for calculating the integral of a function of one variable. The following figure shows how this script looks when viewed in IE.

Example: An HTML document for integrating a function specified in MathML form.

<html>
  <head>
    <title>Integrating MathML</title>
  </head>
  <body>
    <h1>Integrating a MathML Expression</h1>
    <form method="post">
      <p>Enter some MathML:</p>
      <p>
        <textarea name="input" rows="10" cols="50">
          <%Mathlet StringReplace[MSPValue[$$input,"<math><msup><mi>x</mi><mn>2</mn></msup></math>"],{"\n"->"","&" -> "&amp;", "<" -> "&lt;", ">" -> "&gt;", "'"->"&#39;", "\""->"&quot;"}] %>
        </textarea>
      </p>
      <input type="submit" name="inputButton" value="Integrate" /> 
      <p><%Mathlet MSPFormat[Integrate[XML`MathML`MathMLToExpression[$$input],x],TraditionalForm] %></p>
    </form>
  </body>
</html>

webMathematica: Calculating integral of function specified in MathML

Figure: Calculating the integral of a function specified in MathML.

The example above works as follows. The user enters a MathML string in the text box provided and then clicks the "Integrate" button. The Mathematica function StringReplace is applied to the MathML string to remove linebreaks and replace various special characters with the corresponding entity references. This is to prevent these special characters from being automatically replaced in certain browsers. The MathML string obtained after these substitutions are made is then supplied as an argument to the XML`MathML`MathMLToExpression function, which translates the MathML into the corresponding Mathematica expression. Finally, the Integrate function is used to calculate the integral, and the MSPFormat function is used to display the result in TraditionalForm.

You can easily modify this example for doing other types of computation on MathML input. The following example shows an MSP script for plotting a function of one variable specified as a MathML string. The following figure shows how this script looks when viewed in IE.

Example: An HTML document for plotting a function specified in MathML form.

<html>
  <head><title>Plotting MathML</title></head>
  <body>
    <h1>Plotting a MathML Expression</h1>
    <form method="post">
      <p>Enter some MathML :</p>
      <p>
        <textarea name="input" rows="10" cols="50">
          <%Mathlet StringReplace[MSPValue[$$input,"<math><apply><sin/><ci>x</ci></apply></math>"],{"\n"->"","&" -> "&amp;", "<" -> "&lt;", ">" -> "&gt;", "'"->"&#39;", "\""->"&quot;"}] %>
        </textarea>
      </p>
      <p>Enter the start of the range: <input type="text" name="x1" size="10" value="<%Mathlet MSPValue[$$x1, "0"] %>" /></p>
      <p>Enter the end of the range: <input type="text" name="x2" size="10" value="<%Mathlet MSPValue[ $$x2, "10"] %>" /></p>
      <input type="submit" name="inputButton" value="Plot" /> 
      <p><%Mathlet MSPBlock[{$$x1,$$x2}, MSPShow[Plot[XML`MathML`MathMLToExpression[$$input],{x,$$x1,$$x2}]]]%></p>
    </form>
  </body>
</html>

webMathematica: Plotting function specified in MathML

Figure: Plotting a function specified in MathML.

This example is similar to the example An HTML document for plotting a function using webMathematica. The only difference is that the function to be plotted is specified as a MathML string instead of a Mathematica expression. As in the example An HTML document for converting an expression into MathML, the MathML string is processed and converted into a Mathematica expression. The Plot function then produces the plot, and the MSPShow function displays an image of the plot in the Web browser.

The examples given here can be readily extended to provide access to calculations of any degree of complexity directly from a Web page. All the details for sending input to Mathematica and returning output back to the Web page are handled automatically by webMathematica, so the user does not have to worry about the low-level implementation details. Also, since the Mathlet tags can be embedded directly in the HTML document in a fairly natural and seamless way, whoever is maintaining the Web site can choose the tools for generating and maintaining Web pages.

   

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