何时必须将 XPath 属性设置为 XML DOM 对象? [英] When does XPath property have to be set to XML DOM object?

查看:19
本文介绍了何时必须将 XPath 属性设置为 XML DOM 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

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

objXML.setProperty "SelectionLanguage", "XPath" 被注释时,第一个 xpath 表达式(x 对象)返回有效但第二个 xpath 表达式(y 对象)引发错误:

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]

如果我取消注释 objXML.setProperty "SelectionLanguage", "XPath" 两个表达式都有效.

If I uncomment objXML.setProperty "SelectionLanguage", "XPath" both expressions work.

我的问题是什么时候必须显式设置 XPath 属性,或者在不设置此属性的情况下执行什么样的表达式?

My question is when XPath property has to be explicitly set, or what kind of expressions are executed without setting this property?

推荐答案

对于旧版本的 MSXML,默认语言不是 XPath.
您已经使用旧的与版​​本无关的 ProgID"创建了 DomDocument 实例.Microsoft.XMLDOM 对应于 MSXML 3.0(如果有)作为支持独立 ProgID 的 MSXML 的最后一个版本.
您可以像这样确定默认选择语言:

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")

必须返回XSLPattern,选择语言不支持像position()这样的方法.
XPath 是 MSXML 4.0 及更高版本的默认选择语言,因此您有两种正确使用 XPath 的选择.

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.

  1. 使用旧版本将选择语言指定为 XPath.
  2. 使用较新(不那么旧?)的版本而不指定任何选择语言

来自古代文章 闻起来像我的少年次描述 XSL 模式和 XPath 之间的区别.

From an ancient article that smells like my teenage times describing the difference between XSL Patterns and XPath.

MSXML 2.0 支持 XSL 模式,它是 XPath 的前身1.0.XML 寻址语言的概念被引入到最初的 W3C XSL 工作草案中(http://www.w3.org/TR/1998/WD-xsl-19981216.html) 并称为 XSL图案.MSXML 2.0 实现了描述的 XSL 模式语言在原始 XSL 规范中,有一些小例外.

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.

这篇关于何时必须将 XPath 属性设置为 XML DOM 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆