【问题标题】:XPath concat two childrenXPath 连接两个孩子
【发布时间】:2021-07-15 11:18:17
【问题描述】:

我知道这是一个常见问题(我找到了Concatenate multiple node values in xpath XPath joining multiple elements 和其他一些问题),但对于我的生活,我无法弄清楚。我有以下 HTML:

<div class="product-card__price__new"><span class="product-card__price__euros">0.</span> <span class="product-card__price__cents">69</span></div>

我需要从中提取 0.69。我尝试了以下 XPATH:

'.//*[@class="product-card__price__new"]/concat(/span/text(), following-sibling::span[1]/text)'
'.//*[@class="product-card__price__new"]/span/text()/concat(., following-sibling::span[1]/text)'
'.//*[@class="product-card__price__new"]/text()'

但我一直一无所获。提取它的正确表达式是什么?

【问题讨论】:

    标签: html xpath


    【解决方案1】:

    比这简单得多。元素的字符串值是其所有后代文本节点的串联。所以只是

    string(.//*[@class="product-card__price__new"])

    在许多情况下,您不需要调用 string(),因为它是隐式的。

    大多数情况下,人们使用text() 时,他们 (a) 使事情过于复杂和/或 (b) 弄错了。

    【讨论】:

    • 谢谢!我也会更新我的其他 text() 选择器。
    • 您的建议实际上返回0. 69,因为两个跨节点之间存在空间。我建议改为.//*[@class="product-card__price__new"]/string-join(span)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2022-10-19
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多