I know it’s a bizarre question!
The requirement is due to the existing code which expects the result of the XPath query to be a single node. Unfortunately, we are unable to make code changes at this time due to factors outside of our control (unless no workaround exists, which would probably mean that we would have to delay our release).
In my case, the XPath currently returns an integer value (count):
count(/someNode/node)
The code uses XPathNavigator.SelectSingleNode() method, which, of course, fails because the result is not a node.
Instead, I need the XPath to return a node, albeit a fake one. Is it possible? Note that because .NET is used, the XPath would have to be 1.0 (not 2.0).
P.S. And, yes, we’ll look into refactoring the code in question ASAP!
If you need your XPath expression to return a node whose text content is equal to the value of
count(/someNode/node), you could do it like this:That might be grossly inefficient, depending on what your input document is like; and it might very well fail if there is no text node in the input document that contains the right number. However in some circumstances it could do the trick, and might help you avoid delaying your release.
For example, if your input XML is small, and the range of possible values for the
count(...)is limited, and if you are able to make changes to the XML, then you could include extra text or attribute nodes in your input XML to make sure the needed numbers are available.