String greater, less, and equal comparison in XmlDocument

纵饮孤独 提交于 2019-11-28 10:04:57

问题


i'm trying to do a string comparison in a XmlDocument, and the following is what i tried. I am wondering why the first 2 yield the right result, and the last 2 doesn't return any result.

What i was trying to do is to filter out nodes based on a datetime string. Like the last example i have.

thanks,

XmlNodeList test = x2PathDoc.SelectNodes("//config
                                            /pendingversion
                                              [@versionconfigid > 1002002]");

XmlNodeList test2 = x2PathDoc.SelectNodes("//config
                                             /pendingversion
                                               [@versionconfigid >'1002002']");

XmlNodeList test3 = x2PathDoc.SelectNodes("//config
                                             /pendingversion[@test > 'b']");

XmlNodeList test4 = x2PathDoc.SelectNodes("//config
                                             /pendingversion
                                               [@deploydatetime > 
                                                '2010-12-19T03:25:00-08:00']");

回答1:


In XPath 1.0, comparison operator other than equality comparison, works only for numbers. This is because in XML you are dealing with UNICODE. So, in order to make string a complete ordered data type, you need the notion of collations that it was added in XPath 2.0.

The first expression is obviusly right. Why the second works? Because "greater than" operator cast both arguments with number() function.

From http://www.w3.org/TR/xpath/#booleans

First, comparisons that involve node-sets are defined in terms of comparisons that do not involve node-sets; this is defined uniformly for =, !=, <=, <, >= and >.

And after describing the existencial comparison for node sets (a comparison is true only if there is a node in the node set for wich the comparison is true):

When neither object to be compared is a node-set and the operator is <=, <, >= or >, then the objects are compared by converting both objects to numbers and comparing the numbers according to IEEE 754



来源:https://stackoverflow.com/questions/4466494/string-greater-less-and-equal-comparison-in-xmldocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!