Exporting XML

Functions for Exporting XML

Export

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.
In[1]:=
Click for copyable input
Out[1]=
With "MathML" specified, the same expression is written out as MathML.
In[3]:=
Click for copyable input
Out[3]=
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"].
In[5]:=
Click for copyable input
Out[5]=
The .mml extension is associated with MathML.
In[7]:=
Click for copyable input
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.
In[9]:=
Click for copyable input

ExportString

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.
In[11]:=
Click for copyable input
Out[11]=
If a symbolic XML expression is supplied as the first argument of ExportString, the output is ordinary XML.
In[12]:=
Click for copyable input
Out[12]=
If the first argument is some other type of expression, the output is ExpressionML.
In[13]:=
Click for copyable input
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.
In[14]:=
Click for copyable input
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.
In[15]:=
Click for copyable input
Out[15]=

"DocumentHeader"

With , the header automatically contains an XML declaration and a document type declaration for the DTD.
In[16]:=
Click for copyable input
Out[16]=
When does not contain , the output has no header. This is true even if contains other elements such as or .
In[17]:=
Click for copyable input
Out[17]=
is useful for controlling the form of symbolic XML generated. Add an XMLElement[Document] to the symbolic XML output.
In[18]:=
Click for copyable input
Out[18]=
When is not specified, XMLElement[Document] is omitted from the output.
In[19]:=
Click for copyable input
Out[19]=

"AttributeQuoting"

option
value
effect
"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 Wolfram Language strings, which are typically enclosed in double quotes.
In[20]:=
Click for copyable input
Out[20]=
To enclose attribute values in double quotes, set . The double-quote character must be preceded by a forward slash to escape it.
In[21]:=
Click for copyable input
Out[21]=

"CheckXML"

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

option
value
effect
"CheckXML"Truethe file being exported is checked for errors (default)
Falsethe 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.
In[22]:=
Click for copyable input
In[23]:=
Click for copyable input
Out[23]=
In[24]:=
Click for copyable input
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.
In[25]:=
Click for copyable input
Out[25]=
With "CheckXML"->False, nothing is output to the file.
In[27]:=
Click for copyable input
Out[27]=
Out[19]=

"ElementFormatting"

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 .

An example using "ElementFormatting"->All.
In[28]:=
Click for copyable input
Out[28]=
In[29]:=
Click for copyable input
Out[29]=
With "ElementFormatting"->None, no extra indentation is added.
In[30]:=
Click for copyable input
Out[30]=
With "ElementFormatting"->Automatic, elements with mixed content (strings as content) are not indented, while elements with element-only content are indented.
In[31]:=
Click for copyable input
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.
In[32]:=
Click for copyable input
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.
In[33]:=
Click for copyable input
In[36]:=
Click for copyable input
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.

option
value
effect
"Entities""HTML"resolve entity names defined in HTML
"MathML"resolve entity names defined in MathML
lista 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" 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;.
In[37]:=
Click for copyable input
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.
In[38]:=
Click for copyable input
Out[38]=
Here, the entity is not declared and the literal output is retained.
In[39]:=
Click for copyable input
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.
In[40]:=
Click for copyable input
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.

option
value
effect
"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 .

A sample of symbolic XML.
In[42]:=
Click for copyable input
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.
In[43]:=
Click for copyable input
Out[43]=
With "PrefixCreation"->Automatic, a namespace declaration is automatically included only if the specified namespace has the form of a URL.
In[44]:=
Click for copyable input
Out[44]=
In[45]:=
Click for copyable input
Out[45]=
With "PrefixCreation"->False, the namespace is literally used as a namespace prefix.
In[46]:=
Click for copyable input
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.