Exporting XML
Functions for Exporting XML
Export
You can export XML data from Mathematica using the standard Export function.
| Export[file,expr] | export to a format determined by file extension |
| Export[file,expr,format] | export to a specific format |
Exporting files.
The first argument of the function specifies the file to which the data should be exported. The second argument specifies the data to be exported. For exporting XML data, this can be a symbolic XML expression or any other Mathematica expression. You can also specify an optional third argument to control the form of the output. For exporting XML data, the relevant file formats are "XML", "ExpressionML", "MathML", and "SVG".
With
"XML" as the format, all expressions are exported as ExpressionML.
| Out[1]= |  |
With
"MathML" specified, the same expression is written out as MathML.
| Out[3]= |  |
With only two arguments,
Mathematica determines the export format based on the file name extension. .xml is associated with XML.
Export[filename.xml, expr] is equivalent to
Export[filename.xml, expr, "XML"].
| Out[5]= |  |
The .mml extension is associated with MathML.
| Out[7]= |  |
You can control details of the export process using options for Export.
Delete the test files created by evaluating the commands in this section.
ExportString
You can convert Mathematica expressions into XML strings using ExportString.
Exporting strings.
For exporting as XML, the relevant formats are "XML", "ExpressionML", "MathML", and "SVG".
Produce a symbolic XML expression.
| Out[11]= |  |
If a symbolic XML expression is supplied as the first argument of
ExportString, the output is ordinary XML.
| Out[12]= |  |
If the first argument is some other type of expression, the output is ExpressionML.
| Out[13]= |  |
You can control details of the export process using options for ExportString.
Export Options
Introduction
Standard options for Export or ExportString can be used for greater control over the export process.
| Export[file,expr,format,option1->value1,option2->value2,...] |
| export to a file |
| ExportString[expr,format,option1->value1,option2->value2,...] |
| export to a string |
Options for exporting XML data:
"Annotations"
This option controls which annotations are added to the output XML. The value is a list whose elements can be any combination of
,
, or
. The order in the list is irrelevant.
"XMLDeclaration"
When

is one of the annotations, then an XML declaration,
<?xml version="1.0"?>, is included in the header.
| Out[14]= |  |
"DOCTYPEDeclaration"
When

is one of the annotations, then an XML document type declaration of the form
<!DOCTYPE ... > appears in the header. This specifies the DTD for the XML application in which the output is written.
| Out[15]= |  |
"DocumentHeader"
With

, the header automatically contains an XML declaration and a document type declaration for the DTD.
| Out[16]= |  |
When

does not contain

, the output has no header. This is true even if

contains other elements such as

or

.
| Out[17]= |  |

is useful for controlling the form of symbolic XML generated. Add an
XMLElement[Document] to the symbolic XML output.
| Out[18]= |  |
When

is not specified,
XMLElement[Document] is omitted from the output.
| Out[19]= |  |
"AttributeQuoting"
| | |
| "AttributeQuoting" | ' | attribute values are enclosed by single quotes (default) |
| \" | attribute values are enclosed by double quotes |
Values for
.
The default setting,

, ensures that there is no conflict with
Mathematica strings, which are typically enclosed in double quotes.
| Out[20]= |  |
To enclose attribute values in double quotes, set

. The double-quote character must be preceded by a forward slash to escape it.
| Out[21]= |  |
"CheckXML"
This option determines whether the symbolic XML expression being exported is first checked for errors.
| | |
| "CheckXML" | True | the file being exported is checked for errors (default) |
| False | the file being exported is not checked for errors |
Values for
.
Set this option to False if you are confident the symbolic XML is correct.
Checking the XML for errors can cause processing delays.
| Out[23]= |  |
| Out[24]= |  |
can be quite useful, since a small error may completely ruin the exported form of a large symbolic XML expression. With the option on, small errors can often be fixed.
Here the symbolic XML expression has an error, but it is fixed to give a reasonable result.
With
"CheckXML"->False, nothing is output to the file.
"ElementFormatting"
This option controls how elements are indented in the XML file.
| All | all child nodes of an element are indented and long lines are wrapped |
| None | no extra indentation is added |
| Automatic | elements with mixed content are not indented, while elements with element-only content are indented (default) |
| fn | a user-defined function |
Values for
.
An example using
"ElementFormatting"->All.
| Out[28]= |  |
| Out[29]= |  |
With
"ElementFormatting"->None, no extra indentation is added.
| Out[30]= |  |
With
"ElementFormatting"->Automatic, elements with mixed content (strings as content) are not indented, while elements with element-only content are indented.
| Out[31]= |  |
With
"ElementFormatting"->All, long strings are line-wrapped. This can be used to produce output similar to HTML. On the other hand,
ElementFormatting->Automatic produces one long line of text.
| Out[32]= |  |
Advanced users can also specify a function to determine formatting. The function is passed a two-element list,

. The function should return
True when indenting is wanted,
False when no indenting is wanted, and
Automatic when the element-only content should be indented and mixed content should not be indented.
| Out[36]= |  |
"Entities"
When exporting XML documents, it is sometimes desirable to represent special characters using named character entities.
supports output of named character entities.
| | |
| "Entities" | "HTML" | resolve entity names defined in HTML |
| "MathML" | resolve entity names defined in MathML |
| list | a list of character replacement rules |
Values for
.
You can also specify a list as the value of this option. For example, if you want to export both HTML and MathML entities, use "Entities"->{"HTML", "MathML"}. If neither the "HTML" nor "MathML" settings are used, all characters are still output correctly in XML. However, they may be numeric entities or encoded in UTF-8.
Use the
"HTML" setting to turn

into the named character entity
α.
| Out[37]= |  |
If you use your own list of character replacement rules, you are also responsible for including some basic escaping required by XML. For example:
If you specify any value for
, ensure that appropriate entity declarations are present. For example, by using the "HTML" setting, you can easily generate XML with HTML entities.
The Icelandic character "thorn" is exported as the corresponding character entity reference.
| Out[38]= |  |
Here, the entity is not declared and the literal output is retained.
| Out[39]= |  |
"NamespacePrefixes"
This option lets you generate XML markup with a specific namespace declaration and namespace prefixes. The option is specified in the form
.
Use

to generate presentation markup with each MathML element having a namespace prefix
"mml" associated with the MathML namespace.
| Out[40]= |  |
"PrefixCreation"
This option controls whether a namespace declaration should be automatically generated when exporting tags that have a namespace associated with them, but no namespace declaration is present in the symbolic XML.
| | |
| "PrefixCreation" | True | generate a namespace declaration for any elements that have a namespace prefix but no corresponding namespace declaration |
| False | do not generate a namespace declaration for any element |
| Automatic | generate a namespace declaration for any elements that have a namespace prefix in the form of a URL |
Values for
.
A sample of symbolic XML.
| Out[42]= |  |
With

, any namespace prefix generated has the form

, where
i is an index that starts at 0 and is incremented by 1 whenever another namespace is needed.
| Out[43]= |  |
With
"PrefixCreation"->Automatic, a namespace declaration is automatically included only if the specified namespace has the form of a URL.
| Out[44]= |  |
| Out[45]= |  |
With
"PrefixCreation"->False, the namespace is literally used as a namespace prefix.
| Out[46]= |  |
The XML produced in this case is not meaningful because there is no namespace declaration of the form xmlns:prefix = "http://www.mydoc.net/2001/XML". This is desirable when you are exporting the XML as a fragment to be enclosed in an outer piece of XML, for which the namespace has a binding.