xml XPath查询如何根据两个属性获取一个属性的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3871065/
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 13:19:51  来源:igfitidea点击:

XPath query how to get value of one attribute based on two attribute

xmlxpath

提问by Umesh K

I want to extract name attribute value from the following tag

我想从以下标签中提取名称属性值

<application
    comments="Do not erase this one"
    executable="run_CIET"
    icon="default"
    instances="1"
    mode="1"
    name="CIET"
    order="10"
    selection="1"
    tool="y"
/>

I can easily get value of name attribute value based on mode value as shown below

我可以根据模式值轻松获取名称属性值的值,如下所示

xpath Applications.xml '//applications/application[@mode='3']'/@name

But if I want to add more condtion which is "get name attribute value when mode=X and tool attribute is not there in application tag"

但是,如果我想添加更多条件,即“在应用程序标记中不存在模式 = X 且工具属性时获取名称属性值”

How do we do this? I tried something like

我们如何做到这一点?我试过类似的东西

xpath Applications.xml '//applications/application[@mode='3' and !@tool]'/@name

but its not working.

但它不工作。

I have not used XPath before and I am finding it tricky I search W3C help on XPath but did not find what I wanted. Please help.

我以前没有使用过 XPath,我发现它很棘手 我在 XPath 上搜索 W3C 帮助但没有找到我想要的。请帮忙。

回答by Dimitre Novatchev

How do we do this? I tried something like 

    xpath Applications.xml '//applications/application[@mode='3' and !@tool]'/@name

but its not working.



!@tool

is invalid syntax in XPath. There is an !=operator, but no !operator.

是 XPath 中的无效语法。有!=操作员,但没有!操作员。

Use:

使用

//applications/application[@mode='3' and not(@tool)]/@name 

There are two things you should always try to avoid:

您应该始终尽量避免两件事:

  1. using the !=operator -- it has weird definition and doesn't behave like the not()function --never use it if one of the operands is a node-set.

  2. Try to avoid as much as possible using the //abbreviation -- this may cause signifficant inefficiency and also has anomalous behavior that isn't apperent to most people.

  1. 使用!=运算符——它有奇怪的定义,并且不像not()函数——如果其中一个操作数是节点集,就不要使用它。

  2. 尽量避免使用//缩写——这可能会导致显着的低效率,并且还会有大多数人不知道的异常行为。

回答by Flynn1179

Using not(@tool)instead of !@toolshould do the job. If your XPath engine's not behaving you could conceivably do count(@tool)=0, but that shouldn't be necessary.

使用not(@tool)而不是!@tool应该可以完成这项工作。如果您的 XPath 引擎不工作,您可以想象这样做count(@tool)=0,但这不是必需的。