XPath:在不同的DIV中显示信息

时间:2016-12-18 16:46:48

标签: javascript html xml xpath

我是一个小菜鸟,我需要帮助......我有这个XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <book category="WEB">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="WEB">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

这个HTML:

<!DOCTYPE html>
<html> 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <head>
  </head>
  <body>
    <p <div style="color:#0770FF"  id="demo"  ></p>
    <script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        showResult(xhttp.responseXML);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send(); 

function showResult(xml) {
    var txt = "";
	  path = "/bookstore/book //title | //author | //price  | //price | //year"
	
    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();
        } 
   
    } 
    
    document.getElementById("demo").innerHTML = txt;
}
    </script>
  </body>
</html>

我的问题是:如何在不同的列/ DIV中显示信息?例如:

  • 第1列标题
  • column-2 author
  • 第3列......等等。

我很抱歉,如果我的问题可能有点愚蠢,但我找不到解决方案。非常感谢你。

0 个答案:

没有答案