DOMParser()。parseFromString()意外地从XML中删除节点

时间:2014-11-10 14:11:49

标签: javascript xml google-chrome firefox domparser

鉴于缺少xmlns:xlink="http://www.w3.org/1999/xlink"的XML文档:

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book title="Harry Potter">
  <description
  xlink:type="simple"
  xlink:href="/images/HPotter.gif"
  xlink:show="new">
  As his fifth year at Hogwarts School of Witchcraft and
  Wizardry approaches, 15-year-old Harry Potter is.......
  </description>
</book>

<book title="XQuery Kick Start">
  <description
  xlink:type="simple"
  xlink:href="/images/XQuery.gif"
  xlink:show="new">
  XQuery Kick Start delivers a concise introduction
  to the XQuery standard.......
  </description>
</book>
<text>Why Am I Removed?</text>
</bookstore>

以下javascript片段:

var xml = '<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book title="Harry Potter"> <description xlink:type="simple" xlink:href="/images/HPotter.gif" xlink:show="new"> As his fifth year at Hogwarts School of Witchcraft and Wizardry approaches, 15-year-old Harry Potter is....... </description> </book> <book title="XQuery Kick Start"> <description xlink:type="simple" xlink:href="/images/XQuery.gif" xlink:show="new"> XQuery Kick Start delivers a concise introduction to the XQuery standard....... </description> </book><text>Why Am I Removed?</text></bookstore>'
var dom = new DOMParser().parseFromString(xml, 'text/xml'); 
xml = new XMLSerializer().serializeToString(dom);

Firefox(v33.1)给了我以下错误,我期待:

prefix not bound to a namespace

谷歌浏览器(v38.0.2125.111)删除了未解析的xmlns:xlink节点以及所有节点(没有任何错误):

<?xml version="1.0" encoding="UTF-8"?><bookstore> <book title="Harry Potter"> </book></bookstore>

有没有办法在Google Chrome中获得与Firefox相同的行为?我期待解析错误...

0 个答案:

没有答案