I'm trying to create equation text box. For this I found mathdox editor which I'm using to preview the equation graphically and convert it to mathml to preview the equation later on web page. The question is how can I convert OpenMath to MathML in c#? I'm terrible in javascript, so I prefer to send openmath code to server and process every thing there. Have anyone done this?
Both OpenMath and MathML are xml-based markup languages, so to convert one to another first thing that comes to mind is xsl transformations. After a little research you can find xslt stylesheets to do that here (see "XSLT stylesheets for conversion between OpenMath and Content MathML" section). In that zip archive there are 4 files, you need "omtocmml.xsl".
Now let's take sample OpenMath from wikipedia:
<OMOBJ xmlns="http://www.openmath.org/OpenMath">
<OMA cdbase="http://www.openmath.org/cd">
<OMS cd="relation1" name="eq"/>
<OMV name="x"/>
<OMA>
<OMS cd="arith1" name="divide"/>
<OMA>
<OMS cdbase="http://www.example.com/mathops" cd="multiops" name="plusminus"/>
<OMA>
<OMS cd="arith1" name="unary_minus"/>
<OMV name="b"/>
</OMA>
<OMA>
<OMS cd="arith1" name="root"/>
<OMA>
<OMS cd="arith1" name="minus"/>
<OMA>
<OMS cd="arith1" name="power"/>
<OMV name="b"/>
<OMI>2</OMI>
</OMA>
<OMA>
<OMS cd="arith1" name="times"/>
<OMI>4</OMI>
<OMV name="a"/>
<OMV name="c"/>
</OMA>
</OMA>
</OMA>
</OMA>
<OMA>
<OMS cd="arith1" name="times"/>
<OMI>2</OMI>
<OMV name="a"/>
</OMA>
</OMA>
</OMA>
</OMOBJ>
Here is the sample code to apply transformation (it assumes you pass it string with your OpenMath, and assumes omtocmml.xsl is in application directory). Note that this is just a sample, in real life you may want to store xsl in assembly resources, and also may want to use one precompiled XslCompiledTransform and not create it every time.
public static string OpenMathToMathML(string source) {
var trans = new XslCompiledTransform();
using (var fs = File.OpenRead("omtocmml.xsl")) {
var settings = new XmlReaderSettings
{
DtdProcessing = DtdProcessing.Parse
};
using (var schemaReader = XmlReader.Create(fs, settings)) {
trans.Load(schemaReader);
using (var ms = new MemoryStream()) {
using (var sreader = new StringReader(source)) {
using (var reader = XmlReader.Create(sreader)) {
trans.Transform(reader, null, ms);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
}
}
}
}
For the sample OpenMath above, it produces following MathML output:
<math xmlns="http://www.w3.org/1998/Math/MathML">
<apply>
<eq />
<ci>x</ci>
<apply>
<divide />
<apply>
<csymbol definitionURL="http://www.openmath.org/cd/multiops#plusminus" />
<apply>
<minus />
<ci>b</ci>
</apply>
<apply>
<root />
<degree />
<apply>
<minus />
<apply>
<power />
<ci>b</ci>
<cn type="integer">2</cn>
</apply>
<apply>
<times />
<cn type="integer">4</cn>
<ci>a</ci>
<ci>c</ci>
</apply>
</apply>
</apply>
</apply>
<apply>
<times />
<cn type="integer">2</cn>
<ci>a</ci>
</apply>
</apply>
</apply>
</math>
As a final note: note that MathML has two versions - Presentation and Content. omtocmml.xsl stylesheet will convert to Content version. However, if you need Presentation version, the same website above has stylesheets to convert Content to Presentation, so you can convert OpenMath to Presentation MathML by applying two transformations.