getAttributeNS的行为是什么? [英] What is the behavior of getAttributeNS?

查看:79
本文介绍了getAttributeNS的行为是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用JavaScript写一个小程序,我想在其中解析以下XML小片段:

I'm writing a little program in JavaScript in which I want to parse the following little XML snippet:

<iq xmlns="jabber:client" other="attributes">
  <query xmlns="jabber:iq:roster">
    <item subscription="both" jid="romeo@example.com"></item>
  </query>
</iq>

由于我不知道,如果元素和属性具有名称空间前缀,我就使用知道名称空间的函数( getElementsByTagNameNS getAttributeNS )。

Because I don't know, if the elements and attributes have namespace prefixes, I'm using the namespace-aware functions (getElementsByTagNameNS, getAttributeNS).

var queryElement = iq.getElementsByTagNameNS('jabber:iq:roster', 'query')[0];
if (queryElement) {
  var itemElements = queryElement.getElementsByTagNameNS('jabber:iq:roster', 'item');
  for (var i = itemElements.length - 1; i >= 0; i--) {
    var itemElement = itemElements[i];

    var jid = itemElement.getAttributeNS('jabber:iq:roster', 'jid');
  };
};

使用此代码,我无法获得属性 jid (我得到一个空字符串),但是当我使用 itemElement.getAttribute('jid')而不是 itemElement.getAttributeNS时('jabber:iq:roster','jid')我得到了预期的结果。

With this code I don't get the value of the attribute jid (I get an empty string), but when I use itemElement.getAttribute('jid') instead of itemElement.getAttributeNS('jabber:iq:roster', 'jid') I'm getting the expected result.

如何在其中编写代码一种了解名称空间的方式?在我对XML的理解中,属性 jid 的命名空间具有命名空间 jabber:iq:roster ,因此该函数 getAttributeNS 应该返回值 romeo@example.com

How can I write the code in a namespace-aware manner? In my understanding of XML, the namespace of the attribute jid has the namespace jabber:iq:roster and therefore the function getAttributeNS should return the value romeo@example.com.

[UPDATE]问题是(或正在)我对名称空间和XML属性的使用的理解,与DOM API无关。因此,我创建了另一个问题: XML命名空间和未前缀属性。也是因为 XML名称空间和属性不幸地没有给我答案。

[UPDATE] The problem was (or is) my understanding of the use of namespaces together with XML attributes and is not related to the DOM API. Therefor I created an other question: XML Namespaces and Unprefixed Attributes. Also because XML namespaces and attributes unfortunately doesn't give me an answer.

[更新]我现在要做的是,首先检查是否有没有名称空间的属性,然后再检查是否有名称空间:

[UPDATE] What I did now, is to first check if there is the attribute without a namespace and then if it is there with a namespace:

var queryElement = iq.getElementsByTagNameNS('jabber:iq:roster', 'query')[0];
if (queryElement) {
  var itemElements = queryElement.getElementsByTagNameNS('jabber:iq:roster', 'item');
  for (var i = itemElements.length - 1; i >= 0; i--) {
    var itemElement = itemElements[i];

    var jid = itemElement.getAttribute('jid') || itemElement.getAttributeNS('jabber:iq:roster', 'jid');

  };
};


推荐答案

重要的是,> 属性只有在您明确为其命名空间之前,才能获得名称空间

The important thing is that attributes don't get the namespace until you explicitly prefix them with it:

默认名称空间声明适用于其范围内的所有未前缀元素名称。默认名称空间声明不能直接应用于属性名称

这不同于从父级继承默认名称空间的元素,除非已定义了自己的名称空间。话虽如此,您的属性没有命名空间,这就是为什么 getAttribute() getAttributeNS()有效的原因

This is unlike elements that do inherit the default namespace from the parent unless have their own defined. With that said, your attributes are not namespaced and that's why getAttribute() works and getAttributeNS() with a namespace value doesn't.

您的源XML需要看起来像这样来命名属性:

Your source XML would need to look something like this to "namespace" the attribute:

<a:query xmlns:a="jabber:iq:roster">
    <a:item a:subscription="both" a:jid="romeo@example.com"></a:item>
</a:query>

有关此主题的更多信息: XML名称空间和属性

Here's some more on the subject: XML namespaces and attributes.

如果您只想使用支持名称空间的方法,则应该(但不确定,可能是特定于实现的)可以为您使用 null 命名空间。尝试 getAttributeNS(null, jid)。如果不是这样,您始终可以使用 hasAttributeNS()来解决,然后回退到 getAttributeNS() getAttribute()

If you want to only use the namespace-aware methods then it should (not sure though, might be implementation specific) work for you with null namespace. Try getAttributeNS(null, "jid"). If it doesn't, you can always work around it with the hasAttributeNS() and only then a fallback to getAttributeNS() or getAttribute().

这篇关于getAttributeNS的行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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