【问题标题】:XPath Statement doesn´t return value, but true or falseXPath 语句不返回值,而是 true 或 false
【发布时间】:2020-08-20 05:30:48
【问题描述】:

使用函数添加命名空间后:

var select = useNamespaces({ns1: "http://pippo.com/schema"})

我使用以下语句:

var Objects = select("//ns1:References/ns1:Reference[@ReferenceType="+typeofref+"]/text()="+id.toString(), ns);

我想要的输出是带有 ReferenceType = typeofref 和 text() = id 的所有引用

我得到的输出是:True

它似乎找到了一些东西,但它只是说存在元素,但它没有检索它。有人知道为什么吗?

xm文件就是这个:

https://raw.githubusercontent.com/OPCFoundation/UA-Nodeset/master/Robotics/Opc.Ua.Robotics.NodeSet2.xml

【问题讨论】:

  • 答案是:假

标签: node.js xml xpath opc-ua


【解决方案1】:

您的 XPath 以 = + 一些字符串 结尾,因此只会返回 truefalse

改变

"//ns1:References/ns1:Reference[@ReferenceType="+typeofref+"]/text()="+id.toString()

"//ns1:References/ns1:Reference[@ReferenceType="+typeofref+" and .="+id.toString()+"]"

为了选择所有ns1:References/ns1:Reference 元素具有typeofref 值的@ReferenceType 属性和id.toString() 的字符串值。

【讨论】:

  • 非常感谢。我还有另一个问题:我想获取元素的祖父母(UAObject->References->Reference)。你知道哪个是最好的解决方案吗?我尝试使用祖先::UAObject 但它给了我一个空结果
  • 不客气。我建议向minimal reproducible example 发布一个关于ancestor:: 问题的新问题。谢谢。
  • var Objects = select("//ns1:References/ns1:Reference[@ReferenceType="+typeofref+" and .="+id.toString()+"]/../..", ns);轻松解决
  • 很高兴你能成功。由于UAObject 在命名空间中,您的ancestor::UAObject 尝试可能无法选择任何内容,但我希望看到完整的minimal reproducible example 来确定答案。仅供参考,可以使用带有显式轴的命名空间前缀,例如:ancestor::ns1:UAObject 将选择当前节点的所有 ns1:UAObject 祖先。
  • 嘿,我用了你说的祖先::ns1:UAObject。它有效 如果我只想选择 xml 标记的一个元素怎么办?我使用了这个语句: Objects.id = select( "//ns1:References/ns1:Reference[@ReferenceType=" + typeofref + " and .=" + id.toString() + "]/ancestor::ns1:UAObject [@NodeId]", ns, false);
猜你喜欢
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2023-03-15
  • 1970-01-01
相关资源
最近更新 更多