首页 > 学院 > 开发设计 > 正文

XPath教程- 实例练习

2019-11-08 03:13:26
字体:
来源:转载
供稿:网友

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 文档 所有现代浏览器都支持使用 xmlhttpRequest 来加载 XML 文档的方法。 针对大多数现代浏览器的代码:

var xmlhttp=new XMLHttpRequest()

针对古老的微软浏览器(IE 5 和 6)的代码: var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

选取节点 不幸的是,Internet Explorer 和其他处理 XPath 的方式不同。 在我们的例子中,包含适用于大多数主流浏览器的代码。 Internet Explorer 使用 selectNodes() 方法从 XML 文档中的选取节点:

xmlDoc.selectNodes(xpath);

FirefoxChromeOpera 以及 Safari 使用 evaluate() 方法从 XML 文档中选取节点:

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

选取所有 title 下面的例子选取所有 title 节点:

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

结果:

Harry PotterEveryday ItalianLearning XMLXQuery Kick Start

选取第一个 book 的 title 下面的例子选取 bookstore 元素下面的第一个 book 节点的 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>

结果

Harry Potter

这里有一个问题。上面的例子在 IE 和其他浏览器中输出不同的结果。

IE5 以及更高版本将 [0] 视为第一个节点,而根据 W3C 的标准,应该是 [1]。

为了解决 IE5+ 中 [0] 和 [1] 的问题,可以为 XPath 设置语言选择(SelectionLanguage)。

下面的例子选取 bookstore 元素下面的第一个 book 节点的 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>

结果

Harry Potter

选取所有价格 下面的例子选取 price 节点中的所有文本:

/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()"// 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>

结果

29.9930.0039.9549.99

选取价格高于 35 的 price 节点 下面的例子选取价格高于 35 的所有 price 节点:

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

结果

39.9549.99

选取价格高于 35 的 title 节点 下面的例子选取价格高于 35 的所有 title 节点:

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

结果

Learning XMLXQuery Kick Start

—–下面有个“顶”字,你懂得O(∩_∩)O哈哈~ —–乐于分享,共同进步! —–更多文章请看:http://blog.csdn.net/duruiqi_fx



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表