5
$\begingroup$

Setup

Here's my mathML expression:

mathML = ExportString[1/2, "MathML"]
"<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <mfrac>
  <mn>1</mn>
  <mn>2</mn>
 </mfrac>
</math>"

and an XMLElement:

xml = XMLElement["tag", {}, {"data", mathML}]

Task and the problem

which I'd like to Export(String) without manipulating mathMLcontents but it converts some symbols:

ExportString[xml, "XML"] 

"<tag>data&lt;math \
xmlns=&apos;http://www.w3.org/1998/Math/MathML&apos;&gt;
 &lt;mfrac&gt;
  &lt;mn&gt;1&lt;/mn&gt;
  &lt;mn&gt;2&lt;/mn&gt;
 &lt;/mfrac&gt;
&lt;/math&gt;</tag>"

Expected result:

while I'd like to get:

"<tag>data<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <mfrac>
  <mn>1</mn>
  <mn>2</mn>
 </mfrac>
</math></tag>"

Solution(?)

I can get this with

ExportString[xml, "XML",  "Entities" -> {"<" -> "<", ">" -> ">", "'" ->"'"}]

but is this the right approach? If not, can I restrict this to only MathML expressions somehow?

A problem with solution:

The problem is that if those symbols are somewhere outside MathML expression, the resulting XML document will have incorrect structure:

ExportString[
  XMLElement["tag", {}, {"1 > 2"}], 
  "XML", 
  "Entities" -> {"<" -> "<", ">" -> ">", "'" -> "'"}
]
"<tag>1 > 2</tag>"

I suppose it is not acceptable. I'm not sure though.

$\endgroup$

1 Answer 1

2
$\begingroup$

After a bit, I hit upon a solution involving a round-trip through the import/export subsystem. First, it is helpful to have everything in the same representation, so we need to put the mathml into XMLElement form, e.g.

mathml = ImportString[ExportString[1/2, "MathML"],
 {"MathML", "XMLElement"}]
(*
 XMLElement["math", {"xmlns" -> "http://www.w3.org/1998/Math/MathML"}, 
 {XMLElement["mfrac", {}, {XMLElement["mn", {}, {"1"}], 
   XMLElement["mn", {}, {"2"}]}]}]
*)

Then, you can combine them as you wish,

xml = XMLElement["tag", {}, {"data", mathml}]
(*
 XMLElement["tag", {}, {"data", 
  XMLElement["math", {"xmlns" -> "http://www.w3.org/1998/Math/MathML"}, 
   {XMLElement["mfrac", {}, {XMLElement["mn", {}, {"1"}], 
  XMLElement["mn", {}, {"2"}]}]}]}]
*)

and when exported

ExportString[xml, "XML"]
(* "<tag>
 data<mathxmlns='http://www.w3.org/1998/Math/MathML'><mfrac><mn>1</mn><mn>2</mn></mfrac>
 </math></tag>"
*)

Note, it appears to lose formatting, and initially I was under the impression that the text could not be a sibling to an xml element, but re-reading the xml spec, implies that it is, instead, being treated as an inline element.

$\endgroup$
4
  • $\begingroup$ I get no error on 10.2 in Windows with ExportString[ XMLElement[ "tag", {}, {"data", ImportString[ ExportString[#, "MathML"], {"MathML", "XMLElement"}]}], "XML"] &@(1/2). It produces nicely <tag>data<math xmlns='http://www.w3.org/1998/Math/MathML'><mfrac><mn>1</mn><mn>2</mn></mfrac></math></tag> $\endgroup$ Commented Jul 28, 2015 at 13:59
  • $\begingroup$ @RolfMertig look closer, the mathml is being interpreted as a string. If it were being interpreted as xml, it would be formatted differently. $\endgroup$
    – rcollyer
    Commented Jul 28, 2015 at 14:00
  • $\begingroup$ but the result is like like "Expected result" ,or? $\endgroup$ Commented Jul 28, 2015 at 14:03
  • $\begingroup$ @RolfMertig looking through the xml spec now (it's been over a decade since I've read it), and it looks like I may be incorrect: "Text consists of intermingled character data and markup." So, likely the mathml is being treated as inline markup. Will update my answer. $\endgroup$
    – rcollyer
    Commented Jul 28, 2015 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.