路径表达式 | 结果 |
bookstore |
选取 bookstore 元素的所有子节点。 |
/bookstore |
选取根元素 bookstore。
|
bookstore/book |
选取属于 bookstore 的子元素的所有 book 元素。 |
//book |
选取所有 book 子元素,而不管它们在文档中的位置。 |
bookstore//book |
选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。 |
//@lang |
选取名为 lang 的所有属性。 |
//div/form/input[2] |
匹配 上级节点 div 下节点是 form 中的第二个 input 元素 |
//div//div[@id=‘ls‘]/span[position()=3] |
position() 索引函数 |
//div//div[@id=‘ls‘]/span[position()<3] |
position() 索引函数 |
//div//div[@id=‘ls‘]/span[last()] |
last() 索引 表示同级的最后一个元素 |
//div//div[@id=‘ls‘]/span[last()-1] |
last() 索引 表示同级的倒数第二个元素 |
//div//input[@type=‘radio‘ and @value=‘sx‘] |
and 匹配的input元素要两个条件同时满足 |
//div//input[@name=‘password‘ or @name="username"] |
or 或者的关系,匹配的input元素两个条件满足一个即可 |
//div//input[not(@id=‘username‘)] |
not 除了当前条件 匹配div中id不等于username的所有input元素 |
//a[contains(@class,‘btn-primary‘)] |
contains 匹配 a标签的class属性中包含btn-primary元素 |
//div//input[starts-with(@id,‘user‘)] |
starts-with 匹配当前id属性是以‘user‘开头的所有input元素 |
//div[substring(@id,1,11)=‘_mail_input‘] |
substring 匹配div中id的第1-11个字符内容为 _mail_input 的元素 |
//div[substring-before(@id,‘-‘)=‘_mail_input_‘] |
substring-before 匹配div中id内容在 ‘-‘ 之前等于_mail_input_ 的元素 |
//div[substring-after(@id,‘-‘)=‘_224‘ |
substring-after 匹配div中id内容在 ‘-‘ 之后等于 _224 的元素
|
bookstore/* |
选取 bookstore 元素的所有子元素。 |
//* |
选取文档中的所有元素 |
//title[@*] |
选取所有带有属性的 title 元素。 |
//input[@*=‘radio‘] |
选取所有属性值中有radio的input元素 |
//book/title | //book/price |
选取 book 元素的所有 title 和 price 元素。 |
//title | //price |
选取文档中的所有 title 和 price 元素。 |
/bookstore/book/title | //price |
选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档中所有的 price 元素。 |