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 filename 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
"DocumentHeader",
"XMLDeclaration", or
"DOCTYPEDeclaration". The order in the list is irrelevant.
"XMLDeclaration"
When "XMLDeclaration" is one of the annotations, then an XML declaration, <?xml version="1.0"?>, is included in the header.
| Out[35]= |  |
|
"DOCTYPEDeclaration"
When "DOCTYPEDeclaration" 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[36]= |  |
|
"DocumentHeader"
With "Annotations"->{"DocumentHeader", "XMLDeclaration", "DOCTYPEDeclaration"}, the header automatically contains an XML declaration and a document type declaration for the DTD.
| Out[37]= |  |
|
When "Annotations" does not contain "DocumentHeader", the output has no header. This is true even if "Annotations" contains other elements such as "XMLDeclaration" or "DOCTYPEDeclaration".
| Out[38]= |  |
|
"Annotations"->"DocumentHeader" is useful for controlling the form of symbolic XML generated. Add an XMLElement[Document] to the symbolic XML output.
| Out[39]= |  |
|
When "Annotations"->{"DocumentHeader"} is not specified, XMLElement[Document] is omitted from the output.
| Out[40]= |  |
|
"AttributeQuoting"
| | |
| "AttributeQuoting" | ' | attribute values are enclosed by single quotes (default) |
| \" | attribute values are enclosed by double quotes |
Values for "AttributeQuoting".
The default setting, "AttributeQuoting"->"'", ensures that there is no conflict with Mathematica strings, which are typically enclosed in double quotes.
| Out[1]= |  |
|
To enclose attribute values in double quotes, set "AttributeQuoting"->"\"". The double-quote character must be preceded by a forward slash to escape it.
| Out[2]= |  |
|
"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 "CheckXML".
Set this option to
False if you are confident the symbolic XML is correct.
Checking the XML for errors can cause processing delays.
| Out[15]= |  |
| Out[16]= |  |
|
"CheckXML" 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.
| Out[17]= |  |
|
With "CheckXML"->False, nothing is output to the file.
| Out[19]= |  |
|
"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 "ElementFormatting".
An example using "ElementFormatting"->All.
| Out[20]= |  |
| Out[21]= |  |
|
With "ElementFormatting"->None, no extra indentation is added.
| Out[22]= |  |
|
With "ElementFormatting"->Automatic, elements with mixed content (strings as content) are not indented, while elements with element-only content are indented.
| Out[23]= |  |
|
With "ElementFormatting"->All, long strings are linewrapped. This can be used to produce output similar to HTML. On the other hand, ElementFormatting->Automatic produces one long line of text.
| Out[24]= |  |
|
Advanced users can also specify a function to determine formatting. The function is passed a two-element list, {namespace, localName}. 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[28]= |  |
|
"Entities"
When exporting XML documents, it is sometimes desirable to represent special characters using named character entities.
"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 "Entities".
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" or
"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 an  into the named character entity α.
| Out[29]= |  |
|
If you use your own list of character replacement rules, you are also responsible for including some basic escaping required by XML. For example:
"Entities"->{
"&"->"&",
"<"->"<",
">"->">",
"\""->""",
"'"->"'",
"h"->"&lowerCaseH;"
}
If you specify any value for
"Entities", 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[31]= |  |
|
Here, the entity is not declared and the literal output is retained.
| Out[32]= |  |
|
"NamespacePrefixes"
This option lets you generate XML markup with a specific namespace declaration and namespace prefixes. The option is specified in the form
"NamespacePrefixes"->{url->prefix}.
Use "NamespacePrefix" to generate presentation markup with each MathML element having a namespace prefix "mml" associated with the MathML namespace.
| Out[33]= |  |
|
"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 "PrefixCreation".
A sample of symbolic XML.
| Out[1]= |  |
|
With "PrefixCreation"->"True", any namespace prefix generated has the form nsi , where i is an index that starts at 0 and is incremented by 1 whenever another namespace is needed.
| Out[2]= |  |
|
With "PrefixCreation"->Automatic, a namespace declaration is automatically included only if the specified namespace has the form of a URL.
| Out[3]= |  |
| Out[4]= |  |
|
With "PrefixCreation"->False, the namespace is literally used as a namespace prefix.
| Out[5]= |  |
|
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.