Select a node with XPath whose child node contains a specific inner text

后端 未结 4 1719
小鲜肉
小鲜肉 2021-02-14 03:14

Given the following XML:


    
  • abcText1cba
  • abcText2cba
  • 相关标签:
    4条回答
    • 2021-02-14 03:38
      //li[./span[contains(text(),'Text1')]] - have just one target result
      //li[./span[contains(text(),'Text')]] - returns two results as target
      

      This approach is using something that isn't well documented anywhere and just few understands how it's powerful

      Element specified by Xpath has a child node defined by another xpath

      0 讨论(0)
    • Try this XPath

      li/*[@innertext='text']
      
      0 讨论(0)
    • 2021-02-14 03:39

      Your current xpath should be correct. Here is an alternative but ugly one.

      XmlNodeList nodes = doc.SelectNodes("//span/parent::li/span[contains(text(), 'Text1')]/parent::li");
      

      We find all the span-tags. Then we find all the li-tags that has a span-tag as child and contains the 'Text1'.

      OR simply:

      //span[contains(text(), 'Text1')]/parent::li

      0 讨论(0)
    • 2021-02-14 03:41

      Just for readers. The xpath is correct. OP: Perhaps xpath parser didnt support the expression?

      /root/li[span[contains(text(), "Text1")]]
      
      0 讨论(0)
    提交回复
    热议问题