How to use XPath 2.0 methods in .NET 4.0?

I am using .NET 4.0, and I would like to use XPath 2.0 methods such as [[Matches ()] [1], [upper-case ()] [2], [lower-case ()] [3]) when trying to find items in a document.

XPath example: "/MyDocument/MyNode[matches(@MyAttribute, 'MyValue', 'i')]"

I tried using:

  • System.Xml.XPath.XPathNavigator.Compile()
  • System.Xml.XmlDocument.SelectNodes()
  • System.Xml.Linq.XDocument.SelectElements()

But I basically throw an "UndefinedXsltContextException" (or something similar) exception. Can this be done in .NET 4.0, and if so, can you provide a small example of how to configure it to work?

thanks

+6
source share
1 answer

.NET does not currently support XPath 2.0. See this question for more information and third-party alternatives: XPath and XSLT 2.0 for .NET?

If you do not want to use third-party libraries, you can perform the minimum necessary request to get your target element with XPath 1.0 or LINQ to XML, and then do additional work with data using .NET methods to perform the required checks and changes:

  • Matches = Regex.IsMatch - remember that the XPath regular expression pattern may have different metacharacters than the .NET pattern, so translation may be required.
  • upper-case = String.ToUpper - the link also mentions culture / invariant parameters if you need them.
  • lower-case = String.ToLower
+7
source

Source: https://habr.com/ru/post/897862/


All Articles