user1989
user1989

Reputation: 163

parsing xml to get the equation to be returned to python

Here is a javascript file which turns xml file to text.This text is often equation.I want this equation to be in such a way that the result of xml passed to python produce required result.any help is appreciable.

function getDOM(xmlstring) {
    parser=new DOMParser();
    return parser.parseFromString(xmlstring, "text/xml");
}
function remove_tags(node) {
    var result = "";
    var nodes = node.childNodes;
    var tagName = node.tagName;
    if (!nodes.length) {
        if (node.nodeValue == "π") result = "pi";
        else if (node.nodeValue == " ") result = "";
        else result = node.nodeValue;
    } else if (tagName == "mfrac") {
        result = "("+remove_tags(nodes[0])+")/("+remove_tags(nodes[1])+")";
    } else if (tagName == "msup") {
        result = "Math.pow(("+remove_tags(nodes[0])+"),("+remove_tags(nodes[1])+"))";
    } else for (var i = 0; i < nodes.length; ++i) {
        result += remove_tags(nodes[i]);
    }
    if (tagName == "mfenced") result = "("+result+")";
    if (tagName == "msqrt") result = "Math.sqrt("+result+")";
    return result;
}
function stringifyMathML(mml) {
   xmlDoc = getDOM(mml);
   return remove_tags(xmlDoc.documentElement);
}

Example of xml file is

s = stringifyMathML(" <math><mi>sin</mi><mfenced><mi>x</mi></mfenced></math>");
        alert(s);
        alert(eval(s));

I am expecting output to be math.sin(x)

Upvotes: 1

Views: 398

Answers (1)

Baart
Baart

Reputation: 582

Adding the specific .math part will solve the issue:

The "math." part must be added only when special keyword are present. So first, build the potential operation you will need to cover in a list (mList)

Then, if you meet this operation, prepend it with ".math"

var mList = ['pow', 'sin', 'cos', 'pow', 'sqrt', 'π'];

function getDOM(xmlstring) {
    parser=new DOMParser();
    return parser.parseFromString(xmlstring, "text/xml");
}
function remove_tags(node) {
    var result = "";
    var nodes = node.childNodes;
    var tagName = node.tagName;
    if (!nodes.length) {
        if(mList.indexOf(node.nodeValue) != -1 ) {
            result += 'math.'
        }
        if (node.nodeValue == "π") result += "pi";
        else if (node.nodeValue == " ") result += "";
        else result += node.nodeValue;
    } else if (tagName == "mfrac") {
        result += "("+remove_tags(nodes[0])+")/("+remove_tags(nodes[1])+")";
    } else if (tagName == "msup") {
        result += "pow(("+remove_tags(nodes[0])+"),("+remove_tags(nodes[1])+"))";
    } else for (var i = 0; i < nodes.length; ++i) {
        result += remove_tags(nodes[i]);
    }
    if (tagName == "mfenced") result = "("+result+")";
    if (tagName == "msqrt") result = "sqrt("+result+")";

    console.log('returning', result)
    return result;
}
function stringifyMathML(mml) {
   xmlDoc = getDOM(mml);
   return remove_tags(xmlDoc.documentElement);
}

a = stringifyMathML("<math><mi>x</mi></math>");
b = stringifyMathML("<math><mi>x</mi><mo>+</mo><mn>5</mn></math> ");
c = stringifyMathML("<math><mi>sin</mi><mfenced><mi>x</mi></mfenced></math> ");


console.log(a, 'vs x');
console.log(b, 'vs x+5');
console.log(c, 'vs math.sin(x)');

Output

x vs x

x+5 vs x+5

math.sin(x) vs math.sin(x)

Upvotes: 1

Related Questions