在as3中测试xml属性的存在

mat*_*atb 3 apache-flex actionscript-3

在ActionScript 3中测试XML对象上是否存在属性的最佳方法是什么?

http://martijnvanbeek.net/weblog/40/testing_the_existance_of_an_attribute_in_xml_with_as3.html建议使用测试

   if ( node.@test != node.@nonexistingattribute )
Run Code Online (Sandbox Code Playgroud)

我看到建议使用的评论:

 if ( node.hasOwnProperty('@test')) { // attribute qtest exists }
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下,测试都区分大小写.

XML规范:"XML处理器应该以不区分大小写的方式匹配字符编码名称"所以我假设属性名称也应该使用不区分大小写的比较匹配.

谢谢

wel*_*rat 9

请仔细阅读XML规范中的引用:

XML处理器应以不区分大小写的方式匹配字符编码名称

这是在描述规范的4.3.3章的字符编码的声明,它是指存在于姓名encoding的的值<?xml>的处理指令,如"UTF-8""utf-8".我认为绝对没有理由将其应用于文档中任何其他位置的属性名称和/或元素名称.

事实上,在规范的第2.3节中没有提到这一点,Common Syntactic Constructs,其中指定了名称和名称标记.对特殊字符等有一些限制,但对大写和小写字母绝对没有限制.

为了使您的比较不区分大小写,您必须在Flash中执行此操作:

for each ( var attr:XML in xml.@*) {
   if (attr.name().toString().toLowerCase() == test.toLowerCase()) // attribute present if true
}
Run Code Online (Sandbox Code Playgroud)

更确切地说:

var found:Boolean = false;
for each ( var attr:XML in xml.@*) {
    if (attr.name().toString().toLowerCase() == test.toLowerCase()) {
        found = true;
        break;
    }
}
if (found) // attribute present
else // attribute not present
Run Code Online (Sandbox Code Playgroud)