Exporting XML

Functions for Exporting XML


You can export XML data from the Wolfram Language 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 Wolfram Language 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.
With "MathML" specified, the same expression is written out as MathML.
With only two arguments, the Wolfram Language 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"].
The .mml extension is associated with MathML.

You can control details of the export process using options for Export.

Delete the test files created by evaluating the commands in this section.


You can convert Wolfram Language expressions into XML strings using ExportString.

ExportString[expr,format]export to a string

Exporting strings.

For exporting as XML, the relevant formats are "XML", "ExpressionML", "MathML", and "SVG".

Produce a symbolic XML expression.
If a symbolic XML expression is supplied as the first argument of ExportString, the output is ordinary XML.
If the first argument is some other type of expression, the output is ExpressionML.

You can control details of the export process using options for ExportString.

Export Options


Standard options for Export or ExportString can be used for greater control over the export process.

export to a file
export to a string

Options for exporting XML data:


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.


When "XMLDeclaration" is one of the annotations, then an XML declaration, <?xml version="1.0"?>, is included in the header.


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.


With "Annotations"->{"DocumentHeader","XMLDeclaration","DOCTYPEDeclaration"}, the header automatically contains an XML declaration and a document type declaration for the DTD.
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".
"Annotations"->"DocumentHeader" is useful for controlling the form of symbolic XML generated. Add an XMLElement[Document] to the symbolic XML output.
When "Annotations"->{"DocumentHeader"} is not specified, XMLElement[Document] is omitted from the output.


"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 Wolfram Language strings, which are typically enclosed in double quotes.
To enclose attribute values in double quotes, set "AttributeQuoting"->"\"". The double-quote character must be preceded by a forward slash to escape it.


This option determines whether the symbolic XML expression being exported is first checked for errors.

"CheckXML"Truethe file being exported is checked for errors (default)
Falsethe 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.

"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.
With "CheckXML"->False, nothing is output to the file.


This option controls how elements are indented in the XML file.

Allall child nodes of an element are indented and long lines are wrapped
Noneno extra indentation is added
Automaticelements with mixed content are not indented, while elements with element-only content are indented (default)
fna userdefined function

Values for "ElementFormatting".

An example using "ElementFormatting"->All.
With "ElementFormatting"->None, no extra indentation is added.
With "ElementFormatting"->Automatic, elements with mixed content (strings as content) are not indented, while elements with element-only content are indented.
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.
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.


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
lista 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" nor "MathML" setting is 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 &alpha;.

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 "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.
Here, the entity is not declared and the literal output is retained.


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.


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"Truegenerate a namespace declaration for any elements that have a namespace prefix but no corresponding namespace declaration
Falsedo not generate a namespace declaration for any element
Automaticgenerate 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.
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.
With "PrefixCreation"->Automatic, a namespace declaration is automatically included only if the specified namespace has the form of a URL.
With "PrefixCreation"->False, the namespace is literally used as a namespace prefix.

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.