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 |
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".
You can control details of the export process using options for Export.
ExportString
You can convert Wolfram Language expressions into XML strings using ExportString.
ExportString[expr,format] | export to a string |
For exporting as XML, the relevant formats are "XML", "ExpressionML", "MathML", and "SVG".
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"
"DOCTYPEDeclaration"
"DocumentHeader"
"AttributeQuoting"
option | value | effect |
"AttributeQuoting" | ' | attribute values are enclosed by single quotes (default) |
\" | attribute values are enclosed by double quotes |
Values for "AttributeQuoting".
"CheckXML"
This option determines whether the symbolic XML expression being exported is first checked for errors.
option | value | effect |
"CheckXML" | True | the file being exported is checked for errors (default) |
False | the file being exported is not checked for errors |
Set this option to False if you are confident the symbolic XML is correct.
"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.
"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".
"Entities"
When exporting XML documents, it is sometimes desirable to represent special characters using named character entities. "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 | |
list | a list of character replacement rules |
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.
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.
"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}.
"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" | 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 |
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.