Search code examples
xpathxquerysaxonxquery-3.1xpath-3.1

.Net standard library that supports XPath 3.1


I'm maintaining a legacy tool of the company I work for written in C# and I'm converting it to .Net standard 2.0. It uses the Saxon-HE processor to process some XPaths and replace some configurations in files. Its NuGet package on .NET has dependencies that do not allow the execution on all the .Net standard 2.0 compliant platforms (in my case both .Net Framework and .Net core), so I need to replace it with one another tool, better if the standard .Net XPath library.

The problem is that the tool uses some XPaths that perform complex operations such as concatenate strings and select an array item, and I don't know if it's a Saxon-specific syntax or regards a standard.

It is important to know this because if the XPaths are compliant to some XPath standard I could find one another way to process the same XPaths.

Here is some examples:

First:

for $row in /Item/SubItem[*]/SubSubItem return(concat($row, \"/ConcatValue\"))

Second:

/Item/SubItem[*]/SubSubItem/(add[@key=\"TheKey\"]/@value/string(), '')[1]

Do you know something about this XPath syntax?

Thank you


Solution

  • Thanks to this comment I was able to test XPath2.Net and now everything works. I needed to change only one type of XPath definition

    This one:

    /Item/SubItem[*]/SubSubItem/(add[@key=\"TheKey\"]/@value/string(), '')[1]
    

    Changes to

    /Item/SubItem[*]/SubSubItem/(add[@key=\"TheKey\"]/@value/string(.), '')[1]
    

    Please note the additional dot argument of the string() function. This is strange as it should not be require the dot; in fact, per standard

    In the zero-argument version of the function, $arg defaults to the context item. That is, calling fn:string() is equivalent to calling fn:string(.)

    but XPath2 complains with this error

    {"The function 'string'/0 was not found in namespace 'http://www.w3.org/2003/11/xpath-functions'"}
    

    UPDATE:

    After updating the XPath2.Net library to version 1.0.8 the string() syntax works.