在Firefox浏览器下,执行如下代码可显示SVG图像和MathML公式。问题如下:
1、将//echo $xml->asXML() 的注释去掉,出现"XML解析错误:xml处理指令不在实体开始部分 行:115,列12";
2、将//echo $xml->asXML() 的注释去掉,在header('Content-type: application/xhtml+xml')前加注释,脚本可以执行,但svg图形不显示。
相关问题有无遇到,如何解决?
代码如下:
------------------------------------------------------------------------------------
<?php
header('Content-type: application/xhtml+xml');
echo '<?xml version="1.0" encoding="utf-8"?>';
$headerStr='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd"> 
 <html xmlns="http://www.w3.org/1999/xhtml">';
echo $headerStr;
?>
<head>
<title>MathML SVG Example</title>
</head>
<body>
<?php
$XMLStr=<<<SimpleXMLStr
<!-- SVG codes starts here -->
<svg:svg xmlns:svg="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="100%" height="100%">
<svg:circle cx="95px" cy="80px" r="70px" fill="red"/>
<svg:rect x="45" y="65" width="100" height="30" fill="rgb(255, 255, 255)"/>
<svg:circle cx="300px" cy="80px" r="67px" fill="#ffffff" stroke="red" stroke-width="5px"/>
<svg:rect x="250" y="65" width="100" height="30" fill="red"/>
<svg:rect x="285" y="30" width="30" height="100" fill="red"/>
</svg:svg>
<!-- SVG codes ends here -->
SimpleXMLStr;$xml = simplexml_load_string ($XMLStr);
echo $XMLStr;
//echo $xml->asXML() ;
?>
<?php
$XMLStr=<<<SimpleXMLStr
<object_1>
<subject_1>
<text>如果</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b1;</mml:mi>
  </mml:mrow>
</mml:math>
<text>与</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b2;</mml:mi>
  </mml:mrow>
</mml:math>
<text>互补,且</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b1;</mml:mi>
    <mml:mo>&#x3e;</mml:mo>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b2;</mml:mi>
  </mml:mrow>
</mml:math>
<text>则下列表示</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b2;</mml:mi>
  </mml:mrow>
</mml:math>
<text>的余角的式子中:①、</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mn>90</mml:mn>
    <mml:mi>&#xb0;</mml:mi>
    <mml:mo>&#x2212;</mml:mo>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b2;</mml:mi>
  </mml:mrow>
</mml:math>
<text>,②、</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b1;</mml:mi>
    <mml:mo>&#x2212;</mml:mo>
    <mml:mn>90</mml:mn>
    <mml:mi>&#xb0;</mml:mi>
  </mml:mrow>
</mml:math>
<text>,③、</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mfrac>
      <mml:mn>1</mml:mn>
      <mml:mn>2</mml:mn>
    </mml:mfrac>
    <mml:mo stretchy="false">(</mml:mo>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b1;</mml:mi>
    <mml:mo>+</mml:mo>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b2;</mml:mi>
    <mml:mo stretchy="false">)</mml:mo>
  </mml:mrow>
</mml:math>
<text>,④、</text>
<mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML">
  <mml:mrow>
    <mml:mfrac>
      <mml:mn>1</mml:mn>
      <mml:mn>2</mml:mn>
    </mml:mfrac>
    <mml:mo stretchy="false">(</mml:mo>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b1;</mml:mi>
    <mml:mo>&#x2212;</mml:mo>
    <mml:mo lspace="4px" rspace="4px">&#x2220;</mml:mo>
    <mml:mi>&#x3b2;</mml:mi>
    <mml:mo stretchy="false">)</mml:mo>
  </mml:mrow>
</mml:math>
<text>,正确的有:</text>
</subject_1>
<question>
<SimpleChoice>
<Choice><text>(A)4个</text>
</Choice>
<Choice><text>(B)3个</text>
</Choice>
<Choice>
<text>(C)2个</text>
</Choice>
<Choice><text>(D)1个</text>
</Choice>
</SimpleChoice>
</question>
</object_1>
SimpleXMLStr;$xml = simplexml_load_string ($XMLStr);
echo $XMLStr;
//echo $xml->asXML() ;
?></body>
</html>

解决方案 »

  1.   

    < < <SimpleXMLStr 
    =>
    <<<SimpleXMLStr 然后还有无非是有一些特殊字符在某行结尾处.
      

  2.   

    jakey9826:您好!
    代码“< < <SimpleXMLStr”有何错误,请进一步明示。
      

  3.   

    < < <SimpleXMLStr
    =>
    <<<SimpleXMLStr应该全角半角
      

  4.   

    <<<
    真郁闷,这csdn咋回事呀.
      

  5.   

    jakey9826:
    <<<在文件中为西文字符,无问题。不然,echo $XMLStr;也无法执行。
      

  6.   

    jakey9826: 
    可否告知非法代码是啥呀,且如下两条无误?
    1、将//echo $xml->asXML() 的注释去掉,出现"XML解析错误:xml处理指令不在实体开始部分 行:115,列12";
    2、将//echo $xml->asXML() 的注释去掉,在header('Content-type: application/xhtml+xml')前加注释,脚本可以执行,但svg图形不显示。 
      

  7.   

    再问jakey9826: 如下代码在firefox下不执行原因,去掉header('Content-type:application/xhtml+xml');程序可执行.
    <?php
    header('Content-type:application/xhtml+xml');
    echo '<html xmlns="http://www.w3.org/1999/xhtml">';
    ?>
    <?php
    ?>
    <head>
    <title>MathML SVG Example</title>
    </head>
    <body>
    <?php
    $XMLStr='<object_1><subject_1><text>如果</text></subject_1></object_1>';
    $xml = simplexml_load_string ($XMLStr);
    echo $XMLStr;
    echo $xml->asXML() ;
    ?>
    </body>
    </html>
      

  8.   

    sorry!
    以前不太了解svg,所以以上回答可能有些问题.1 我所说的非法字符,指的就是?>后面的出现的任何字符, <<<SimpleXMLStr后面出现的任何字符
      可能我是直接复制你的代码,所以有偏差
    2 对于svg,我的理解是这样的.
     2-1 ie不能很好的支持svg, 需要安装一些插件才能显示图形,比如adodb
     ff下支持下比ie要好. 
     2-2 目前svg可以将svg保存成扩展名.svg的文件,并且用embed或者object标签引入.
     2-3 ff下html和svg可以混在一起,但需要严格符合xml规范,所以你的测试实例出现问题很正常.分析一下你的测试页面的问题, $xml->asXML();输出xml,包括<?xml version="1.0">标签,所以你一段html里包含了N多标任签,这就是你不能正确输出图形的主要原因.
    稍后我发出正确的代码!
     
      

  9.   

    用simplexml处理是画蛇添足了.
    <?php
    header('Content-type: application/xhtml+xml'); 
    echo <<<TEST
    <?xml version="1.0" standalone="no" ?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head> 
    <title>MathML SVG Example </title> 
    </head> 
    <body> 
    TEST;//输出svg图像
    echo <<<svgXML
    <svg:svg xmlns:svg="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="100%" height="100%"> 
    <svg:circle cx="95px" cy="80px" r="70px" fill="red"/> 
    <svg:rect x="45" y="65" width="100" height="30" fill="rgb(255, 255, 255)"/> 
    <svg:circle cx="300px" cy="80px" r="67px" fill="#ffffff" stroke="red" stroke-width="5px"/> 
    <svg:rect x="250" y="65" width="100" height="30" fill="red"/> 
    <svg:rect x="285" y="30" width="30" height="100" fill="red"/> 
    </svg:svg>
    svgXML;//输出math
    $mathML =  <<<mathML
    <object_1> 
    <subject_1> 
    <text>如果 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b1; </mml:mi> 
      </mml:mrow> 
    </mml:math> 
    <text>与 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b2; </mml:mi> 
      </mml:mrow> 
    </mml:math> 
    <text>互补,且 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b1; </mml:mi> 
        <mml:mo>&#x3e; </mml:mo> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b2; </mml:mi> 
      </mml:mrow> 
    </mml:math> 
    <text>则下列表示 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b2; </mml:mi> 
      </mml:mrow> 
    </mml:math> 
    <text>的余角的式子中:①、 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mn>90 </mml:mn> 
        <mml:mi>&#xb0; </mml:mi> 
        <mml:mo>&#x2212; </mml:mo> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b2; </mml:mi> 
      </mml:mrow> 
    </mml:math> 
    <text>,②、 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b1; </mml:mi> 
        <mml:mo>&#x2212; </mml:mo> 
        <mml:mn>90 </mml:mn> 
        <mml:mi>&#xb0; </mml:mi> 
      </mml:mrow> 
    </mml:math> 
    <text>,③、 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mfrac> 
          <mml:mn>1 </mml:mn> 
          <mml:mn>2 </mml:mn> 
        </mml:mfrac> 
        <mml:mo stretchy="false">( </mml:mo> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b1; </mml:mi> 
        <mml:mo>+ </mml:mo> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b2; </mml:mi> 
        <mml:mo stretchy="false">) </mml:mo> 
      </mml:mrow> 
    </mml:math> 
    <text>,④、 </text> 
    <mml:math xmlns:mml="http://www.w3.org/1998/Math/MathML"> 
      <mml:mrow> 
        <mml:mfrac> 
          <mml:mn>1 </mml:mn> 
          <mml:mn>2 </mml:mn> 
        </mml:mfrac> 
        <mml:mo stretchy="false">( </mml:mo> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b1; </mml:mi> 
        <mml:mo>&#x2212; </mml:mo> 
        <mml:mo lspace="4px" rspace="4px">&#x2220; </mml:mo> 
        <mml:mi>&#x3b2; </mml:mi> 
        <mml:mo stretchy="false">) </mml:mo> 
      </mml:mrow> 
    </mml:math> 
    <text>,正确的有: </text> 
    </subject_1> 
    <question> 
    <SimpleChoice> 
    <Choice> <text>(A)4个 </text> 
    </Choice> 
    <Choice> <text>(B)3个 </text> 
    </Choice> 
    <Choice> 
    <text>(C)2个 </text> 
    </Choice> 
    <Choice> <text>(D)1个 </text> 
    </Choice> 
    </SimpleChoice> 
    </question> 
    </object_1>
    mathML;
    echo iconv('gb2312', 'utf-8', $mathML);
    echo "
    </body>
    </html>";
    ?>
      

  10.   

    jakey9826: 非常感谢!
        感谢细致的解答,专业的测试。
        我使用simpleXML的主要原因,从数据库查询记录,通过simpleXML寻找、分离不同的不同项目,动态生成form表单,
    ,由于输出内容依赖数据库,不可能象您说提供的方法生成网页,故采用simpleXML。
        您判断准确,确实simpleXML对象输出产生问题($xml->asXML())。
        可否有更好的解决方案,期待您的建议。谢谢!
      

  11.   

    不用客气,主要是对你提出的问题很感兴趣,呵呵.毕竟是新鲜东西.如果非要用simplexml,也很简单.
    echo str_replace('<?xml version="1.0"?>', '', $xml->asXML()); 
    将这个xml头去掉就可以了.