Xpath works on “MSXML2.DOMDocument” but not on “MSXML2.DOMDocument60” [duplicate]

佐手、 提交于 2019-12-23 12:27:37

问题


Possible Duplicate:
String greater, less, and equal comparison in XmlDocument

Hi, In VBA I have the folowing expression:

 SourceXml.selectNodes("//Races/Race[/FirstRun[@ActStart>'2011-03-01' or
 @ActEnd<'2011-03-15']]")

If I define the SourceXml as MSXML2.DOMDocument it retrieves a list with the desired nodes. If I define the SourceXml as MSXML2.DOMDocument60 it retrieves a list with 0 elements inside.

Whath is wrong with the Xpath expression?


回答1:


The expression you have provided:

//Races/Race[/FirstRun[@ActStart>'2011-03-01' or  ActEnd<'2011-03-15']]

will not select any node, because in XPath 1.0 there are no > or < comparison operators for strings (only for numbers). The two strings above are first converted to numbers, which yields NaN and any comparison involving NaN is false(). Therefore, the value of the predicate is false() and the expression doesn't select any node.

The fact that using MSXML2.DOMDocument.SelectNodes() selects nodes is because in this early version of MSXML the default selection language is not XPath but something called "XSL" (if I remember well) and it is not the standard, W3C XPath language.

I guess that MSXML6 no longer provides this obsolete dialect.

In your case you might be able to use this XPath expression successfully:

//Races/Race[/FirstRun
              [translate(@ActStart,'-','') > 20110301 
             or
               translate(ActEnd, '-','') < 20110315
              ]
            ]


来源:https://stackoverflow.com/questions/4865476/xpath-works-on-msxml2-domdocument-but-not-on-msxml2-domdocument60

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