[英]Test existence of xml attribute in as3
在ActionScript 3中测试XML对象上是否存在属性的最佳方法是什么?
http://martijnvanbeek.net/weblog/40/testing_the_existance_of_an_attribute_in_xml_with_as3.html建议使用测试
if ( node.@test != node.@nonexistingattribute )
我看到建议使用的评论:
if ( node.hasOwnProperty('@test')) { // attribute qtest exists }
但在这两种情况下,测试都区分大小写。
从XML规范 :“XML处理器应该以不区分大小写的方式匹配字符编码名称”所以我假设属性名称也应该使用不区分大小写的比较匹配。
谢谢
请仔细阅读XML规范中的引用:
XML处理器应以不区分大小写的方式匹配字符编码名称
这是在描述字符编码声明的规范的第4.3.3章中,它仅涉及<?xml>
处理指令的encoding
值中存在的名称,例如"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
}
更确切地说:
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
如何使用XML的contains()方法或XMLList的length()方法?
例如
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.