【问题标题】:How do I filter by attribute and value across multiple elements?如何跨多个元素按属性和值进行过滤?
【发布时间】:2015-07-16 22:03:09
【问题描述】:

我试图通过检查它的类型来找到一个 XML 节点(类型是一个属性)。我可以使用以下代码成功地做到这一点:

var xml:XML = describeType(this);
var classname:String = getQualifiedClassName(mainGroup);
var xmlList:XMLList = xml.accessor.(@type==classname);
if (xmlList.length()) {
    trace("Found:" + xmlList[0]);
}

但我不知道它是否总是一个“访问器”元素。它可能是一个“变量”元素,也可能是其他东西。因此,我想搜索 XML 中所有具有 type 属性的元素,该属性的值与我要查找的值相匹配。当我使用以下内容时,它说它无效:

// 1084: Syntax error: expecting doublecolon before semicolon.
var xmlList:XMLList = xml..(@type==classname);

获取我要查找的信息的正确语法是什么?

这里有一些用于测试的 XML:

<type name="MyApplication" base="spark.components::WindowedApplication" isDynamic="false" isFinal="false" isStatic="false">
    <accessor name="explicitMinWidth" access="readwrite" type="Number" declaredBy="mx.core::UIComponent"/>

    <accessor name="mainGroup" access="readwrite" type="components::MainGroup" declaredBy="MyApplication">

    </accessor>

    <variable name="controlBarGroup" type="spark.components::Group">

    </variable>
</type>

【问题讨论】:

    标签: xml actionscript-3 e4x


    【解决方案1】:

    像这样使用* 怎么样:

    var xml:XML = <type name="MyApplication" base="spark.components::WindowedApplication" isDynamic="false" isFinal="false" isStatic="false">
        <accessor name="explicitMinWidth" access="readwrite" type="Number" declaredBy="mx.core::UIComponent" />
        <accessor name="mainGroup" access="readwrite" type="components::MainGroup" declaredBy="MyApplication" />
        <variable name="controlBarGroup" type="spark.components::Group" />
        <variable name="explicitMinWidth" access="readwrite" type="Number" declaredBy="mx.core::UIComponent" />
    </type>;
    
    var xml_list:XMLList = xml.*.(@type == 'Number');
    trace(xml_list.length());   // gives : 2
    

    编辑:

    为避免错误Error #1065: Variable type is not defined 和删除不包含type 属性的行,您可以使用:

    var xml_list:XMLList = xml.*.(attribute('type') == 'Number');
    trace(xml_list.length());   // gives : 2
    

    希望能有所帮助。

    【讨论】:

    • @1.21gigawatts 你的意思是你的xml数据不包含“类型”属性吗?
    • @1.21gigawatts 是的,当我看到你的评论时,我只是在编辑我的答案;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2012-07-05
    • 1970-01-01
    • 2023-04-07
    • 2016-11-02
    • 2020-04-18
    相关资源
    最近更新 更多