Programmatic Worksheet Access
This worksheet demonstrates some of the features of the Worksheet package. This package provides support for accessing Maple worksheets programmatically. As you read this worksheet, execute the commands in sequence.
restart:
Accessing the Worksheet Package
To use the Worksheet package, you must first issue the following call to the procedure with, which provides interactive access to procedures organized in packages.
with(Worksheet);
Comparator,Convert,Display,DisplayFile,FromString,ReadFile,ToString,WorksheetToMapleText,WriteFile,worksheet
Importing a Worksheet As an XML Document
Maple worksheets are represented in Maple as XML documents. This makes it unnecessary to know the (undocumented) storage format for the Maple worksheet and allows you to use new and existing tools for manipulating XML document instances. (For details, see XMLTools.) To create an XML representation of a worksheet, use the ReadFile and FromString commands.
wname := FileTools:-JoinPath([kernelopts(mapledir), "examplesclassic","WorksheetPackage.mws"]):
doc := ReadFile(wname):
Converting a Worksheet to Different Formats
Note that Maple worksheets can be imported into Maple in two different formats: -- "mw", this is the same format that is used by the Standard worksheet,
-- "maple8_xml", the format introduced in Maple 8.
Option `format` can be used to specify which XML representation to generate.
doc2 := ReadFile(wname, format = "maple8_xml"):
Display the worksheet you loaded by using the Display command.
# Display(doc);
# Display(doc2);
Use the Convert utility to convert worksheets in "mw" format to worksheets in "maple8_xml" format and back.
doc3 := Convert(doc, format="maple8_xml"):
evalb(doc2 = doc3);
false
l := XMLTools:-Compare(doc2, doc3);
l:=1,3,1,2,1,2,1
if l <> [] then [op(l[1..-3], doc2), op(l[1..-3], doc3)] end if;
_XML_Attribute⁡_XML_AttrName⁡major,_XML_AttrValue⁡6,_XML_Attribute⁡_XML_AttrName⁡major,_XML_AttrValue⁡12
To convert a worksheet to ".mws" format, use the ToString command.
str := ToString(doc, format = "mws"):
Accessing a Worksheet By Using the XMLTools Package
Now that you have the worksheet exported as an XML document, you can use functions from the XMLTools package to access its subnodes.
XMLTools[FirstChild](doc);
_XML_Text⁡
XMLTools[SecondChild](doc);
_XML_Element⁡_XML_ElementType⁡Version,_XML_Attribute⁡_XML_AttrName⁡major,_XML_AttrValue⁡12,_XML_Attribute⁡_XML_AttrName⁡minor,_XML_AttrValue⁡0,
XMLTools[AttributeTable](%);
tablemajor=12,minor=0
XMLTools[ThirdChild](doc);
XMLTools[ElementStatistics](doc);
View-Properties=1,Label-Scheme=1,Version=1,Task=1,Styles=1,Worksheet=3,Section=4,Title=6,Equation=10,Output=10,Hyperlink=21,Layout=24,Input=26,Group=26,Text-field=45,Font=112
XMLTools[ContentModelCount](doc);
27
Return to Index for Example Worksheets
Download Help Document