【问题标题】:xPath XML file with namespaces using Javascript使用 Javascript 的带有命名空间的 xPath XML 文件
【发布时间】:2011-11-07 14:19:17
【问题描述】:

我已经进行了大量研究,但仍然遇到了死胡同:(

这是我的 XML 文件 (test.xml):

<bookstore>
  <book genre="autobiography">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <bk:book genre="novel" bk:genre="fiction" 
xmlns:bk="http://purl.org/dc/elements/1.1/">
    <bk:title>The Confidence Man</bk:title>
    <bk:author>
      <bk:first-name>Herman</bk:first-name>
      <bk:last-name>Melville</bk:last-name>
    </bk:author>
    <bk:price>11.99</bk:price>
  </bk:book>
</bookstore>

这是我的 Javascript 文件:

<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}


xml=loadXMLDoc("test.xml");
//xml.remove_namespaces;
path="/bookstore/bk:book/bk:title";
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);
//var nodes=xmlDoc.getElementsByTagName('bk:title');

for (i=0;i<nodes.length;i++)
  {
  document.write(nodes[i].childNodes[0].nodeValue);
  document.write("<br />");
  }
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();

while (result)
  {
  document.write(result.childNodes[0].nodeValue);
  document.write("<br />");
  result=nodes.iterateNext();
  }
}
</script>

</body>
</html>

我无法获取 bk 命名空间标签内的值 :(

我已经尝试了所有这些 //*[name() 等等等等垃圾,不行:(

任何帮助将不胜感激!!!!!!!!!!!!!!!!!!!

亲切的问候, 山姆·冈戈麦斯

【问题讨论】:

    标签: javascript html xml xpath namespaces


    【解决方案1】:

    这里有一些示例代码来展示如何处理命名空间:

    var path="/bookstore/bk:book/bk:title";
    
    if (typeof xml.evaluate !== 'undefined') {
      var result = xml.evaluate(
       path,
       xml,
       function (prefix) {
         if (prefix === 'bk') {
           return 'http://purl.org/dc/elements/1.1/';
         }
         else {
           return null;
         }
       },
       XPathResult.ANY_TYPE,
       null
      );
      // now use the code here you already have in your sample for evaluate 
    }
    else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined') {
      xml.setProperty('SelectionLanguage', 'XPath');
      xml.setProperty('SelectionNamespaces', 'xmlns:bk="http://purl.org/dc/elements/1.1/"');
      var nodes = xml.selectNodes(path);
      // now use the code you already have for selectNodes
    }
    

    【讨论】:

      【解决方案2】:

      啊,我的代码在 IE 中运行,但不能在 Firefox 之类的浏览器中运行(有趣的是,该代码仅在 IE 由服务器(apache 等)托管时才在 IE 中执行)。

      我现在修复了所有问题,它可以在所有浏览器上完美运行。马丁真是太感谢你了。

      我没有正确解析函数构造函数变量。

      这是功能齐全的代码:

      <html>
      <body>
      <script type="text/javascript">
      
      function loadXMLDoc(dname)
      {
      if (window.XMLHttpRequest)
        {
        xhttp=new XMLHttpRequest();
        }
      else
        {
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      xhttp.open("GET",dname,false);
      xhttp.send("");
      return xhttp.responseXML;
      }
      
      xml=loadXMLDoc("test.xml");
      var path="/bookstore/bk:book/bk:title";
      
      if (typeof xml.evaluate !== 'undefined') 
      {
        var result = xml.evaluate(
         path,
         xml,
         function (prefix) {
           if (prefix === 'bk') {
             return 'http://purl.org/dc/elements/1.1/';
           }
           else {
             return null;
           }
         },
         XPathResult.ANY_TYPE,
         null
        );
        // now use the code here you already have in your sample for evaluate
        var nodes=xml.evaluate(
         path,
         xml,
         function (prefix) {
           if (prefix === 'bk') {
             return 'http://purl.org/dc/elements/1.1/';
           }
           else {
             return null;
           }
         },
         XPathResult.ANY_TYPE,
         null);
      var result=nodes.iterateNext();
      
      while (result)
        {
        document.write(result.childNodes[0].nodeValue);
        document.write("<br />");
        result=nodes.iterateNext();
        } 
      }
      else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined') 
      {
        xml.setProperty('SelectionLanguage', 'XPath');
        xml.setProperty('SelectionNamespaces', 'xmlns:bk="http://purl.org/dc/elements/1.1/"');
        var nodes = xml.selectNodes(path);
        // now use the code you already have for selectNodes
      var nodes=xml.selectNodes(path);
      //var nodes=xmlDoc.getElementsByTagName('bk:title');
      
      for (i=0;i<nodes.length;i++)
        {
        document.write(nodes[i].childNodes[0].nodeValue);
        document.write("<br />");
        }
      
      
      }
      
      </script>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-05
        • 1970-01-01
        • 1970-01-01
        • 2020-10-27
        • 2012-06-09
        • 2020-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多