Question : XPath newbie

Please look at the code below and advise me how I can bring back in the XPath not just title but author and price ? (Thanks)

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>

  <script type="text/javascript">

    var xmlDoc;

    function fnOnLoad() {
      if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
      }
      else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.open("GET", "books.xml", false);
      xmlhttp.send();
      xmlDoc = xmlhttp.responseXML;
    }

    function SelectTitleNode_price_gt_35() {

      var buf = "";

      path = "/bookstore/book[price>35]/title";

      // code for IE

      if (window.ActiveXObject) {
        xmlDoc.setProperty("SelectionLanguage", "XPath");
        var nodes = xmlDoc.selectNodes(path);
        for (i = 0; i < nodes.length; i++) {
          buf += "(IE) " + nodes[i].childNodes[0].nodeValue ;
          buf += "<br/>";
        }
      } else {

        // code for Mozilla, Firefox, Opera, etc.

        if (document.implementation && document.implementation.createDocument) {
          var nodes = xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ANY_TYPE, null);
          var result = nodes.iterateNext();
          while (result) {
            buf += "(Firefox) " + result.childNodes[0].nodeValue + "<br/>";
            result = nodes.iterateNext();
          }
        }
      }

      document.getElementById("sec00").innerHTML = buf;
    }
  </script>

</head>
<body onload="fnOnLoad()">
  <input type="button" value="Select TITLE nodes with price>35" onclick="SelectTitleNode_price_gt_35()" />&nbsp;
  <div id="sec00"></div>
</body>
</html>
Attachments:
 
Books XML
 

Answer : XPath newbie

check this out
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
path = "/bookstore/book[price>35]";

      // code for IE

      if (window.ActiveXObject) {
        xmlDoc.setProperty("SelectionLanguage", "XPath");
        var book_nodes = xmlDoc.selectNodes(path);
        for (i = 0; i < book_nodes.length; i++) {
          var book_fields = book_nodes[i].childNodes;
          for (j = 0; j < book_fields.length; j++ ) {
              if( book_fields[j].nodeName == "author" )
                buf += "(IE) author: " + book_fields[j].text + "<br/>";
              if( book_fields[j].nodeName == "price" )
                buf += "(IE) price: " + book_fields[j].text + "<br/>";
          }
        }
      } else {

        // code for Mozilla, Firefox, Opera, etc.

        if (document.implementation && document.implementation.createDocument) {
          var nodes = xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ANY_TYPE, null);
          var result = nodes.iterateNext();
          while (result) {
              var book_fields = result.childNodes;
              for (j = 0; j < book_fields.length; j++ ) {
                  if( book_fields[j].nodeName == "author" )
                    buf += "(Firefox) author: " + book_fields[j].childNodes[0].nodeValue + "<br/>";
                  if( book_fields[j].nodeName == "price" )
                    buf += "(Firefox) price: " + book_fields[j].childNodes[0].nodeValue + "<br/>";
              }
            result = nodes.iterateNext();
          }
        }
      }
Random Solutions  
 
programming4us programming4us