用于返回合格子节点值的字符串串联的 XPath

发布于 2024-08-04 12:45:48 字数 461 浏览 7 评论 0原文

任何人都可以建议一种 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 表达式中可能吗?

谢谢。

Can anyone please suggest an XPath expression format that returns a string value containing the concatenated values of certain qualifying child nodes of an element, but ignoring others:

<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>

The returned value should be a single string:

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

Is this possible in a single XPath expression?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

涙—继续流 2024-08-11 12:45:48

在 XPath 2.0 中:

string-join(/*/node()[not(self::p)], '')

In XPath 2.0 :

string-join(/*/node()[not(self::p)], '')

你爱我像她 2024-08-11 12:45:48

在 XPath 1.0 中:

您可以用来

/div//text()[not(parent::p)]

捕获所需的文本节点。连接本身无法在 XPath 1.0 中完成,我建议在主机应用程序中完成。

In XPath 1.0:

You can use

/div//text()[not(parent::p)]

to capture the wanted text nodes. The concatenation itself cannot be done in XPath 1.0, I recommend doing it in the host application.

倾城°AllureLove 2024-08-11 12:45:48
/div//text()

无论中间节点如何,双斜杠都会强制提取文本

/div//text()

double slash forces to extract text regardless of intermediate nodes

金橙橙 2024-08-11 12:45:48

这种外观有效:

用作上下文 /div/

text() | em/text()

或者不使用上下文:

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

如果要连接前两个字符串,请使用以下内容:

concat(/div/text(), /div/em/text())

This look that works:

Using as context /div/:

text() | em/text()

Or without the use of context:

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

If you want to concat the first two strings, use this:

concat(/div/text(), /div/em/text())
余生共白头 2024-08-11 12:45:48

如果您想要除 p 之外的所有子级,您可以尝试以下...

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

返回...

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

If you want all children except p, you can try the following...

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

which returns...

This text node should be returned.
And the value of this element.
And this.
后来的我们 2024-08-11 12:45:48

我知道这有点晚了,但我认为我的答案仍然有意义。我最近遇到了类似的问题。而且因为我在Python 3.6中使用scrapy,它不支持xpath 2.0,所以我无法使用几个在线答案中建议的string-join函数。

我最终找到了一个简单的解决方法(如下所示),我在任何 stackoverflow 答案中都没有看到该解决方法,这就是我分享它的原因。

temp_selector_list = response.xpath('/div')
string_result = [''.join(x.xpath(".//text()").extract()) for x in temp_selector_list]

希望这有帮助!

I know this comes a bit late, but I figure my answer could still be relevant. I recently ran into a similar problem. And because I use scrapy in Python 3.6, which does not support xpath 2.0, I could not use the string-join function suggested in several online answers.

I ended up finding a simple workaround (as shown below) which I did not see in any of the stackoverflow answers, that's why I'm sharing it.

temp_selector_list = response.xpath('/div')
string_result = [''.join(x.xpath(".//text()").extract()) for x in temp_selector_list]

Hope this helps!

窗影残 2024-08-11 12:45:48

您也可以使用 for-each 循环并将值组合在变量中,如下所示

<xsl:variable name="newstring">
    <xsl:for-each select="/div//text()">
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:variable>

You could use a for-each loop as well and assemble the values in a variable like this

<xsl:variable name="newstring">
    <xsl:for-each select="/div//text()">
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:variable>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文