简体   繁体   English

在as3中测试xml属性的存在

[英]Test existence of xml attribute in as3

What is the best method to test the existence of an attribute on an XML object in ActionScript 3 ? 在ActionScript 3中测试XML对象上是否存在属性的最佳方法是什么?

http://martijnvanbeek.net/weblog/40/testing_the_existance_of_an_attribute_in_xml_with_as3.html is suggesting to test using http://martijnvanbeek.net/weblog/40/testing_the_existance_of_an_attribute_in_xml_with_as3.html建议使用测试

   if ( node.@test != node.@nonexistingattribute )

and I saw comments suggesting to use: 我看到建议使用的评论:

 if ( node.hasOwnProperty('@test')) { // attribute qtest exists }

But in both case, tests are case sensitive. 但在这两种情况下,测试都区分大小写。

From the XML Specs : "XML processors should match character encoding names in a case-insensitive way" so I presume attribute name should also be match using a case-insensitive comparison. XML规范 :“XML处理器应该以不区分大小写的方式匹配字符编码名称”所以我假设属性名称也应该使用不区分大小写的比较匹配。

Thank you 谢谢

Please re-read your quote from the XML specs carefully: 请仔细阅读XML规范中的引用:

XML processors should match character encoding names in a case-insensitive way XML处理器应以不区分大小写的方式匹配字符编码名称

This is in chapter 4.3.3 of the specs describing character encoding declarations , and it refers only to the names present in the encoding value of the <?xml> processing instruction, such as "UTF-8" or "utf-8" . 这是在描述字符编码声明的规范的第4.3.3章中,它涉及<?xml>处理指令的encoding值中存在的名称,例如"UTF-8""utf-8" I see absolutely no reason for this to apply to attribute names and/or element names anywhere else in the document. 我认为绝对没有理由将其应用于文档中任何其他位置的属性名称和/或元素名称。

In fact, there is no mention of this in section 2.3 of the specs, Common Syntactic Constructs , where names and name tokens are specified. 事实上,在规范的第2.3节中没有提到这一点, Common Syntactic Constructs ,其中指定了名称和名称标记。 There are a few restrictions on special characters and such, but there is absolutely no restriction on upper and lower case letters . 对特殊字符等有一些限制,但对大写和小写字母绝对没有限制

To make your comparison case-insensitive, you will have to do it in Flash: 为了使您的比较不区分大小写,您必须在Flash中执行此操作:

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

or rather: 更确切地说:

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

How about using XML's contains() method or XMLList's length() method ? 如何使用XML的contains()方法或XMLList的length()方法?

eg 例如

var xml:XML = <root><child id="0" /><child /></root>;

trace(xml.children().@id.length());//test if any children have the id attribute
trace(xml.child[1].@id.length());//test if the second node has the id attribute
trace(xml.contains(<nephew />));//test for inexistend node using contains()
trace(xml.children().nephew.length());//test for inexistend node using legth()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM