Opera和Firefox之间的XML命名空间和xhr.responseXML的jQuery差异 [英] Difference in jQuery with XML namespace and xhr.responseXML between Opera and Firefox

查看:140
本文介绍了Opera和Firefox之间的XML命名空间和xhr.responseXML的jQuery差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <!DOCTYPE HTML> 
< html>< head>< title> XML-problem< / title>

< script src =jquery-1.3.2.min.jstype =text / javascript>< / script>

< script type =text / javascript>
$ b $(函数(){
$('< p />')。load(text.xml,function(responseText,textStatus,xhr){$ b $ ();
$(this).text(x_txt); b $ var $ x $ = $(xhr.responseXML);
var x_txt = xml.find('atom\\:x' .appendTo('#container');
});
});

< / script>

< / head>< body>< div id =container/>< / body>< / html>

这个脚本应该在加载文档时加载text.xml。 text.xml如下所示:

 < xml xmlns:atom =http://www.w3.org/2005 /原子> 
< atom:x> Text< / atom:x>
< / xml>

当这个文件被加载时, atom的文本内容:x -node被追加到文档中。我可以在我的浏览器窗口中看到文本。



这在Firefox中可以正常工作。不过,除非将查询从'atom \\:x'更改为'x'。在这种情况下,它在Opera,但不是Firefox。



我发现了一种解决方法,即将查询改为'atom\\ :x,x',但是我希望深入到底。

$ hr

<有趣的是,我可以直接内联xml,而不是通过改变XHR的方式来获取。

  var xml = $ (xhr.responseXML); 

转换为

  var xml = $('< xml xmlns:atom =http://www.w3.org/2005/Atom>< atom:x> Text< / atom:x>< XML>'); 

在这种情况下,查询'atom\\:x' 在两个浏览器中都会给出所需的结果,而在'x'>中都不会给这两个浏览器带来任何结果。



事实上,这在Opera中的作用是不同的,这让我得出结论,前者是Opera中的一个bug。这是一个合理的结论吗?我在哪里可以指出这个标准是如何运作的?






结论:


  1. 这个问题的替代解决方法是什么?任何比我找到的更好?

  2. 这是Opera中的错误吗?如果是的话,哪一个标准是这样说的?

希望你能帮忙:)

解决方案

这不是Opera中的一个bug。这是正确的行为


在支持名称空间的客户机中,元素类型选择器(名称空间分隔符之后的部分,如果存在的话)的名称部分将仅匹配元素的合格名称。

在您的案例中,本地名称是 x atom:x is not even XML中的合法本地名称

此外, namespace-前缀类型选择器在CSS中有不同的语法,根本不使用冒号:
$ b $ pre $ @namespace atom url http://www.w3.org/2005/Atom);
atom | x {color:blue}

你的语法似乎依赖于一个怪癖通过命名空间不知道的用户代理中的HTML解析器。
$ b HTML解析器将冒号作为标签名称的一部分,并在缺省名称空间中获得 atom:x 元素,它会匹配 atom \:x 选择器,但是在XML中,您可以在<$ c $中获得 x 元素c> http://www.w3.org/2005/Atom 命名空间。


Consider this:

<!DOCTYPE HTML>
<html><head><title>XML-problem</title>

<script src="jquery-1.3.2.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(function() {
    $('<p/>').load("text.xml", function(responseText, textStatus, xhr) {
        var xml = $(xhr.responseXML);
        var x_txt = xml.find('atom\\:x').text();
        $(this).text(x_txt).appendTo('#container');
    });
});

</script>

</head><body><div id="container" /></body></html>

This script should load text.xml when the document has been loaded. text.xml looks like this:

<xml xmlns:atom="http://www.w3.org/2005/Atom">
    <atom:x>Text</atom:x>
</xml>

When this file has been loaded, the text contents of the atom:x-node are appended to the document. I can see "Text" in my browser window.

This works as expected in Firefox. However, it does not work in Opera unless I change the query from 'atom\\:x' to just 'x'. In this case it works in Opera, but not Firefox.

I have discovered a workaround, namely changing the query to 'atom\\:x, x', but I would rather like to get to the bottom of this.


Now for the funny twist: I can inline the xml directly instead of getting it from XHR by changing

var xml = $(xhr.responseXML);

into

var xml = $('<xml xmlns:atom="http://www.w3.org/2005/Atom"><atom:x>Text</atom:x></xml>');

In this case a query of 'atom\\:x' will give the desired result in both browsers and just 'x' will give no result in both browsers.

The fact that this works differently in Opera leads me to conclude that the former behavior is a bug in Opera. Is this a reasonable conclusion? Where can I point to for the standard that describes how this is supposed to work?


In conclusion:

  1. What are the alternative work-arounds for this problem? Any better than the one I have found?
  2. Is this a bug in Opera? If yes, which standard says so?

Hope you can help :)

解决方案

It's not a bug in Opera. It's the correct behavior:

In a namespace-aware client, the name part of element type selectors (the part after the namespace separator, if it is present) will only match against the local part of the element's qualified name.

In your case local name is x, and atom:x isn't even a legal local name in XML.

Moreover, namespace-prefixed type selector in CSS has different syntax that doesn't use colon at all:

@namespace atom url(http://www.w3.org/2005/Atom);
atom|x { color: blue }

Your syntax seems to rely on a quirk introduced by HTML parsers in namespace-unaware user-agents.

HTML parser "eats" the colon as part of tag name and you get atom:x element in default namespace, which would match atom\:x selector, but in XML that you get x element in http://www.w3.org/2005/Atom namespace.

这篇关于Opera和Firefox之间的XML命名空间和xhr.responseXML的jQuery差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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