XPath:如何根据元素的值选择元素? [英] XPath: How to select elements based on their value?

查看:35
本文介绍了XPath:如何根据元素的值选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XPath 的新手,这可能是一个基本问题.请耐心等待并帮助我解决问题.我有一个这样的 XML 文件:

I am new to using XPath and this may be a basic question. Kindly bear with me and help me in resolving the issue. I have an XML file like this:

<RootNode>
  <FirstChild>
    <Element attribute1="abc" attribute2="xyz">Data</Element>
  <FirstChild>
</RootNode>

我可以通过以下方式验证 标签的存在:

I can validate the presence of an <Element> tag with:

//Element[@attribute1="abc" and @attribute2="xyz"]

现在我还想检查字符串 "Data" 的标记值.为了实现这一点,我被告知要使用:

Now I also want to check the value of the tag for string "Data". For achieving this I was told to use:

//Element[@attribute1="abc" and @attribute2="xyz" and Data]

当我使用后面的表达式时,出现以下错误:

When I use the later expression I get the following error:

断言失败消息:No Nodes Matched //Element[@attribute1="abc" and @attribute2="xyz" and Data]

Assertion failure message: No Nodes Matched //Element[@attribute1="abc" and @attribute2="xyz" and Data]

请向我提供您的建议,我使用的 XPath 表达式是否有效.如果不是,什么是有效的 XPath 表达式?

Kindly provide me with your advice whether the XPath expression I have used is valid. If not what will be the valid XPath expression?

推荐答案

条件如下:

//Element[@attribute1="abc" and @attribute2="xyz" and Data]

检查 Element 中是否存在元素 Data 而不是元素值 Data.

checks for the existence of the element Data within Element and not for element value Data.

相反,您可以使用

//Element[@attribute1="abc" and @attribute2="xyz" and text()="Data"]

这篇关于XPath:如何根据元素的值选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆