selectSingleNode/ selectNodes in MSXml 4.0

selectSingleNode/ selectNodes in MSXml 4.0

原来的一段程序,自从把MSXml的版本换成4.0之后(Msxml2.DOMDocument.4.0),发现selectSingleNode 和selectNodes的返回结果发生了变化。变化发生在对Yahoo image的搜索结果的处理上,与这段程序中处理的其他xml文件不同,Yahoo的这个返回结果,有Namespace的存在。

查了资料,得到的这个问题的原因:

1. MSXml4.0 在调用selectSingleNode 和 selectNodes的时候,使用的是XPath而不是3.0中默认的XSL Patterns

2. XPath中不支持默认命名空间(default namespace)的概念。所以要设置有前缀的namespace。

3. 在selectSingle/ selectNodes的参数中(XPath)中要使用namespace。

4. MSXml3.0 这两个函数如果要使用XPath,要在SelectionLanguage属性中设置。

XML 代码贴在最后了。
原来的代码:

dom.selectNodes(’//Result’)

用了4.0之后:

dom.setProperty(”SelectionNamespaces”,’xmlns:yh=”urn:yahoo:srchmi”‘ )
dom.selectNodes(’//yh:Result’)

References

<google groups >
When you were using .selectSingleNode (and for that matter, selectNodes) you were actually using “XSL Pattern” selections rather than XPath. This is because MSXML3 DOM supported and defaulted to
using “XSL Pattern”.
<What’s New in MSXML 4.0 >
<MSDN: SelectionLanguage Property >
(SelectionLanguage property) Used in MSXML 3.0 to specify whether the DOM object should use XPath language (”XPath”) or the old XSLPattern language (default) as the query language.

In MSXML 3.0 only, the default for this property is “XSLPattern”; however, you can set this property to “XPath” to switch the query language from XSLPattern to XPath. Once the property is set, there is no way to switch back without creating an instance of a new DOM object.

For MSXML 4.0 and later, only a value of “XPath” is recognized as XPath is the only query language supported and therefore, this property can be ignored.

<google groups >
XPath
does not support the concept of a default namespace.
You MUST provide a binding to a prefix which idnetifes the namespace to which your node(set) belongs.

XML source

<?xml version=”1.0″ encoding=”UTF-8″?>
<ResultSet xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”urn:yahoo:srchmi”
xsi:schemaLocation=”urn:yahoo:srchmi http://api.search.yahoo.com/ImageSearchService/V1/ImageSearchResponse.xsd” totalResultsAvailable=”4″ totalResultsReturned=”4″ firstResultPosition=”1″>
<Result><Title>institutmitlogo.jpg</Title><Summary /><Url>http://elib.uni-stuttgart.de/img/institutmitlogo.jpg</Url><ClickUrl>http://elib.uni-stuttgart.de/img/institutmitlogo.jpg</ClickUrl><RefererUrl>http://elib.uni-stuttgart.de/opus/volltexte/2004/1704</RefererUrl><FileSize>18051</FileSize><FileFormat>jpeg</FileFormat><Height>142</Height><Width>142</Width>
<Thumbnail><Url>http://mud.mm-a6.yimg.com/image/2140405770</Url><Height>125</Height><Width>125</Width></Thumbnail>
</Result>

<Result><Title>freebsd-powered.gif</Title><Summary /><Url>http://www.beetfoundation.com/images/freebsd-powered.gif</Url><ClickUrl>http://www.beetfoundation.com/images/freebsd-powered.gif</ClickUrl><RefererUrl>http://www.beetfoundation.com/words/h/hyperoxide.html</RefererUrl><FileSize>999</FileSize><FileFormat>gif</FileFormat><Height>31</Height><Width>88</Width>
<Thumbnail><Url>http://mud.mm-a4.yimg.com/image/1136824734</Url><Height>31</Height><Width>88</Width></Thumbnail>
</Result>
</ResultSet>
<!– ws01.search.scd.yahoo.com compressed/chunked Sat May 13 07:31:42 PDT 2006 –>

No comments yet. Be the first.

Leave a reply

Random posts

  • SPAM site hosted by Google Docs
  • 研发公司扁平化管理的一个坏处
  • Python中的and和or
  • OLAP, Analysis Service不错的视频介绍
  • 现在用的主机