对于XML结果,Chrome不正确的jQuery find() [英] Chrome incorrect jQuery find() on XML result
问题描述
我发现只有Chrome并没有给出我期望在XML上使用jQuery find(selector)
的结果(来自 $ .parseXML()
)。
考虑以下简单的代码( https://jsfiddle.net/a504caa0 ):
<!DOCTYPE html>
< html>
< head>
< script src =https://code.jquery.com/jquery-3.2.1.js>< / script>
< / head>
< body>
< script>
$(function()
{
var xml =
'<?xml version =1.0?>'+
'< DataSet>' +
'< xs:schema xmlns:xs =http://www.w3.org/2001/XMLSchema>'+
'< xs:element>'+
'< / xs:element>'+
'< / xs:schema>'+
'< / DataSet>'
;
var xmlDoc = $。 parseXML(xml)
var $ docElm = $(xmlDoc.documentElement);
alert(> find('xs\\\:schema')。length =+ $ docElm.find( '> xs \\:schema')。length);
alert(find('xs\\:schema')。length =+ $ docElm.find('xs\\\ ('> xs \\:element')。length =+:schema $ docElm.find('> xs\\:schema')。find('> xs\\:element')。length);
alert(> find('xs\\ \\':schema')。find('xs \\:element')。length =+ $ docElm.find('> ; xs\\:schema')。find('xs\\:element')。length);
});
< / script>
< / body>
< / html>
在IE 11或Firefox中执行此操作会为四个查询中的每一个报告1元素匹配,是我期望的。
但是,在Chrome(57.0.2987.133)中查询1& 3返回1个元素,但查询2& 4返回0个元素。这意味着:
$ xml.find('> selector')
正在找到一个匹配项,而
$ xml .find('selector')
找不到匹配项。换句话说,如果查询一个直接孩子,它会发现一个元素,但如果查询任何后代,它说它不在那里!
我不知道这是否是一个仅限XML的问题,或者是否需要使用 xs \\:
命名空间是一个问题。
在现实生活中,XML越来越深,有时我需要查询任何后代,而不仅仅是直接的孩子。解决方案不能仅仅通过改变上面的查询(例如,将多个 find()
s合并为一个或使用 children()
),它必须在 find()
中没有前置>
的情况下一般工作/解释Chrome行为。
编辑:我刚刚尝试从XML和<$ c中删除所有 xs:
$ c> xs\\:来自 find()
,果然它现在在Chrome下返回1个元素。因此,它看起来像是 find()
问题中的XML名称空间。当然,我不能在现实生活中这样做。看起来Chrome可以找到带有名称空间前缀的直接子项,但是当它涉及泛型下降时,则无法找到它?是否有可接受的解决方法?
我在 https://bugs.chromium.org/p/chromium/issues/detail?id=738372 。 p>
根据回复 https://bugs.chromium.org/p/chromium/issues/detail?id=738372#c10 ,这个错误在Chrome#60中得到修复。 Chrome现在可以正确地使用名称空间搜索XML,并提供与Firefox / IE 11相同的结果。
I am finding that Chrome, only, is not giving me the result I expect using a jQuery find(selector)
on XML (from $.parseXML()
).
Consider the following much simplified code (https://jsfiddle.net/a504caa0/):
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>
<script>
$(function ()
{
var xml =
'<?xml version="1.0"?>' +
'<DataSet>' +
' <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' +
' <xs:element>' +
' </xs:element>' +
' </xs:schema>' +
'</DataSet>'
;
var xmlDoc = $.parseXML(xml)
var $docElm = $(xmlDoc.documentElement);
alert("> find('xs\\:schema').length=" + $docElm.find('> xs\\:schema').length);
alert("find('xs\\:schema').length=" + $docElm.find('xs\\:schema').length);
alert("> find('xs\\:schema').find('> xs\\:element').length=" + $docElm.find('> xs\\:schema').find('> xs\\:element').length);
alert("> find('xs\\:schema').find('xs\\:element').length=" + $docElm.find('> xs\\:schema').find('xs\\:element').length);
});
</script>
</body>
</html>
Running this in IE 11 or Firefox reports a 1-element match for each of the four queries, which is what I expect.
However, in Chrome (57.0.2987.133) queries 1 & 3 return 1 element but queries 2 & 4 return 0 elements. This means that:
$xml.find('> selector')
is finding a match, while
$xml.find('selector')
is not finding a match. In other words, if queried for a direct child it finds an element, but if queried for any descendant it says it's not there!
I do not know whether this is an XML-only issue, or whether the need to use the xs\\:
namespace is a problem.
In real life the XML is bigger and deeper, and there are times I need to query for any descendant, not just direct children. The solution must not just work by changing the above queries (e.g. conglomerating multiple find()
s into one or using children()
), it must work/explain the Chrome behaviour generically on find()
without a leading >
.
EDIT: I just tried removing all xs:
from the XML and xs\\:
from the find()
, and sure enough it now returns 1 element under Chrome. So, it looks like this is an XML-namespace in find()
issue. Of course, I cannot do that in real life. It seems Chrome can find direct children with namespace prefix but not when it involves a generic descent? Is there an acceptable workaround?
I reported this bug for chromium at https://bugs.chromium.org/p/chromium/issues/detail?id=738372.
As per response https://bugs.chromium.org/p/chromium/issues/detail?id=738372#c10 there, this bug became fixed in Chrome #60. Chrome now correctly searches XML with namespaces, delivering the same results as Firefox/IE 11.
这篇关于对于XML结果,Chrome不正确的jQuery find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!