简体   繁体   中英

How can I access xml attributes in FireFox using XPath?

I can't find the right JavaScript method to access the attribute values I want from an xml file, using XPath in FireFox. Here's the sample 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>

I have viewed the other answers to previous questions ( Getting attribute using XPath ), but I can't seem to adapt them to work with my code.

Here's what I'm using (taken from the w3schools site):

<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>

This returns Everyday Italian, Harry Potter. I'd like to adapt to return en, en.

Appreciate the current code is working as it should, I just can't figure out where to put something like getAttributes method. I don't really understand why the firefox loop is using the childNodes method either. Thanks.

In terms of XPath, if you want to select all attributes of those elements use the path /bookstore/book/title/@* , if you want to select those lang attributes use /bookstore/book/title/@lang .

In the XPath API with evaluate method ( https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate ) and the iterateNext method ( https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/iterateNext ) you can then simply use

    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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM