Search code examples
xmlactionscript-3xmllint

find XML node based on element value when multiples may be present


var xmlData:XML = XML(<data>
    <item>
        <type atr="a">AAA</type>
        <type atr="b">BBB</type>
    </item>
    <item>
        <type atr="c">CCC</type>
    </item>
</data>);


trace(xmlData.item.(type=='AAA')); // does not work
trace(xmlData.item.(type=='CCC')); // works
trace(xmlData.item.type.(@atr=='a').parent()); // works
trace(xmlData.item.type.(@atr=='c').parent()); // works

It seems that I cannot get a node based on its value when siblings are present, unless I use attributes.

Is there a way to retrieve item based on the value when there is an unknown amount of elements, without looping manually or using attributes?


Solution

  • You could use XMLList/contains:

    trace(xmlData.item.(type.contains(<type atr="a">AAA</type>)))
    // or
    trace(xmlData.item.(type.contains("AAA")))