Wicked-Good-XPath评估xPathResult包含[Exception:TypeError]

时间:2014-03-03 21:50:47

标签: javascript exception xpath typeerror wicked-good-xpath

我参与了一个从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对象的假设。它被传递给jQuery AJAX调用ASP.NET {ajax处理程序的success处理程序。在检查xdoc对象的属性时,它看起来像是一个有效的XML文档对象。是否有一些exaluate依赖于我应该注意的特定属性?

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

2014年3月3日更新:

确定。这必须是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] };

3 个答案:

答案 0 :(得分:0)

如果数据请求的响应类型是responseXML,那么将在IE9及更低版本(或更新版本中的等效文档模式)中返回MSXML对象,这与DOM Level 3 XPath不兼容。请改用responseText,然后通过XMLDocument将其转换为DOMParser

<强>参考

答案 1 :(得分:0)

已修复此文件的本地名称功能已在Wicked Good XPath中修复,但尚未保证一般的XML支持。正在努力......

答案 2 :(得分:0)

尝试评估文档。

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