Search code examples
javascriptjqueryjquery-selectorsxmldom

How to use code for MATH MML in Jquery in XML Parser


Using JQuery XML Parser everything works well. I have Codes for Equation , while i try to find the code its return none. But code is actually in XML. I dont know the exact reason why it doesnt find. May be the xml node contain ":" that is the reason or not. Can you help me to find the code mml:math

XML Code

<p>
  <disp-formula id="disp-formula3-xxxx">
    <label>(3)</label>
    <alternatives>
      <mml:math id="math15-xxxx" display="block">
        <mml:mrow>
          <mml:mi mathvariant="normal">Risk</mml:mi>
          <mml:mo>=</mml:mo>
          <mml:mstyle displaystyle="true">
            <mml:mrow>
              <mml:mo>&#x0222B;</mml:mo>
            </mml:mrow>
          </mml:mstyle>
          <mml:mi mathvariant="double-struck">P</mml:mi>
          <mml:mspace width="0.25em"/>
          <mml:mspace width="0.25em"/>
          <mml:mo stretchy="false">[</mml:mo>
          <mml:mi mathvariant="normal">Load Events</mml:mi>
          <mml:mo stretchy="false">]</mml:mo>
          <mml:mo>&#x000D7;</mml:mo>
          <mml:mi mathvariant="double-struck">P</mml:mi>
          <mml:mspace width="0.25em"/>
          <mml:mo stretchy="false">[</mml:mo>
          <mml:mi mathvariant="normal">Responses</mml:mi>
          <mml:mo>|</mml:mo>
          <mml:mi mathvariant="normal">Loads</mml:mi>
          <mml:mo stretchy="false">]</mml:mo>
          <mml:mo>&#x000D7;</mml:mo>
          <mml:mi mathvariant="double-struck">C</mml:mi>
          <mml:mspace width="0.25em"/>
          <mml:mo stretchy="false">[</mml:mo>
          <mml:mi mathvariant="normal">Loads</mml:mi>
          <mml:mo>,</mml:mo>
          <mml:mi mathvariant="normal">Responses</mml:mi>
          <mml:mo stretchy="false">]</mml:mo>
        </mml:mrow>
      </mml:math>
      <graphic xlink:href="10.1177_1687814018802531-eq3.tif"/>
    </alternatives>
  </disp-formula>
</p>

In JQUERY

alert($xml.find("mml:math").length)

it returns zero. i have to check lot of coding based on Math MML code. Can you help me to understand whats the problem here


Solution

  • To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\. docs

    So you need to escape : in your selector and use

    $xml.find("mml\\:math").length
    

    var $xml = $.parseXML($("#xml").html().trim());
    var leng = $($xml).find('mml\\:math').length;
    console.log(leng);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="xml">
      <p>
        <disp-formula id="disp-formula3-xxxx">
          <label>(3)</label>
          <alternatives>
            <mml:math id="math15-xxxx" display="block">
              <mml:mrow>
                <mml:mi mathvariant="normal">Risk</mml:mi>
                <mml:mo>=</mml:mo>
                <mml:mstyle displaystyle="true">
                  <mml:mrow>
                    <mml:mo>&#x0222B;</mml:mo>
                  </mml:mrow>
                </mml:mstyle>
                <mml:mi mathvariant="double-struck">P</mml:mi>
                <mml:mspace width="0.25em"/>
                <mml:mspace width="0.25em"/>
                <mml:mo stretchy="false">[</mml:mo>
                <mml:mi mathvariant="normal">Load Events</mml:mi>
                <mml:mo stretchy="false">]</mml:mo>
                <mml:mo>&#x000D7;</mml:mo>
                <mml:mi mathvariant="double-struck">P</mml:mi>
                <mml:mspace width="0.25em"/>
                <mml:mo stretchy="false">[</mml:mo>
                <mml:mi mathvariant="normal">Responses</mml:mi>
                <mml:mo>|</mml:mo>
                <mml:mi mathvariant="normal">Loads</mml:mi>
                <mml:mo stretchy="false">]</mml:mo>
                <mml:mo>&#x000D7;</mml:mo>
                <mml:mi mathvariant="double-struck">C</mml:mi>
                <mml:mspace width="0.25em"/>
                <mml:mo stretchy="false">[</mml:mo>
                <mml:mi mathvariant="normal">Loads</mml:mi>
                <mml:mo>,</mml:mo>
                <mml:mi mathvariant="normal">Responses</mml:mi>
                <mml:mo stretchy="false">]</mml:mo>
              </mml:mrow>
            </mml:math>
            <graphic xlink:href="10.1177_1687814018802531-eq3.tif"/>
          </alternatives>
        </disp-formula>
      </p>
    </div>