Tricks of the Trade
In and Out
Download This Issue
Mathematica 4.2: A Technical Review
Mathematica 4.2 introduced powerful new functionality for importing, manipulating, and exporting XML. You can import any XML document and then use Mathematica’s built-in functions to analyze the contents of the document or transform its structure. You can also export notebooks in XML format using the NotebookML DTD defined for this purpose. Mathematica 4.2 also includes support for specific XML formats such as MathML, SVG, and XHTML for exporting mathematical formulas, graphics, and complete notebooks, respectively.
Manipulating XML Using SymbolicXML
When you import an XML document, it is automatically converted into Symbolic XML, a special type of Mathematica expression that preserves the structure of the original XML document. Since both XML and Mathematica expressions have a tree structure, it is easy to define a mapping between them. The advantage of converting an XML document to SymbolicXML is that you can then use all the standard techniques of Mathematica programming, including functional programming and pattern matching to extract or analyze specific pieces of data from the XML document. Let us look at some examples.
This imports an XML file containing data on baseball players. You can download this file from the TMJ website at www.mathematica-journal.com/issue/v9i1.
With some knowledge of how the particular data set is structured, you can construct parsing functions to extract specific pieces of data from the file. This example extracts the entries corresponding to players for the New York Yankees.
The data set contains the names of 21 players who are hitters for the Yankees.
The following function can be applied to the data in order to obtain a list of player names that fit the criteria specified earlier.
You can fetch XML data over the web using the XML`Parser`XMLGet function. Here is a function that extracts data from the Amazon.com website.
This searches for all books listed on Amazon.com that contain the word “mathematica” in the title.
The following command sorts the search results in order of their Amazon sales rank and displays them in a table.
Exporting Notebooks as XML
You can save any notebook in XML format by choosing the File > Save As > XML (NotebookML) menu command. NotebookML is an XML format developed by Wolfram Research for representing notebooks. Here is a simple notebook expression.
This exports the notebook expression as NotebookML. Notice the correspondence between the different parts of the notebook expression and the element names in the NotebookML representation.
When you import the NotebookML file back into Mathematica, it is automatically converted into a notebook.
You can also export specific parts of a notebook in XML format, using another XML format called ExpressionML. As you would expect, ExpressionML is a subset of NotebookML. Here is the ExpressionML for a simple mathematical formula.
Working with MathML
Mathematica 4.2 included comprehensive support for the MathML 2.0 standard for describing mathematics in machine-to-machine communication. Endorsed and recommended by the World Wide Web Consortium (W3C) and developed with strong support from Wolfram Research, MathML makes it possible to embed mathematical expressions in web pages and XML documents.
You can export a notebook as an XHTML document with all formulas in the notebook automatically converted into MathML by choosing File > Save As > HTML+MathML. The resulting document can then be displayed in any browser that supports MathML, such as Mozilla 1.0, Netscape 7.0, or Internet Explorer 5.5 (using the freely distributed MathPlayer plug-in).
You can also select a formula and choose Edit > Copy As > MathML to copy its MathML representation to the Clipboard. Conversely, if you paste a MathML string into a notebook, it can be automatically interpreted and converted into a Mathematica expression. This makes it possible, for example, to copy a mathematical formula from a web page, paste it into a notebook, and evaluate it as standard Mathematica input.
There are also several kernel functions for converting between MathML and the boxes and expressions used by Mathematica to represent mathematical formulas. For example, XML`MathML`ExpressionToMathML yields the MathML representation of its argument.
There are several conversion options for customizing the output. For example, by default, the above function produces parallel markup containing both content and presentation MathML. You can get only presentation markup by explicitly specifying the "Formats" conversion option.
You can use the function XML`MathML`MathMLToExpression to do the reverse transformation, that is, from MathML to a Mathematica expression.
About Mathematica Download Mathematica Player
Copyright © Wolfram Media, Inc. All rights reserved.