Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Xml.XPath.XPathException: Expression must evaluate to a node-set when executing SelectSingleNode("//(artist|author)")

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");
like image 710
knoopx Avatar asked Jan 24 '23 18:01

knoopx


1 Answers

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).

like image 149
froh42 Avatar answered Jan 29 '23 08:01

froh42