-
MathType
-
WirisQuizzes
-
Nubric
-
CalcMe
-
MathPlayer
-
Store FAQ
-
MathFlow
-
BF FAQ
-
Miscellaneous
-
Wiris Integrations
Modified XML encoding for MathML
Reading time: 2minReference describing the modified XML encoding used by MathType to safely store MathML in environments that cannot contain raw XML.
Summary
MathML is an XML-based language. Some platforms and HTML contexts cannot safely store raw XML because reserved XML characters may be filtered, escaped incorrectly, or interpreted as HTML markup.
To avoid these issues, MathType uses a modified XML encoding that replaces the five reserved XML characters with alternative Unicode characters. The original MathML can be restored by reversing these substitutions.
Why a modified encoding is used
MathType stores formulas as MathML. In some environments, however, embedding raw MathML directly is not possible. Typical examples include:
- HTML attributes (such as
data-mathml). - HTML editors that sanitize unknown XML tags.
- Content management systems that remove embedded XML.
- Legacy platforms that treat MathML as invalid HTML.
Using the modified encoding preserves MathML while allowing it to be safely stored within HTML content.
Character mapping
The following substitutions are applied.
| XML character | Replacement character |
|---|---|
< |
« |
> |
» |
& |
§ |
" |
¨ |
' |
` |
To recover the original MathML, replace each encoded character with its corresponding XML character.
When decoding modified XML, handle the encoded MathML as a plain text string and reverse the character substitutions before parsing the MathML or decoding HTML/XML character references.
In particular, do not decode character references such as ¨ before reversing the substitutions. Doing so may turn them into characters used by the encoding itself, resulting in incorrect or invalid MathML.
Example
Original MathML:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mfenced open="<" close=">"><mi>x</mi></mfenced></math>Stored using the modified encoding:
«math xmlns=¨http://www.w3.org/1998/Math/MathML¨»«mfenced open=¨§lt;¨ close=¨§gt;¨»«mi»x«/mi»«/mfenced»«/math»Notice that the ampersands in the < and > character references are also encoded as §. The character references must remain encoded until the modified XML substitutions have been reversed.
Typical use cases
The modified encoding is commonly found in:
- The
data-mathmlattribute of legacy MathType image formulas. - Exported HTML content.
- Integrations that store MathML inside HTML attributes.
See Migrate MathType image formulas to MathML for a practical migration example.
Decoding
Decoding simply consists of reversing the character substitutions listed above.
| From | To |
|---|---|
« |
< |
» |
> |
§ |
& |
¨ |
" |
` |
' |
Apply these substitutions directly to the encoded text before parsing the resulting MathML or decoding any HTML/XML character references. For example:
«mfenced open=¨§lt;¨ close=¨§gt;¨»first becomes:
<mfenced open="<" close=">">The resulting string can then be processed as normal MathML.
Legacy XSLT example
Earlier versions of the documentation included an XSLT stylesheet that demonstrated how to automatically reverse the modified encoding. The same result can be achieved with any XML or text-processing library that supports the character substitutions shown above.
Notes
- This encoding does not modify the MathML structure or semantics.
- Only the five reserved XML characters are replaced.
- encoding is fully reversible when the substitutions are reversed before parsing or otherwise transforming the encoded content.
- New integrations working directly with MathML usually do not need to apply this encoding manually.