简体   繁体   中英

.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

The XPath expressions you have given as examples require an XPath 2.0 processor but they are not specific to Saxon.

The expression

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

is a ForExpression , which is specific to XPath 2.0, and is not easily converted to XPath 1.0 because its result is a sequence of strings, and there is no such data type in XPath 1.0.

The expression

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

is specific to XPath 2.0 because it uses a parenthesized expression on the RHS of the "/" operator; and again, because it returns a sequence of strings.

I'm afraid I can't advise you whether there exist XPath 2.0 libraries that run on .NET Core, which I assume is your requirement. Saxon cannot be made to run on .NET Core because of its dependency on IKVM, which doesn't support that platform and which (I gather) cannot readily be adapted to do so.

Note that XPath 2.0 is a subset of XQuery 1.0, so you could extend your search to XQuery 1.0 processors as well as XPath 2.0 processors.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM