XML.mws
XML Support
by Waterloo Maple, 2001
Maple 7 provides support for creation, manipulation, import and export of XML data through its
XMLTools
package The package allows you to publish Maple solutions as XML data files or HTML/XHTML web pages, or import XML data files into your Maple application and bring all of Maple's power to bear on its analysis.
Below are examples of using the
XMLTools
package to create and publish an HTML and an XML data file that list the first 10 prime numbers, using Maple to generate the primes.
(For an example showing the importation and manipulation of XML data in a Maple 7 worksheet, see the demo
Web Connectivity with TCP/IP Sockets.)
>
with( XMLTools );
Example 1: Creating an HTML data file and publishing it to the Web
We create an HTML data file listing the first 10 prime numbers, using Maple to generate the primes. Then we publish the page to the Web.
To print out "1st prime, 2nd prime, 3rd prime, 4th prime, ...", we tell Maple the proper English suffixes for the integers 1 through 10.
>
suffixes := ["st", "nd", "rd", seq("th", i=4..10)];
We create a Maple expression containing tags for "HTML", "HEAD", "TITLE", etc. To open an HTML/XML tag, use the
Element
command. Use a right-parenthesis to close the tag. Of course,
Element
tags can be nested.
Each list element in the UL list invokes the Maple command
ithprime( )
to compute the next prime number. Of course, we could embed any of Maple's mathematical functionality into the list elements. Prime numbers provide merely one example.
>
myHomePage :=
Element( "HTML",
Element( "HEAD",
Element( "TITLE", "My Home Page" ) ),
Element( "BODY",
Element( "B", "Here are the first 10 prime numbers" ),
Element( "UL",
seq(Element("LI",cat("The ",i,suffixes[ i ]," prime number is ",ithprime(i))),
i=1..10))));
Now we use the
XMLTools:-Print
command to print out the above Maple expression as tagged HTML code.
>
Print( myHomePage );
<HTML>
<HEAD>
<TITLE>My Home Page</TITLE>
</HEAD>
<BODY>
<B>Here are the first 10 prime numbers</B>
<UL>
<LI>The 1st prime number is 2</LI>
<LI>The 2nd prime number is 3</LI>
<LI>The 3rd prime number is 5</LI>
<LI>The 4th prime number is 7</LI>
<LI>The 5th prime number is 11</LI>
<LI>The 6th prime number is 13</LI>
<LI>The 7th prime number is 17</LI>
<LI>The 8th prime number is 19</LI>
<LI>The 9th prime number is 23</LI>
<LI>The 10th prime number is 29</LI>
</UL>
</BODY>
</HTML>
We now write this HTML code to a file and publish it on the web with the
XMLTools:-WriteFile
command.
>
WriteFile("C:/temp/primeNumbers.html", myHomePage):
fclose
tells Maple we're finished writing to the file. (Note this is required for proper generation of the file.)
View the HTML output
>
fclose( "C:/temp/primeNumbers.html" );
Maple can count how many tags of each type are contained in the XHMTL code.
>
ElementStatistics( myHomePage );
Example 2: Creating a generic XML data file
Let's repeat Example 1 with the exception that we customize the names of the tags to reflect our current application. That is, the code is no longer strict HTML but generic XML. This illustrates the capacity of the XML standard to represent data files of any format.
>
myHomePage :=
Element( "MATH_DATA",
Element( "HEADER_INFO",
Element( "FILE_NAME", "My Home Page" ) ),
Element( "DATA",
Element( "DESCRIPTION", "Here are the first 10 prime numbers" ),
Element( "PRIMELISTING",
seq(Element("NEXTPRIME",cat("The ",i,myStrings[ i ]," prime number is ",ithprime(i))),
i=1..10))));
>
Print( myHomePage );
<MATH_DATA>
<HEADER_INFO>
<FILE_NAME>My Home Page</FILE_NAME>
</HEADER_INFO>
<DATA>
<DESCRIPTION>Here are the first 10 prime numbers</DESCRIPTION>
<PRIMELISTING>
<NEXTPRIME>The 1st prime number is 2</NEXTPRIME>
<NEXTPRIME>The 2nd prime number is 3</NEXTPRIME>
<NEXTPRIME>The 3rd prime number is 5</NEXTPRIME>
<NEXTPRIME>The 4th prime number is 7</NEXTPRIME>
<NEXTPRIME>The 5th prime number is 11</NEXTPRIME>
<NEXTPRIME>The 6th prime number is 13</NEXTPRIME>
<NEXTPRIME>The 7th prime number is 17</NEXTPRIME>
<NEXTPRIME>The 8th prime number is 19</NEXTPRIME>
<NEXTPRIME>The 9th prime number is 23</NEXTPRIME>
<NEXTPRIME>The 10th prime number is 29</NEXTPRIME>
</PRIMELISTING>
</DATA>
</MATH_DATA>