XPath 路径表达式
参考资料
XPath 路径表达式
XPath 路径表达式
基本概念
XML路径语言
用于在XML文档中导航和选择节点
W3C标准
节点类型
元素节点
属性节点
文本节点
命名空间节点
处理指令节点
注释节点
文档(根)节点
路径表达式语法
绝对路径:/root/child
相对路径:child/grandchild
当前节点:.
父节点:..
通配符:*
常用表达式
选择所有节点://*
选择特定元素://element
选择带有属性的元素://element[@attribute]
选择特定属性值://element[@attribute='value']
谓语(Predicates)
用于查找特定节点
示例://book[1] (第一个book元素)
示例://book[price>35] (价格大于35的book)
轴(Axes)
ancestor
ancestor-or-self
attribute
child
descendant
descendant-or-self
following
following-sibling
namespace
parent
preceding
preceding-sibling
self
运算符
| (联合)
(加)
(减)
(乘)
div (除)
= (等于)
!= (不等于)
< (小于)
<= (小于等于)
(大于)
= (大于等于)
or
and
mod (取模)
函数
节点函数:position(), last(), count()
字符串函数:concat(), substring(), contains()
布尔函数:true(), false(), not()
数字函数:sum(), floor(), ceiling()
示例
选择所有book元素://book
选择title属性为"XML"的book://book[@title='XML']
选择price大于30的book://book[price>30]
选择第一个book的子title:/bookstore/book[1]/title
选择所有title和price元素://title | //price