Xpath in R for element that contains target text itself or in any of their children

Viewed 164

I want to find the element that contains the target text itself or in any of their children.

Sample data:

library(magrittr)
library(xml2)
library(rvest)    
html <- "<button><span><span>as</span></span></button><button><p>ds</p></button><input><span><span>as</span></span><input>"
doc <- html %>% read_html()
doc %>% html_nodes(xpath = "//*[self::button and //*[contains(text(), 'as')]]")

Please consider that my original data is more complex, i check for 10+ strings that could be within the target Elements. Therefore, i would prefer using "//*[self::button or self::Input]" instead of "//button",... Moreover, the target text could be within the target element (button or Input) itself or in any of the children.

Desired Output:

First button and the input

What i tried:

doc %>% html_nodes(xpath = "//*[(self::button or self::input) and //*[contains(text(), 'as')]]")
doc %>% html_nodes(xpath = "//*[(self::button or self::input)]//*[contains(text(), 'as')]")

see How do I select child elements of any depth using XPath?

1 Answers

The problem with your expressions is that they use an absolute location path instead of a relative location path. An example that preserves your style:

//*[(self::button or self::input) and .//*[contains(text(), 'as')]]

More in the XPath style:

//*[self::button|self::input][.//text()[contains(.,'as')]]

Test it here

Related