For example:
Set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = False
objXML.validateOnParse = False
objXML.resolveExternals = False
objXML.load("http://www.w3schools.com/dom/books.xml")
'objXML.setProperty "SelectionLanguage", "XPath"
For Each x In objXML.selectNodes("//book[@category='cooking' and @category='children']")
WScript.Echo x.text
Next
For Each y In objXML.selectNodes("//book[position()<3]")
WScript.Echo y.text
Next
When objXML.setProperty "SelectionLanguage", "XPath"
is commented then first xpath expression (x object) is returned valid but second xpath expression (y object) raises error:
msxml3.dll (14, 1) : Unknown method.
//book[-->position()<--<3]
If I uncomment objXML.setProperty "SelectionLanguage", "XPath"
both expressions work.
My question is when XPath property has to be explicitly set, or what kind of expressions are executed without setting this property?
Default language is not XPath for older versions of MSXML.
You've created DomDocument instance using an old, "version independent ProgID". Microsoft.XMLDOM
corresponds MSXML 3.0 (if you have) as the last version of MSXML which supported independent ProgIDs.
You can determine default selection language like this :
WScript.Echo objXML.getProperty("SelectionLanguage")
Must be return XSLPattern
which a selection language does not supports methods like position()
. XPath
is default selection language for MSXML 4.0 and later, so you have two choices using XPath properly.
- Using older versions specifying selection language as XPath.
- Using newer (less older?) versions without specifyng any selection language
From an ancient article that smells like my teenage times describing the difference between XSL Patterns and XPath.
MSXML 2.0 provides support for XSL Patterns, the precursor to XPath 1.0. The notion of an XML addressing language was introduced into the original W3C XSL Working Drafts (http://www.w3.org/TR/1998/WD-xsl-19981216.html) and called XSL Patterns. MSXML 2.0 implements the XSL Patterns language as described in the original XSL specification with a few minor exceptions.
So, I think you were on minor (!) exceptions.
来源:https://stackoverflow.com/questions/11462566/when-does-xpath-property-have-to-be-set-to-xml-dom-object