XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。
下面列出了最有用的路径表达式:
nodename 选取此节点的所有子节点。
/ 从根节点选取。
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
. 选取当前节点。
.. 选取当前节点的父节点。
@ 选取属性。
绝对vs相对XPath(/vs .)
/ 引入绝对位置路径,从文档的根开始。
. 从上下文节点开始引入相对位置路径。
命名元素vs任何元素(enamevs *)
/ename选择一个ename根元素
./ename选择ename当前节点的所有子元素。
/* 选择根元素,无论名称如何。
./*或者*选择上下文节点的所有子元素,而不管名称。
后代或自我轴(//*)
//ename选择ename文档中的所有元素。
.//ename选择ename上下文节点或其下的所有元素。
//* 选择文档中的所有元素,无论名称如何。
.//* 选择上下文节点处或下方的所有元素,无论名称如何。