document.evaluate allways在某些pages \sites上的singleNodeValue中返回null [英] document.evaluate allways returns null in singleNodeValue on some pages\sites

查看:218
本文介绍了document.evaluate allways在某些pages \sites上的singleNodeValue中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

页面: http://h4z.it/View/-20100729_designtea.jpg

要在控制台中执行的代码:
document.evaluate(// img,document,null,9,null).singleNodeValue





document.evaluate(/ /a\",document,null,9,null).singleNodeValue



甚至是



结果(用Chrome和Firefox测试): null



我认为页面覆盖了document.evaluate,显示


document.evaluate

函数evaluate(){[native code] }

删除document.evaluate 没有帮助,那还有什么可以的它是打破 document.evaluate

解决方案

在你的问题中使用了一个xhtml命名空间,可能你也看到了这种情况。

由于您为命名空间解析器参数设置 null ,因此无法找到元素。



MDN使用XPath

lockquote>

注意:XPath定义了一个没有前缀的QNames,只匹配
中的元素为空的名称空间。在XPath中,没有办法将默认的
命名空间应用于常规元素引用(例如,
p [@id ='_ myid'] for xmlns ='
http://www.w3.org/1999/xhtml )。要匹配非空名称空间中的
默认元素,您必须使用
['namespace-uri()='a特定元素。 http://www.w3.org/1999/xhtmlrel =nofollow> http://www.w3.org/1999/xhtml '和name()='p'和
@id ='_ myid'](这种方法适用于可能不知道
命名空间的动态XPath),或者使用前缀名称测试,并创建
a命名空间解析器,将前缀映射到命名空间。 / b>

所以,如果你设置了一个解析器,你可以通过前缀来正确地访问元素:

 函数解析器(前缀){
返回前缀===xhtml? 'http://www.w3.org/1999/xhtml':null;


document.evaluate(// xhtml:a,document,resolver,9,null).singleNodeValue

。评估文档



创建解析器



如果你想得到一个节点而不必知道命名空间,你可以使用 local-name() XPath函数作为表达式的一部分
$ b $ $ $ $ $ $ $ $ $ $ $ document.evaluate(// * [local-name()=' img'],document,null,9,null).singleNodeValue


page: http://h4z.it/View/-20100729_designtea.jpg

code to execute in console: document.evaluate("//img",document,null,9,null).singleNodeValue

or

document.evaluate("//a",document,null,9,null).singleNodeValue

or even

document.evaluate("//html",document,null,9,null).singleNodeValue

result (tested both with Chrome and Firefox): null

I thought that page overrided document.evaluate but it shows

document.evaluate

function evaluate() { [native code] }

and delete document.evaluate does not help, so what else can it be which breaks document.evaluate ?

解决方案

The page you show in your question uses an xhtml namespace and probably so do the other pages that you are seeing this happen on.

Since you are setting null for the namespace resolver argument it cannot find the elements.

MDN Using XPath

Note: XPath defines QNames without a prefix to match only elements in the null namespace. There is no way in XPath to pick up the default namespace as applied to a regular element reference (e.g., p[@id='_myid'] for xmlns='http://www.w3.org/1999/xhtml'). To match default elements in a non-null namespace, you either have to refer to a particular element using a form such as ['namespace-uri()='http://www.w3.org/1999/xhtml' and name()='p' and @id='_myid'] (this approach works well for dynamic XPath's where the namespaces might not be known) or use prefixed name tests, and create a namespace resolver mapping the prefix to the namespace.

So if you setup a resolver than you can access the element correctly by prefixing them:

function resolver(prefix){
   return prefix === "xhtml" ? 'http://www.w3.org/1999/xhtml' : null;
}

document.evaluate("//xhtml:a",document,resolver,9,null).singleNodeValue

.evaluate doc

Creating Resolver

If you want to get a node without having to know the namespace you can use local-name() XPath function as part of the expression

document.evaluate("//*[local-name()='img']",document,null,9,null).singleNodeValue

这篇关于document.evaluate allways在某些pages \sites上的singleNodeValue中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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