xml 通过内文选择 XPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1998681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:54:26  来源:igfitidea点击:

XPath selection by innertext

xmlxpath

提问by kdt

I'm trying to extract an element with a particular innertext from a parsed XML document. I know that I can select an element that has a child with a particular innertext using //myparent[mychild='foo'], but I actually just want to select the "mychild" element in this example.

我试图从解析的 XML 文档中提取具有特定内部文本的元素。我知道我可以使用 选择一个具有特定内部文本的子元素的元素//myparent[mychild='foo'],但实际上我只想在此示例中选择“mychild”元素。

<myparent>
  <mychild>
    foo
  </mychild>
</myparent>

What would be the XPath query for "foo" that would return the "mychild" node?

返回“mychild”节点的“foo”的 XPath 查询是什么?

回答by glmxndr

Have you tried this?

你试过这个吗?

//myparent/mychild[text() = 'foo']

Alternatively, you can use the shortcut for the selfaxis:

或者,您可以使用self轴的快捷方式:

//myparent/mychild[. = 'foo']

回答by P?l Thingb?

Matt said it, but the full solution:

马特说过,但完整的解决方案:

//myparent[mychild='foo']/mychild

回答by Sándor Krisztián

You might consider using the containsfunction to return true/false if the test was found like so:

contains如果像这样找到测试,您可能会考虑使用该函数返回真/假:

//mychild[contains(text(),'foo')]

See XSLT, XPath, and XQuery Functionsfor functions reference

有关函数参考请参阅XSLT、XPath 和 XQuery 函数