?
This document uses PHP Chinese website manual Release
在本節(jié),讓我們通過實例來學習一些基礎的 XPath 語法。
我們將在下面的例子中使用這個 XML 文檔:
"books.xml":
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
所有現(xiàn)代瀏覽器都支持使用 XMLHttpRequest 來加載 XML 文檔的方法。
針對大多數(shù)現(xiàn)代瀏覽器的代碼:
var xmlhttp=new XMLHttpRequest()
針對古老的微軟瀏覽器(IE 5 和 6)的代碼:
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
不幸的是,Internet Explorer 和其他處理 XPath 的方式不同。
在我們的例子中,包含適用于大多數(shù)主流瀏覽器的代碼。
Internet Explorer 使用 selectNodes() 方法從 XML 文檔中的選取節(jié)點:
xmlDoc.selectNodes(xpath);
Firefox、Chrome、Opera 以及 Safari 使用 evaluate() 方法從 XML 文檔中選取節(jié)點:
xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null);
下面的例子選取所有 title 節(jié)點:
<html><!DOCTYPE html> <html> <body> <script> 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("books.xml"); path="/bookstore/book/title" // code for IE if (window.ActiveXObject) { var nodes=xml.selectNodes(path); 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>
下面的例子選取 bookstore 元素下面的第一個 book 節(jié)點的 title:
<html><!DOCTYPE html> <html> <body> <script> 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("books.xml"); path="/bookstore/book[1]/title"; // code for IE if (window.ActiveXObject) { var nodes=xml.selectNodes(path); 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>
這里有一個問題。上面的例子在 IE 和其他瀏覽器中輸出不同的結(jié)果。
IE5 以及更高版本將 [0] 視為第一個節(jié)點,而根據(jù) W3C 的標準,應該是 [1]。
為了解決 IE5+ 中 [0] 和 [1] 的問題,可以為 XPath 設置語言選擇(SelectionLanguage)。
下面的例子選取 bookstore 元素下面的第一個 book 節(jié)點的 title:
<html><!DOCTYPE html>
<html>
<body>
<script>
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("books.xml");
path="/bookstore/book[1]/title";
// code for IE
if (window.ActiveXObject)
{
xml.setProperty("SelectionLanguage","XPath");
var nodes=xml.selectNodes(path);
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>
下面的例子選取 price 節(jié)點中的所有文本:
<html><!DOCTYPE html> <html> <body> <script> 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("books.xml"); path="/bookstore/book/price/text()" // code for IE if (window.ActiveXObject) { var nodes=xml.selectNodes(path); for (i=0;i<nodes.length;i++) { document.write(nodes[i].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.nodeValue + "<br>"); result=nodes.iterateNext(); } } </script> </body> </html>
下面的例子選取價格高于 35 的所有 price 節(jié)點:
<html><!DOCTYPE html> <html> <body> <script> 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("books.xml"); path="/bookstore/book[price>35]/price"; // code for IE if (window.ActiveXObject) { var nodes=xml.selectNodes(path); 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>
下面的例子選取價格高于 35 的所有 title 節(jié)點:
<html><!DOCTYPE html> <html> <body> <script> 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("books.xml"); path="/bookstore/book[price>35]/title"; // code for IE if (window.ActiveXObject) { var nodes=xml.selectNodes(path); 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>