XPath/XQuery:在节点中查找文本,但忽略特定后代元素的内容 [英] XPath / XQuery: find text in a node, but ignoring content of specific descendant elements
问题描述
我试图找到一种在节点内搜索字符串的方法,但不包括这些节点的某些子元素的内容.简单明了,我想在文本的段落中搜索字符串,不包括作为段落子元素的脚注.
I am trying to find a way to search for a string within nodes, but excluding ythe content of some subelements of those nodes. Plain and simple, I want to search for a string in paragraphs of a text, excluding the footnotes which are children elements of the paragraphs.
例如
我的文档是:
<document>
<p n="1">My text starts here/</p>
<p n="2">Then it goes on there<footnote>It's not a very long text!</footnote></p>
</document>
当我搜索文本"时,我希望 Xpath/XQuery
检索第一个 p 元素,而不是第二个(其中文本"仅包含在脚注中子元素).
When I'm searching for "text", I would like the Xpath / XQuery
to retrieve the first p element, but not the second one (where "text" is contained only in the footnote subelement).
我尝试了 contains()
函数,但它检索了两个 p 元素.
I have tried the contains()
function, but it retrieves both p elements.
任何帮助将不胜感激:)
Any help would be much appreciated :)
推荐答案
我想在文本的段落,不包括作为子元素的脚注段落的
I want to search for a string in paragraphs of a text, excluding the footnotes which are children elements of the paragraphs
XPath 1.0 - 唯一的解决方案:
An XPath 1.0 - only solution:
使用:
//p//text()[not(ancestor::footnote) and contains(.,'text')]
针对以下 XML 文档(从您的文档中获取,但在 footnote
中添加了 p
以使其更有趣):
Against the following XML document (obtained from yours but added p
s within a footnote
to make this more interesting):
<document>
<p n="1">My text starts here/</p>
<p n="2">Then it goes on there
<footnote>It's not a very long text!
<p>text</p>
</footnote>
</p>
</document>
这个 XPath 表达式准确地选择了想要的文本节点:
My text starts here/
这篇关于XPath/XQuery:在节点中查找文本,但忽略特定后代元素的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!