document.evaluate allways在某些pages \sites上的singleNodeValue中返回null [英] document.evaluate allways returns null in singleNodeValue on some pages\sites
问题描述
页面: 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 注意:XPath定义了一个没有前缀的QNames,只匹配 所以,如果你设置了一个解析器,你可以通过前缀来正确地访问元素: 如果你想得到一个节点而不必知道命名空间,你可以使用 page: http://h4z.it/View/-20100729_designtea.jpg code to execute in console:
or or even result (tested both with Chrome and Firefox): I thought that page overrided document.evaluate but it shows document.evaluate function evaluate() { [native code] } and 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 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: If you want to get a node without having to know the namespace you can use
这篇关于document.evaluate allways在某些pages \sites上的singleNodeValue中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
lockquote>
中的元素为空的名称空间。在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
document.evaluate("//img",document,null,9,null).singleNodeValue
document.evaluate("//a",document,null,9,null).singleNodeValue
document.evaluate("//html",document,null,9,null).singleNodeValue
null
delete document.evaluate
does not help, so what else can it be which breaks document.evaluate
?null
for the namespace resolver argument it cannot find the elements.
function resolver(prefix){
return prefix === "xhtml" ? 'http://www.w3.org/1999/xhtml' : null;
}
document.evaluate("//xhtml:a",document,resolver,9,null).singleNodeValue
local-name()
XPath function as part of the expressiondocument.evaluate("//*[local-name()='img']",document,null,9,null).singleNodeValue