System.Xml.XPath.XPathException:执行 SelectSingleNode(“//(artist|author)”) 时,表达式的计算结果必须为节点集

发布于 2024-07-15 00:23:14 字数 357 浏览 5 评论 0原文

有人可以解释一下为什么这不起作用吗?

我正在执行

XmlNode xmlNode = xmlDocument.SelectSingleNode("//(artist|author)");

并且我得到了,

System.Xml.XPath.XPathException: Expression must evaluate to a node-set.

但是即使有很多艺术家节点,这也有效并且不会引发异常

XmlNode xmlNode = xmlDocument.SelectSingleNode("//artist");

Can somebody explain me why is this not working?

I'm executing

XmlNode xmlNode = xmlDocument.SelectSingleNode("//(artist|author)");

and I get

System.Xml.XPath.XPathException: Expression must evaluate to a node-set.

but this works and does not raise the exception even when there are many artist nodes

XmlNode xmlNode = xmlDocument.SelectSingleNode("//artist");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-07-22 00:23:14

据我所知,您可以使用“|” 就在 XPath 查询的顶层,因此请尝试查询再见,

    "//artist|//author"

递归搜索 (//) 的方式不是很快,因此请确保您的 dom 文档很小。

更新:

我在 规范 中查找了它:

3.3 节点集

位置路径可以用作
表达。 表达式返回
由路径选择的节点集。

| 运算符计算并集
它的操作数,必须是节点集。

这意味着您在“|”左右写的任何内容 需要单独用作 xpath 查询,即“|” 然后从中创建联合。

具体来说,您不能说“递归搜索(称为作者的东西或称为艺术家的东西)”,因为“称为作者的东西”不会评估 xpath 查询(节点集)的结果。

To my knowledge you can use '|' just at the top level of an XPath Query, so try the query

    "//artist|//author"

Bye the way doing recursive searches (//) isn't very fast, so make sure your dom document is small.

Update:

I looked it up in the specification:

3.3 Node-sets

A location path can be used as an
expression. The expression returns the
set of nodes selected by the path.

The | operator computes the union of
its operands, which must be node-sets.

That means whatever you write left and right of "|" needs to be usable as an xpath query on its own, the "|" then just creates the union from it.

Specifically you can not say "search recursively for (something called author OR something called artist)" because "something called author" does not evaluate to the result of an xpath-query (a node set).

做个ˇ局外人 2024-07-22 00:23:14
  1. //artist|//author 适用于 XPATH 1.0 和 2.0
  2. //(artist|author) 适用于 XPATH 2.0

Microsoft 是一家懒惰的公司。 他们的框架仅支持 XPATH 1.0

  1. //artist|//author works with XPATH 1.0 and 2.0
  2. //(artist|author) works with XPATH 2.0

Microsoft is a lazy corporation. Their framework support only XPATH 1.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文