【问题标题】:xpath contains text does not work with or operatorxpath 包含文本不适用于 or 运算符
【发布时间】:2021-04-23 21:41:44
【问题描述】:

似乎contains(text(), 'TARGET_STRING')html_nodes 以及“或运算符”:“|”可以正常工作,但不能一起工作。

可重现的例子:

html <- "<a>a</a><p>abc</p>"

xp <- "//*[self::a|self::b]" # or operator works
xp2 <- "//*[contains(text(),'abc')]" # contains text works

# but it doesnt work together
xp3 <- "//*[self::a|contains(text(),'abc')]" 

html_nodes(x = read_html(html), xpath = xp)
html_nodes(x = read_html(html), xpath = xp2)

# this one fails
html_nodes(x = read_html(html), xpath = xp3)

【问题讨论】:

    标签: r xpath


    【解决方案1】:

    只需将| 替换为or 即可:

    //*[self::a or contains(text(),'abc')]
    

    【讨论】:

    • 哦,哇,本来可以试试的。你知道为什么“|”运营商不在这里工作? - P.S:我可以在三分钟内接受。
    • @Tlatwork ,注意|实际上不是OR运算符,而是union set operator
    • 哈哈好吧,如果你仔细阅读stackoverflow.com/questions/5350666/…(我的来源)它实际上也在 cmets 中提到。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 2020-07-13
    • 2021-04-30
    • 2020-09-28
    • 2020-08-03
    相关资源
    最近更新 更多