如何使用XPath访问FireFox中的xml属性?

时间:2019-05-25 09:07:10

标签: javascript xml firefox xpath

我找不到使用FireFox中的XPath从xml文件访问所需属性值的正确JavaScript方法。这是示例xml:

<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

我已经查看了先前问题(Getting attribute using XPath)的其他答案,但似乎无法使它们适应我的代码。

这就是我正在使用的内容(摘自w3schools网站):

<html>
<body>

<p id="demo"></p>
<p id="demo2"></p>

<script>
// from here: https://www.w3schools.com/xml/tryit.asp?filename=try_xpath_select_cdnodes
// new xmlhttprequest object
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        //call showResult function, pass response to request to it (i.e. the page we want to scrape)
        showResult(xhttp.responseXML);
    }
};
// initialise request with method and URL
xhttp.open("GET", "book.html", true);
// run request
xhttp.send(); 

// function to get xpath result, interate over it and write to paragraph tag with demo id
function showResult(xml) {
    var txt = "";
    //this is the xpath bit, the code to grab the tag values we want
    path = "/bookstore/book/title"
    if (xml.evaluate) {
        var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
        while (result) {
            txt += result.childNodes[0].nodeValue + "<br>";
            result = nodes.iterateNext();
        } 
    // Code For Internet Explorer
    } else if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
        xml.setProperty("SelectionLanguage", "XPath");
        nodes = xml.selectNodes(path);
        for (i = 0; i < nodes.length; i++) {
            txt += nodes[i].childNodes[0].nodeValue + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt;
}

</body>
</html>

这将返回《 Everyday Italian》,哈利·波特。我想适应返回en,en。

了解当前代码是否应该正常工作,我只是不知道在哪里放置类似getAttributes方法的内容。我真的不太明白为什么firefox循环也使用childNodes方法。谢谢。

1 个答案:

答案 0 :(得分:1)

对于XPath,如果要选择那些元素的所有属性,请使用路径/bookstore/book/title/@*,如果要选择那些lang的属性,请使用/bookstore/book/title/@lang

在具有evaluate方法(https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate)和iterateNext方法(https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/iterateNext)的XPath API中,您只需使用

    var xpathResult = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
    var node = null;
    while ((node = xpathResult.iterateNext()) != null) {
        txt += node.nodeValue + "<br>";
    } 

var xmlCode = `<bookstore>
<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>`;


var xml = new DOMParser().parseFromString(xmlCode, 'application/xml');

var path = '/bookstore/book/title/@lang';

var xpathResult = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
    
var node = null;

var values = [];

while ((node = xpathResult.iterateNext()) != null) {
  values.push(node.nodeValue);
}

document.getElementById('demo').innerHTML = values.join('<br>');
<p id="demo"></p>