XPath 在双重条件下取元素 [英] XPath take element on double condition
问题描述
我有一个来自 OPC-UA 的信息模型,用 xml (https://github.com/OPCFoundation/UA-Nodeset/blob/v1.04/Robotics/Opc.Ua.Robotics.NodeSet2.xml).从这个模型我想:查看 UAObject 中的引用并仅在引用具有时才获取 DisplayName1)我要找的nodeid2) 有一个字段 IsRecursive="false"
I have an InformationModel from OPC-UA, written in xml (https://github.com/OPCFoundation/UA-Nodeset/blob/v1.04/Robotics/Opc.Ua.Robotics.NodeSet2.xml). From this model I want to: Look at the reference inside an UAObject and get the DisplayName only if the reference has 1)The nodeid I am looking for 2)Has a field IsRecursive="false"
我尝试使用此代码来获取所有对象的引用,该引用指向我指定的 nodeid 并且它可以工作.这是它的代码:
I tried this code for getting all object with a reference which point to my specified nodeid and it works. Here it is the code for it:
var ObjectsName2 = select(
"//ns1:References/ns1:Reference[.=" +
formatted_id + "]/ancestor::ns1:UAObject/ns1:DisplayName/text()",
nodes,
false
);
现在我想采用具有特定 nodeid 且字段 IsForward 设置为false"的那个.所以我有一个双重条件需要用 AND 来验证.这是我正在使用的代码:
Now I want to take the one that has the specific nodeid and has field IsForward setted to "false". So I have a double condition that needs to be verified with an AND. Here it is the code I am using:
var formatted_id = '"' + ParentNodeId + '"';
var negate = "false";
var ObjectsName = select(
"//ns1:References/ns1:Reference[.=" +
formatted_id +
"//@ns1:IsForward="+negate+"]/ancestor::ns1:UAObject/ns1:DisplayName/text()",
nodes,
false
);
错误的结果是一个空数组.为了让这里更容易理解,这是一个显示我想要的示例.
The wrong result is an empty array. To make a bit easier to understand here it is an example that shows what I want.
假设您有这个 xml 条目,而我要查找的节点是 ns=1;i=15008:
Imagine you have this xml entry and the node that I am looking for is ns=1;i=15008:
<UAObject NodeId="ns=1;i=15024" BrowseName="2:ParameterSet" ParentNodeId="ns=1;i=15008">
<DisplayName>ParameterSet</DisplayName>
<Description>Flat list of Parameters</Description>
<References>
<Reference ReferenceType="HasComponent">ns=1;i=15061</Reference>
<Reference ReferenceType="HasTypeDefinition">i=58</Reference>
<Reference ReferenceType="HasModellingRule">i=78</Reference>
<Reference ReferenceType="HasComponent" IsForward="false">ns=1;i=15008</Reference>
</References>
</UAObject>
如您所见,有一个名为HasComponent"的引用.它指向我的节点 (ns=1;i=15008) 并且 IsForward=false".
As you can see there is a Reference called "HasComponent" which points to my node (ns=1;i=15008) and IsForward="false".
预期的输出应该是输出数组中UAObject的显示名称-->ParameterSet有人可以帮我吗?
The expected output should be the display name of the UAObject in the output array -->ParameterSet Can someone help me?
推荐答案
单纯基于xpath,这个表达式:
Simply based on xpath alone, this expression:
//Reference[@ReferenceType="HasComponent"][@IsForward="false"]
应在示例 xml 中选择 ns=1;i=15008
.
should select ns=1;i=15008
in your sample xml.
这篇关于XPath 在双重条件下取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!