Does the selectsinglenode method of xmldocument always support XPath functions?

Source: Internet
Author: User
Tags xpath functions

Problem
Modify todayProgramThe selectsinglenode method does not support the contains XPath function. No official comments have been found on the internet and msdn, indicating that selectsinglenode does not support the contains function.
At that time, my scenario was like this:
1. XML document structure

<? XML version = "1.0" ?>  
< Xsf: xdocumentclass Xmlns: xsf = "Http://schemas.microsoft.com/office/infopath/2003/solutionDefinition" >
< Xsf: errorcondition Expressioncontext = "T_subtitle/ichannelnum" >
< Xsf: subexpression > Xpath_getsiglenodevalue ("t_subtitle/ichannelnum", ".../itimesusage ") < 1 </Xsf: subexpression >  
</ Xsf: errorcondition >
- < Xsf: errorcondition Expressioncontext = "T_descauthorize/itimesusage" >
< Xsf: subexpression > Xpath_getsiglenodevalue ("t_descauthorize/itimesusage", "../ichannelnum ") < 1
</Xsf: subexpression >  
</ Xsf: errorcondition >
</ Xsf: xdocumentclass >

2. My implementation.
I wantCodeFind the node where expressioncontext is not equal to t_subtitle/ichannelnum and xsf: suexpression contains the string "ichannelnum. I wrote the following code for implementation:

// Aobjview_xsf is the document object that stores the preceding XML
VaR Aobjview_xsf = New Activexobject ('msxml2. domdocument ');
Aobjview_xsf.load ( " Test. xml " );
VaR Objerrorconditions = Aobjview_xsf.selectsinglenode ( " Xsf: xdocumentclass/xsf: customvalidation/xsf: errorcondition [@ expressioncontext! =' " + Strbinding +   " 'And contains (xsf: subexpression ,' " + Fieldname + " ')] " );

In this case, ie reports an error: Contains is an unimplemented method !. Now I am dizzy. Clearly in the XPath reference manual, msdn says contains is a supported method.

Cause

 Is it really not supported?
After reading msdn, I found the following XPath functions:

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- 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.

Is only contains not supported? What about other functions? In order to confirm my idea, I tried all the above functions and IE reported the same error, which was not supported in the past!

Originally, only version 4.0 was supported.
I found that the description of these XPATH functions is only supported by version 4.0 in the MSXML 4.0 SDK? So I modified the Code:

// Aobjview_xsf is the document object that stores the preceding XML
VaR Aobjview_xsf = New Activexobject ('msxml2. domdocument. 4.0 ');
Aobjview_xsf.load ( " Test. xml " );
VaR Objerrorconditions = Aobjview_xsf.selectsinglenode ( " Xsf: xdocumentclass/xsf: customvalidation/xsf: errorcondition [@ expressioncontext! =' " + Strbinding +   " 'And contains (xsf: subexpression ,' " + Fieldname + " ')] " );

When you run the program, no errors not supported by the contains function are thrown, but xsf is an undefined prefix. Yes. The namespace is not declared. I modified the code again:

VaR Aobjview_xsf = New Activexobject ('msxml2. domdocument. 4.0 ');
Aobjview_xsf.load ( " Test. xml " );
Aobjview_xsf.setproperty ( " Selectionnamespaces " , " Xmlns: xsf = 'HTTP: // schemas.microsoft.com/office/infopath/2003/solutiondefinition' " );
VaR Objerrorconditions = Aobjview_xsf.selectsinglenode ( " Xsf: xdocumentclass/xsf: customvalidation/xsf: errorcondition [@ expressioncontext! =' " + Strbinding +   " 'And contains (xsf: subexpression ,' " + Fieldname + " ')] " );

Run the program and pass the test! So I came to the conclusion that only msxml2.domdocument. 4.0 or later versions support these XPATH functions. But is this Conclusion Correct? I am not sure.

Not only does msxml2.domdocument 4.0 or later support XPath functions such as contains

I further searched for some information, indicating that msxml2.domdocument 2.0 does not support these XPATH functions. In the end, why does msxml2.domdocument 2.0 and msxml2.domdocument 3.0 support operators like equals, not equals, and greater than, but cannot recognize functions like contains? Until I find this method: setproperty ("selectionlanguage", "XPath "). At this time, I suddenly realized that msxml2.domdocument of versions earlier than 4.0 uses a method similar to regular expression matching by default for node selection, while version 4.0 and later uses XPath as the default selection language by default.
I use msxml2.domdocument 2.0 and make the following changes to the Code to run properly:

// Aobjview_xsf is the document object that stores the preceding XML
VaR Aobjview_xsf = New Activexobject ('msxml2. domdocument ');
Aobjview_xsf.setproperty ( " Selectionlanguage " , " Xpath " );
Aobjview_xsf.setproperty ( " Selectionnamespaces " , " Xmlns: xsf = 'HTTP: // schemas.microsoft.com/office/infopath/2003/solutiondefinition' " );
Aobjview_xsf.load ( " Test. xml " );
VaR Objerrorconditions = Aobjview_xsf.selectsinglenode ( " Xsf: xdocumentclass/xsf: customvalidation/xsf: errorcondition [@ expressioncontext! =' " + Strbinding +   " 'And contains (xsf: subexpression ,' " + Fieldname + " ')] " );

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.