javascript XPath 示例

本文介绍了一个使用XPath进行XML文档节点选择的测试案例。通过HTML页面中的JavaScript代码演示了如何根据不同浏览器选择适当的XML解析方式,并提供了针对IE和Firefox的具体实现。用户可以通过输入XPath表达式来获取XML文档中匹配的节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

books.html 代码
 
  1. <html>  
  2.     <head>  
  3.         <title>xpath testtitle>  
  4.         <script type="text/javascript">  
  5.             var xmlDoc ;  
  6.             var exploer;  
  7.               
  8.             loadXML();  
  9.               
  10.             //载入Xml文档  
  11.         function loadXML(){  
  12.         // IE 浏览器  
  13.          if (window.ActiveXObject)  {    
  14.               exploer='ie';  
  15.             xmlDoc  = new ActiveXObject("Microsoft.XMLDOM");    
  16.             xmlDoc.asyncfalse;    
  17.             xmlDoc.load("books.xml");    
  18.          }  
  19.          // Mozilla, Firefox, Opera浏览器  
  20.          else if (document.implementation &&document.implementation.createDocument){   
  21.                exploer='firefox';   
  22.              xmlDoc  =  document.implementation.createDocument("","",null);    
  23.              xmlDoc.load("books.xml");  
  24.          }  
  25.          else{    
  26.              alert("由于你的浏览器不支持Javascript,目录树无法加载...");    
  27.          }  
  28.       }  
  29.         
  30.       function fnIEParse(path){  
  31.       
  32.                 alert(path);  
  33.                 var nodes = xmlDoc.selectNodes(path);  
  34.                 for(var i=0;i<nodes.length;i++){  
  35.                     var node = nodes[i];  
  36.                     alert(node.xml);  
  37.                 }  
  38.       }  
  39.         
  40.       function fnFirefoxParse(path){  
  41.         alert(path);  
  42.         var oNSResolver = xmlDoc.createNSResolver(xmlDoc.documentElement);  
  43.         var nodes = xmlDoc.evaluate(xpath,xmlDoc, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);  
  44.         alert("");  
  45.           
  46.       }  
  47.                       
  48.             function fnSelectNodes(){  
  49.                 var path=document.getElementById("path").value;  
  50.                 if(exploer=='ie'){  
  51.                     fnIEParse(path);  
  52.                 }else if(exploer='firefox'){  
  53.                     fnFirefoxParse(path);  
  54.                 }else{  
  55.                     alert("由于你的浏览器不支持Javascript,path...");    
  56.                 }  
  57.             }  
  58.               
  59.         script>  
  60.     head>  
  61.     <body>  
  62.         <input type="text" id="path" size="120"/>  
  63.         <input type="button" value="get nodes" onclick="fnSelectNodes()"/>  
  64.       <br>  
  65.       <div id="books">  
  66.         <iframe src="books.xml" width="100%" height="90%">?iframe>  
  67.       div>  
  68.     body>  
  69. html>  

books.xml 代码
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <bookstore>  
  4.   
  5. <book category="COOKING">  
  6.   <title lang="en">Everyday Italiantitle>  
  7.   <author>Giada De Laurentiisauthor>  
  8.   <year>2005year>  
  9.   <price>30.00price>  
  10. book>  
  11.   
  12. <book category="CHILDREN">  
  13.   <title lang="en">Harry Pottertitle>  
  14.   <author>J K. Rowlingauthor>  
  15.   <year>2005year>  
  16.   <price>29.99price>  
  17. book>  
  18.   
  19. <book category="WEB">  
  20.   <title lang="en">XQuery Kick Starttitle>  
  21.   <author>James McGovernauthor>  
  22.   <author>Per Bothnerauthor>  
  23.   <author>Kurt Cagleauthor>  
  24.   <author>James Linnauthor>  
  25.   <author>Vaidyanathan Nagarajanauthor>  
  26.   <year>2003year>  
  27.   <price>49.99price>  
  28. book>  
  29.   
  30. <book category="WEB">  
  31.   <title lang="zh">Learning XMLtitle>  
  32.   <author>Erik T. Rayauthor>  
  33.   <year>2003year>  
  34.   <price>39.95price>  
  35. book>  
  36.   
  37. bookstore>  

输入
 //book[title[@lang='en'] and price<=30]/title/text()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值