XPATH了解

特殊标签

找SVG这种特殊标签可以使用[name()='svg'],如//[name()='svg']/[name()='line'][2]

文本

找标签内的文本时可以使用: //*[text()='abc']
如果有空格或回车什么的,就截空下://a[normalize-space(text())='abc']
如果想取回当前节点下的所有文本可用://a[normalize-space(.)]

常用函数

函数名 解释
contains(@class,'btn') class属性包含btn
starts-with(@class,'btn') class属性以btn开头
ends-with(@class,'btn') class属性以btn结尾
text() 文本
//input[@type='a'][@name='b'] 并且
//input[@type='a' and @name='b'] 并且
//input[@type='a' or @name='b']
//input[@*='a'] 匹配所有属性
//a[last()] a的最后一个子元素

boolean()
ceiling()
choose()
concat()
contains()
count()
current() XSLT-specific
document() XSLT-specific
element-available()
false()
floor()
format-number() XSLT-specific
function-available()
generate-id() XSLT-specific
id() (partially supported)
key() XSLT-specific
lang()
last()
local-name()
name()
namespace-uri()
normalize-space()
not()
number()
position()
round()
starts-with()
string()
string-length()
substring()
substring-after()
substring-before()
sum()
system-property() XSLT-specific
translate()
true()
unparsed-entity-url() XSLT-specific (not supported)

轴 descendant

XPath轴(XPath Axes)可定义某个相对于当前节点的节点集:
语法:

轴名称::节点测试[谓语]
选取值
child 选取当前节点的所有子元素
parent 选取当前节点的父节点
descendant 选取当前节点的所有后代元素(子、孙等)
ancestor 选取当前节点的所有先辈(父、祖父等)
descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身
ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身
preceding-sibling 选取当前节点之前的所有同级节点
following-sibling 选取当前节点之后的所有同级节点
preceding 选取文档中当前节点的开始标签之前的所有节点
following 选取文档中当前节点的结束标签之后的所有节点
self 选取当前节点
attribute 选取当前节点的所有属性
namespace 选取当前节点的所有命名空间节点

Refer to

https://developer.mozilla.org/en-US/docs/Web/XPath/Functions
https://www.cnblogs.com/jpfss/p/10973806.html
https://www.cnblogs.com/zhaozhan/archive/2009/09/10/1563728.html
http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html

原文地址:https://www.cnblogs.com/liehen2046/p/11468561.html