zoukankan
html css js c++ java
xpath的一些测试
<!DOCTYPE html> <html> <head> <title>xpath test by 司徒正美</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="/dom/dom.js"></script> <script> window.onload = function(){ var xmlstr = '<?xml version="1.0" encoding="ISO-8859-1"?>'+ document.getElementsByTagName("xml")[0].innerHTML //.html(); function parseXML( data,xml,tmp ) { try { if ( window.DOMParser ) { // Standard tmp = new DOMParser(); xml = tmp.parseFromString( data , "text/xml" ); } else { // IE xml = new ActiveXObject( "Microsoft.XMLDOM" ); xml.async = "false"; xml.loadXML( data ); xml.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"); xml.setProperty("SelectionLanguage", "XPath"); } } catch( e ) { xml = undefined; } if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) { throw ( "Invalid XML: " + data ); } return xml; } var xml = parseXML(xmlstr); function getElementsByXPath(xpath,context,doc) { var result = []; if( doc.evaluate){ var nodes = doc.evaluate(xpath, context, null, 7, null); for (var i = 0, n = nodes.snapshotLength; i < n; i++){ result[i] = nodes.snapshotItem(i) } }else{ nodes = context.selectNodes(xpath); for (i = 0, n = nodes.length; i < n; i++){ result[i] = nodes[i] } } return result; }; var $X = function(expr){ var nodes = getElementsByXPath(expr,xml,xml); console.log(nodes) return nodes } showIDs = function(expr){ var nodes = getElementsByXPath(expr,xml,xml); for(var i=0,el;el = nodes[i++];){ console.log(el.getAttribute("id")) } } $X("//book"); $X("//book/title") $X("//book//title") $X("//book/child::title") $X('//book//title[@class="classname2"]') $X("/descendant-or-self::node()[@class='classname2']") $X("/descendant-or-self::node()[@class='classname']") $X("//node()[@class='classname']") $X("//*[@class='classname2']") console.log("==================") $X("id('aa0')") showIDs("//node()[@class='classname']") } </script> </head> <body> <xml> <bookstore> <title class="classname0" id="aa0">ddd</title> <title class="classname" id="aa1">ddd</title> <book> <title lang="en" class="classname" id="aa2">foo</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book> <title lang="en" class="classname" id="aa3">bar</title> <author>J K. Rowling<title class="classname2">ddd</title></author> <year>2005</year> <price>29.99</price> </book> </bookstore> </xml> </body> </html>
运行代码
机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年
查看全文
相关阅读:
8位单片机可用的 mktime localtime函数
【转载】linux获取mac地址
【转载】openwrt框架分析
JVM调优工具Arthas的使用
Grafana监控JVM
JAVA死锁排查-性能测试问题排查思路
JVM的堆内存泄漏排查-性能测试
性能测试之JVM的故障分析工具VisualVM
性能测试之 JVM 异常说明和分析工具
性能测试之 JVM 概念认识
原文地址:https://www.cnblogs.com/rubylouvre/p/2238174.html
最新文章
Appium+Python-入门实例(计算器)
Monkey框架(测试方法篇)
Monkey框架(测试方法篇)
Monkey框架(基础知识篇)
Monkey框架(基础知识篇)
基于胜率矩阵的PageRank排序
Random Thoughts on Deep Reinforcement Learning
《Population Based Training of Neural Networks》论文解读
AlphaZero并行五子棋AI
Gumbel-Softmax Trick和Gumbel分布
热门文章
《Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments》论文解读
《Playing hard exploration games by watching YouTube》论文解读
MDP中值函数的求解
树莓派+QT5.9.9交叉编译(QT环境配置篇)
树莓派+QT5.9.9交叉编译,100%可以使用的交叉编译
QT_移动APP设计(环境搭建篇)
QML动态标注线
QML文字灰飞烟灭(优化篇)
QML文字灰飞烟灭效果
QML设计飘散效果
Copyright © 2011-2022 走看看