首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >返回符合条件的子节点值的字符串连接的XPath

返回符合条件的子节点值的字符串连接的XPath
EN

Stack Overflow用户
提问于 2009-09-10 08:06:37
回答 5查看 50.7K关注 0票数 22

有没有人可以建议一种XPath表达式格式,它返回一个字符串值,其中包含元素的某些合格子节点的串连值,但忽略其他节点:

<div>
    This text node should be returned.
    <em>And the value of this element.</em>
    And this.
    <p>But this paragraph element should be ignored.</p>
</div>

返回值应为单字符串:

This text node should be returned. And the value of this element. And this.

这在单个XPath表达式中是可能的吗?

谢谢。

EN

回答 5

Stack Overflow用户

发布于 2009-09-10 08:13:32

这样的外观是有效的:

使用as context /div/

text() | em/text()

或者不使用上下文:

/div/text() | /div/em/text()

如果您想要连接前两个字符串,请使用以下命令:

concat(/div/text(), /div/em/text())
票数 6
EN

Stack Overflow用户

发布于 2009-09-10 08:14:48

/div//text()

双斜杠强制提取文本,而不考虑中间节点

票数 6
EN

Stack Overflow用户

发布于 2013-06-18 08:57:48

如果您想要除p之外的所有子项,您可以尝试执行以下操作...

    string-join(//*[name() != 'p']/text(), "")

它返回..。

This text node should be returned.
And the value of this element.
And this.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1403971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档