For an ASP.NET project I wrote a controller action and a form where the user can enter an xpath expression and retrieve the result from an xml file on the server. It works fine, even with some string functions like concat, substring-before and substring-after.
To get rid of some commas in the output, I tried to use string-join and tokenize. However, by using one of these functions I end up with an XPathException.
XPathDocument doc = new XPathDocument(@"C:\temp.xml");
XPathNavigator navigator = doc.CreateNavigator();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(navigator.NameTable);
nsmgr.AddNamespace("x", "systemInfo");
var temp = navigator.Evaluate("string-join(tokenize('The quick brown fox', ' '), ';')", nsmgr); // Exception
Did I use these xpath functions not correctly?
string-join
and tokenize
are XPath 2.0 functions, but .net's XPathNavigator only supports XPath 1.0. You cannot use these functions, and there is no XPath 1.0 equivalent for them.
Either perform string manipulation outside XPath in C# or use some library that extends the capabilities, you might want to have a look at Saxon or BaseX which both offer APIs for C#. There are also some more of them, both open source and commercial ones.
An XPath 1.0 hack for this particular example: translate('The quick brown fox', ' ', ';')
will replace all occurrences of spaces with semicolons..