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>
运行代码
机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年
查看全文
相关阅读:
已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)
暂时解决Sublime Text 2不支持input问题(转)
Python中的注释(转)
You don't have permission to access / on this server
mysql开启慢查询方法(转)
php获取当前url完整地址
js中日期转换为时间戳
发现js端日期跟php端日期格式不一致
首发Zend Studio正式版注册破解(转)
Arduino入门笔记(3):单LED闪烁
原文地址:https://www.cnblogs.com/rubylouvre/p/2238174.html
最新文章
安卓开发_深入理解广播机制
安卓开发_深入学习ViewPager控件
安卓问题集-Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
调整屏幕显示亮度
java.lang.RuntimeException: Fail to connect to camera service问题
ListView的addHeaderView()方法相关问题
安卓开发--android library projects cannot be launched错误
Java.lang.OutOfMemoryError处理
浅谈PopupWindow弹出菜单
Cookie和Session
热门文章
protobuf初识
Go语言基础之流程控制
Go语言基础之包
Go语言标准库之time
常见排序算法
解决go get下载包失败问题
Go语言标准库之flag
为什么你应该学习Go语言?
Elasticsearch
Python 初学笔记(转)
Copyright © 2011-2022 走看看