問題
???
今天在修改程序的一個BUG時,竟然發現selectSingleNode方法不支持contains Xpath函數,感到很奇怪。在網上和MSDN也沒有發現官方言論表明selectSingleNode不支持contains 函數。
當時我的情景大概是這樣的:
1、Xml文檔的結構










2.我的實現。
???
我希望通過代碼找到expressionContext不等于T_SUBTITLE/ICHANNELNUM,并且xsf:suExpression中包含字符串"ICHANNELNUM"的節點。我書寫了以下代碼來實現:





??
這時IE報出錯誤:contains是未實現的方法!。這下我就暈了,明明在Xpath的參考手冊,MSDN上都寫著contains是支持的方法。
原因
?
難道真的不支持?
?查閱MSDN我發現還有以下Xpath函數:
Microsoft XML Core Services (MSXML) 4.0 - XPath Reference |
![]() |
String Functions
concat | Returns the concatenation of the arguments. |
contains | Returns true if the first argument string contains the second argument string; otherwise returns false. |
normalize-space | Returns the argument string with the white space stripped. |
starts-with | Returns true if the first argument string starts with the second argument string; otherwise returns false. |
string | Converts an object to a string. |
string-length | Returns the number of characters in the string. |
substring | Returns the substring of the first argument starting at the position specified in the second argument and the length specified in the third argument. |
substring-after | Returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string. |
substring-before | Returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string. |
translate | Returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. |
??
難道就只有contains不支持嗎?其他函數呢?為了證實我的想法,我嘗試了上述所有函數,IE都報出同樣的錯誤,原來都不支持!
原來只有4.0版本的支持
???
我發現這些Xpath函數的說明在MSXML 4.0 SDK下,難道只有4.0版本支持?于是我把代碼進行了修改:




?????
運行程序,竟然不再拋出contains函數不支持的錯誤了,但是出現另外一個錯誤,xsf是未定義的前綴。對,應該是沒有聲明名稱空間。我對代碼再次進行了修改:




???
運行程序,測試通過!于是我下了這樣一個結論:只有Msxml2.DOMDocument.4.0以上版本才支持這些Xpath函數。但是這個結論對嗎?我自己也不敢確定。
不只Msxml2.DOMDocument 4.0以上版本支持contains等Xpath函數
????
我進一步的查找了一些資料,都沒有信息表明Msxml2.DOMDocument 2.0不支持這些Xpath函數。到底Msxml2.DOMDocument 2.0、Msxml2.DOMDocument 3.0為什么支持類似等于、不等于、大于這樣的操作符,而無法識別contains這樣的函數呢?直到我發現了這個方法:setProperty("SelectionLanguage", "XPath")。這時我恍然大悟,原來4.0以下版本的Msxml2.DOMDocument默認用類似正則匹配的方法來進行節點選取的,而4.0及以上版本默認采用Xpath作為默認的選擇語言。
??? 我采用Msxml2.DOMDocument 2.0,將代碼做以下修改也是可以正常運行的:






?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
