国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

XPath教程- 實例練習

2019-11-08 03:13:26
字體:
供稿:網(wǎng)友

xml實例文檔

我們將在下面的例子中使用這個 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>

加載 XML 文檔 所有現(xiàn)代瀏覽器都支持使用 xmlhttpRequest 來加載 XML 文檔的方法。 針對大多數(shù)現(xiàn)代瀏覽器的代碼:

var xmlhttp=new XMLHttpRequest()

針對古老的微軟瀏覽器(IE 5 和 6)的代碼: var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

選取節(jié)點 不幸的是,Internet Explorer 和其他處理 XPath 的方式不同。 在我們的例子中,包含適用于大多數(shù)主流瀏覽器的代碼。 Internet Explorer 使用 selectNodes() 方法從 XML 文檔中的選取節(jié)點:

xmlDoc.selectNodes(xpath);

FirefoxChromeOpera 以及 Safari 使用 evaluate() 方法從 XML 文檔中選取節(jié)點:

xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null);

選取所有 title 下面的例子選取所有 title 節(jié)點:

/bookstore/book/title

代碼:

<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book/title"http:// code for IEif (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>

結(jié)果:

Harry PotterEveryday ItalianLearning XMLXQuery Kick Start

選取第一個 book 的 title 下面的例子選取 bookstore 元素下面的第一個 book 節(jié)點的 title:

/bookstore/book[1]/title

代碼:

<html><body><script type="text/Javascript">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("/example/xmle/books.xml");path="/bookstore/book[1]/title";// code for IEif (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>

結(jié)果

Harry Potter

這里有一個問題。上面的例子在 IE 和其他瀏覽器中輸出不同的結(jié)果。

IE5 以及更高版本將 [0] 視為第一個節(jié)點,而根據(jù) W3C 的標準,應(yīng)該是 [1]。

為了解決 IE5+ 中 [0] 和 [1] 的問題,可以為 XPath 設(shè)置語言選擇(SelectionLanguage)。

下面的例子選取 bookstore 元素下面的第一個 book 節(jié)點的 title:

xml.setProperty("SelectionLanguage","XPath");xml.selectNodes("/bookstore/book[1]/title");

代碼

<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book[1]/title";// code for IEif (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>

結(jié)果

Harry Potter

選取所有價格 下面的例子選取 price 節(jié)點中的所有文本:

/bookstore/book/price/text()

代碼

<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book/price/text()"http:// code for IEif (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>

結(jié)果

29.9930.0039.9549.99

選取價格高于 35 的 price 節(jié)點 下面的例子選取價格高于 35 的所有 price 節(jié)點:

/bookstore/book[price>35]/price

代碼

<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book[price>35]/price";// code for IEif (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>

結(jié)果

39.9549.99

選取價格高于 35 的 title 節(jié)點 下面的例子選取價格高于 35 的所有 title 節(jié)點:

/bookstore/book[price>35]/title

代碼

<html><body><script type="text/javascript">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("/example/xmle/books.xml");path="/bookstore/book[price>35]/title";// code for IEif (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>

結(jié)果

Learning XMLXQuery Kick Start

—–下面有個“頂”字,你懂得O(∩_∩)O哈哈~ —–樂于分享,共同進步! —–更多文章請看:http://blog.csdn.net/duruiqi_fx



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴海县| 灯塔市| 仙居县| 象州县| 油尖旺区| 桃园县| 开化县| 延安市| 祥云县| 葫芦岛市| 墨脱县| 略阳县| 塔城市| 屏南县| 安顺市| 新乡市| 黄冈市| 遵化市| 涟水县| 湘潭县| 康马县| 读书| 永年县| 彭水| 淮北市| 安远县| 沈阳市| 祁连县| 侯马市| 洛宁县| 镇沅| 买车| 蕲春县| 怀来县| 崇州市| 会泽县| 苍山县| 哈密市| 上蔡县| 罗江县| 抚远县|