[英]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.