网址:https://www.w3school.com.cn/example/xmle/books.xml

使用@属性方式查找元素

案例:获取//*[@category] 属性

xpath使用属性元素定位,包含 and 、or、not

 

 

2.使用父节点获取元素唯一性

如果在搜索栏中,使用//*[@lang='en'],会找到4个,我们可以使用父类中唯一,定位出唯一元素

//book[1]//*[@lang='en']

 

父层级关系与属性使用

//bookstore/book[1]/*[@*]

xpath使用属性元素定位,包含 and 、or、not

 

 

 

2.属性使用not  反向取数

 //bookstore/book[1]/*[not (@*)]

xpath使用属性元素定位,包含 and 、or、not

 

 

 

3.使用使用and 匹配多个满足要求的元素

//book[@category and @cover]

xpath使用属性元素定位,包含 and 、or、not

 

 

 

4.属性使用or定位多个元素

//book[@category or @cover]

xpath使用属性元素定位,包含 and 、or、not

 

 

 

2.查找文本内容

1.在文本中使用大于小于符号获取元素

//book/price[text()=29.99]

或者这样也可以

//book/price[.>40]

xpath使用属性元素定位,包含 and 、or、not

 

//book/title[contains(@lang,"e")]

xpath使用属性元素定位,包含 and 、or、not

 

 

2 根据文本精确查找,查找文本为29.99的内容

//book/price[.=29.99]   #.表示文本
//book/price[.>40]

xpath使用属性元素定位,包含 and 、or、not

 

 

 

3.使用contains模糊匹配

//book/price[contains(.,"29")]

使用contains属性进行模糊匹配

//book/title[contains(@lang,"e")]

xpath使用属性元素定位,包含 and 、or、not

 

相关文章:

  • 2021-10-27
  • 2021-12-10
  • 2021-06-19
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-06-30
猜你喜欢
  • 2021-10-13
  • 2021-09-23
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2022-12-23
相关资源
相似解决方案