books.html 代码
books.xml 代码
- <html>
- <head>
- <title>xpath testtitle>
- <script type="text/javascript">
- var xmlDoc ;
- var exploer;
- loadXML();
- //载入Xml文档
- function loadXML(){
- // IE 浏览器
- if (window.ActiveXObject) {
- exploer='ie';
- xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
- xmlDoc.async= false;
- xmlDoc.load("books.xml");
- }
- // Mozilla, Firefox, Opera浏览器
- else if (document.implementation &&document.implementation.createDocument){
- exploer='firefox';
- xmlDoc = document.implementation.createDocument("","",null);
- xmlDoc.load("books.xml");
- }
- else{
- alert("由于你的浏览器不支持Javascript,目录树无法加载...");
- }
- }
- function fnIEParse(path){
- alert(path);
- var nodes = xmlDoc.selectNodes(path);
- for(var i=0;i<nodes.length;i++){
- var node = nodes[i];
- alert(node.xml);
- }
- }
- function fnFirefoxParse(path){
- alert(path);
- var oNSResolver = xmlDoc.createNSResolver(xmlDoc.documentElement);
- var nodes = xmlDoc.evaluate(xpath,xmlDoc, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
- alert("");
- }
- function fnSelectNodes(){
- var path=document.getElementById("path").value;
- if(exploer=='ie'){
- fnIEParse(path);
- }else if(exploer='firefox'){
- fnFirefoxParse(path);
- }else{
- alert("由于你的浏览器不支持Javascript,path...");
- }
- }
- script>
- head>
- <body>
- <input type="text" id="path" size="120"/>
- <input type="button" value="get nodes" onclick="fnSelectNodes()"/>
- <br>
- <div id="books">
- <iframe src="books.xml" width="100%" height="90%">?iframe>
- div>
- body>
- html>
books.xml 代码
- xml version="1.0" encoding="UTF-8"?>
- <bookstore>
- <book category="COOKING">
- <title lang="en">Everyday Italiantitle>
- <author>Giada De Laurentiisauthor>
- <year>2005year>
- <price>30.00price>
- book>
- <book category="CHILDREN">
- <title lang="en">Harry Pottertitle>
- <author>J K. Rowlingauthor>
- <year>2005year>
- <price>29.99price>
- book>
- <book category="WEB">
- <title lang="en">XQuery Kick Starttitle>
- <author>James McGovernauthor>
- <author>Per Bothnerauthor>
- <author>Kurt Cagleauthor>
- <author>James Linnauthor>
- <author>Vaidyanathan Nagarajanauthor>
- <year>2003year>
- <price>49.99price>
- book>
- <book category="WEB">
- <title lang="zh">Learning XMLtitle>
- <author>Erik T. Rayauthor>
- <year>2003year>
- <price>39.95price>
- book>
- bookstore>
输入
//book[title[@lang='en'] and price<=30]/title/text()