1

我参与了一个从 IE8 迁移到 IE9 的项目。我了解到 IE9 及更高版本不再支持 selectNodes() 来执行 xPath 查询。作为回应,我求助于 Google 的 Wicked-Good-XPath,一个原生 JavaScript xpath 库,以访问 Evaluate XPath 函数。

我尝试执行 XPath 查询的 XML 文档如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<SearchResults xmlns="http://tempuri.org/SearchResults.xsd">
   <ADISResults>
      <ADISID>123456</ADISID>
      <Title>...</Title>
      <FN>...</FN>
      <MN />
      <LN>...</LN>
      <Suffix />
      <PreferredState>...</PreferredState>
      <CouncilCity>...</CouncilCity>
      <Lost>...</Lost>
      <Deceased>...</Deceased>
      <CategoryID>...</CategoryID>
      <Category>...</Category>
      <Classification>....</Classification>
      <Affiliation>...</Affiliation>
      <Individual>...</Individual>
      <DisplayName>...</DisplayName>
      <Selected>...</Selected>
      <WGSelected>...</WGSelected>
      <SortName>...</SortName>
      <DMASEntityDesc>...</DMASEntityDesc>
      <DMASSourceDesc>...</DMASSourceDesc>
      <NoSolicit>...</NoSolicit>
      <NoMail>...</NoMail>
      <Region>...</Region>
      <Region1>...</Region1>
   </ADISResults>
   <ResultTotals>
      <MaxResults>...</MaxResults>
      <NumResults>...</NumResults>
   </ResultTotals>
</SearchResults>

由于我想选择任何/所有ADISResults节点,我的 XPath 查询,考虑到命名空间,看起来像//*[local-name()='ADISResults']:该查询似乎不是问题,因为它在XPathTester产生了预期的结果。

我的 JavaScript 调用 Wicked-Good-XPath 及其评估函数如下所示:

wgxpath.install();
var xPathQuery = "//*[local-name()='ADISResults']";
var xpr = xdoc.evaluate(xPathQuery, xdoc, null, XPathResult.ANY_TYPE, null);
var nl = xpr.singleNodeValue;

但是,如果我运行上面的代码并检查 XPathResult,var xpr我会看到[Exception: TypeError]许多变量的属性,包括xpr.singleNodeValue. 最终,我试图将匹配的 xml 节点列表检索到var nl.

我究竟做错了什么?

2014 年 3 月 4 日更新:

在进行了更多测试之后,我认为问题在于我对xdoc我正在使用的对象的假设。它被传递给对successASP.NET ajax 处理程序的 jQuery AJAX 调用的处理程序。在检查xdoc对象的属性时,它看起来像是一个有效的 XML 文档对象。是否有一些exaluate我应该注意的特定属性?

所有其他参数及其类型似乎都符合W3文档,因此将xdoc对象作为唯一的嫌疑人。

2014 年 3 月 4 日更新 2:

好的。这必须是xdoc对象本身内部的一些微妙之处。我已经生成了它的内容转储,希望它可以帮助解决这个问题。

webkitFullscreenElement: null;
webkitFullscreenEnabled: true;
webkitCurrentFullScreenElement: null;
webkitFullScreenKeyboardInputAllowed: false;
webkitIsFullScreen: false;
childElementCount: 1;
lastElementChild: [object Element];
firstElementChild: [object Element];
children: [object HTMLCollection];
onwaiting: null;
onvolumechange: null;
ontimeupdate: null;
onsuspend: null;
onsubmit: null;
onstalled: null;
onshow: null;
onselect: null;
onseeking: null;
onseeked: null;
onscroll: null;
onreset: null;
onratechange: null;
onprogress: null;
onplaying: null;
onplay: null;
onpause: null;
onmousewheel: null;
onmouseup: null;
onmouseover: null;
onmouseout: null;
onmousemove: null;
onmouseleave: null;
onmouseenter: null;
onmousedown: null;
onloadstart: null;
onloadedmetadata: null;
onloadeddata: null;
onload: null;
onkeyup: null;
onkeypress: null;
onkeydown: null;
oninvalid: null;
oninput: null;
onfocus: null;
onerror: null;
onended: null;
onemptied: null;
ondurationchange: null;
ondrop: null;
ondragstart: null;
ondragover: null;
ondragleave: null;
ondragenter: null;
ondragend: null;
ondrag: null;
ondblclick: null;
oncuechange: null;
oncontextmenu: null;
onclose: null;
onclick: null;
onchange: null;
oncanplaythrough: null;
oncanplay: null;
oncancel: null;
onblur: null;
onabort: null;
currentScript: null;
webkitHidden: true;
webkitVisibilityState: hidden;
hidden: true;
visibilityState: hidden;
onwheel: null;
onwebkitpointerlockerror: null;
onwebkitpointerlockchange: null;
onwebkitfullscreenerror: null;
onwebkitfullscreenchange: null;
onselectstart: null;
onselectionchange: null;
onsearch: null;
onreadystatechange: null;
onpaste: null;
oncut: null;
oncopy: null;
onbeforepaste: null;
onbeforecut: null;
onbeforecopy: null;
webkitPointerLockElement: null;
compatMode: CSS1Compat;
selectedStylesheetSet: null;
preferredStylesheetSet: null;
characterSet: null;
readyState: interactive;
defaultCharset: undefined;
charset: undefined;
location: null;
lastModified: 03/04/2014 22:26:49;
anchors: [object HTMLCollection];
forms: [object HTMLCollection];
links: [object HTMLCollection];
applets: [object HTMLCollection];
images: [object HTMLCollection];
head: null;
body: null;
cookie: ;
URL: http://localhost:58748/AJAXHandlers/AdvSrch.ashx;
domain: localhost;
referrer: ;
title: ;
styleSheets: [object StyleSheetList];
defaultView: null;
documentURI: http://localhost:58748/AJAXHandlers/AdvSrch.ashx;
xmlStandalone: false;
xmlVersion: 1.0;
xmlEncoding: null;
inputEncoding: null;
documentElement: [object Element];
implementation: [object DOMImplementation];
doctype: null;
parentElement: null;
textContent: null;
baseURI: http://localhost:58748/AJAXHandlers/AdvSrch.ashx;
localName: null;
prefix: null;
namespaceURI: null;
ownerDocument: null;
nextSibling: null;
previousSibling: null;
lastChild: [object Element];
firstChild: [object Element];
childNodes: [object NodeList];
parentNode: null;
nodeType: 9;
nodeValue: null;
nodeName: #document;
createElement: function createElement() { [native code] };
createDocumentFragment: function createDocumentFragment() { [native code] };
createTextNode: function createTextNode() { [native code] };
createComment: function createComment() { [native code] };
createCDATASection: function createCDATASection() { [native code] };
createProcessingInstruction: function createProcessingInstruction() { [native code] };
createAttribute: function createAttribute() { [native code] };
getElementsByTagName: function getElementsByTagName() { [native code] };
importNode: function importNode() { [native code] };
createElementNS: function createElementNS() { [native code] };
createAttributeNS: function createAttributeNS() { [native code] };
getElementsByTagNameNS: function getElementsByTagNameNS() { [native code] };
getElementById: function getElementById() { [native code] };
createEvent: function createEvent() { [native code] };
createRange: function createRange() { [native code] };
createNodeIterator: function createNodeIterator() { [native code] };
createTreeWalker: function createTreeWalker() { [native code] };
execCommand: function execCommand() { [native code] };
queryCommandEnabled: function queryCommandEnabled() { [native code] };
queryCommandIndeterm: function queryCommandIndeterm() { [native code] };
queryCommandState: function queryCommandState() { [native code] };
queryCommandSupported: function queryCommandSupported() { [native code] };
queryCommandValue: function queryCommandValue() { [native code] };
getElementsByName: function getElementsByName() { [native code] };
elementFromPoint: function elementFromPoint() { [native code] };
caretRangeFromPoint: function caretRangeFromPoint() { [native code] };
getSelection: function getSelection() { [native code] };
getCSSCanvasContext: function getCSSCanvasContext() { [native code] };
getElementsByClassName: function getElementsByClassName() { [native code] };
querySelector: function querySelector() { [native code] };
querySelectorAll: function querySelectorAll() { [native code] };
webkitExitPointerLock: function webkitExitPointerLock() { [native code] };
evaluate: function evaluate() { [native code] };
adoptNode: function adoptNode() { [native code] };
getOverrideStyle: function getOverrideStyle() { [native code] };
registerElement: function registerElement() { [native code] };
webkitCancelFullScreen: function webkitCancelFullScreen() { [native code] };
webkitExitFullscreen: function webkitExitFullscreen() { [native code] };
createExpression: function createExpression() { [native code] };
createNSResolver: function createNSResolver() { [native code] };
insertBefore: function insertBefore() { [native code] };
replaceChild: function replaceChild() { [native code] };
removeChild: function removeChild() { [native code] };
appendChild: function appendChild() { [native code] };
hasChildNodes: function hasChildNodes() { [native code] };
cloneNode: function cloneNode() { [native code] };
normalize: function normalize() { [native code] };
isSupported: function isSupported() { [native code] };
lookupPrefix: function lookupPrefix() { [native code] };
isDefaultNamespace: function isDefaultNamespace() { [native code] };
lookupNamespaceURI: function lookupNamespaceURI() { [native code] };
ELEMENT_NODE: 1;
ATTRIBUTE_NODE: 2;
TEXT_NODE: 3;
CDATA_SECTION_NODE: 4;
ENTITY_REFERENCE_NODE: 5;
ENTITY_NODE: 6;
PROCESSING_INSTRUCTION_NODE: 7;
COMMENT_NODE: 8;
DOCUMENT_NODE: 9;
DOCUMENT_TYPE_NODE: 10;
DOCUMENT_FRAGMENT_NODE: 11;
NOTATION_NODE: 12;
DOCUMENT_POSITION_DISCONNECTED: 1;
DOCUMENT_POSITION_PRECEDING: 2;
DOCUMENT_POSITION_FOLLOWING: 4;
DOCUMENT_POSITION_CONTAINS: 8;
DOCUMENT_POSITION_CONTAINED_BY: 16;
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 32;
isSameNode: function isSameNode() { [native code] };
isEqualNode: function isEqualNode() { [native code] };
compareDocumentPosition: function compareDocumentPosition() { [native code] };
contains: function contains() { [native code] };
addEventListener: function addEventListener() { [native code] };
removeEventListener: function removeEventListener() { [native code] };
dispatchEvent: function dispatchEvent() { [native code] };
4

3 回答 3

0

此本地名称功能已针对 XML 文档进行了修复,已在 Wicked Good XPath 中修复,但尚不能保证一般的 XML 支持。正在努力...

于 2014-07-22T14:15:19.103 回答
0

如果数据请求的响应类型为responseXML,则在 IE9 及以下(或较新版本的等效文档模式)中将返回一个 MSXML 对象,这与 DOM Level 3 XPath 不兼容。改为使用responseText,然后将其转换为XMLDocumentvia DOMParser

参考

于 2014-06-12T00:01:38.130 回答
0

尝试评估文件。

wgxpath.install();
var xPathQuery = "//*[local-name()='ADISResults']";
var xpr = document.evaluate(xPathQuery, xdoc, null, XPathResult.ANY_TYPE, null);
var nl = xpr.singleNodeValue;
于 2014-07-29T12:41:47.817 回答