收录日期:2019/05/22 09:20:52 时间:2010-01-14 21:17:17 标签:xml,flex,actionscript,e4x

Heres some code for a test case. I don't understand why the first two queries produce a result but the third one doesn't. Any ideas?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  creationComplete="onInit();" >
    <mx:Script>
        <![CDATA[

private function onInit():void{
    var someXML:XML = 
        <libs>
            <library libLevel="System">
                <type typeName="Established Problem" typeID="2">
                    <template templateName="asthma" templateAbbr="asthma-fu" templateInsertDate="05/03/2004" templateID="14"/>
                    <template templateName="gastroesophageal reflux" templateAbbr="GERD" templateInsertDate="05/03/2004" templateID="15"/>
                </type>
            </library>
        </libs>;
        trace("library");
        trace(someXML.library.(@libLevel == "System"));//works as expected
        trace("type");
        trace(someXML.library.type.(@typeID == "2"));//works as expected
        trace("template");
        trace(someXML.library.type.template.(@templateID == "14"));//returns nothing
            }
        ]]>
    </mx:Script>
</mx:Application>

What does work is if I add a dummy node below the one that doesn't query right.

var someXML:XML = 
        <libs>
            <library libLevel="System">
                <type typeName="Established Problem" typeID="2">
                    <template templateName="asthma" templateAbbr="asthma-fu" templateInsertDate="05/03/2004" templateID="14">
                        <blah/>
                    </template>
                    <template templateName="gastroesophageal reflux" templateAbbr="GERD" templateInsertDate="05/03/2004" templateID="15">
                        <blah/>
                    </template>
                </type>
            </library>
        </libs>;

Why does it only work if there is a node below it?

You are getting a result, it's just that XML.toString (called by trace()) returns the content of simple XML nodes, and your single result node has no content. (Actually, your result is a single-node XMLList, so it acts like an XML node.)

To see the difference:

    var x:XMLList = someXML.library.type.template.(@templateID == "14");
    trace(x);                // empty string
    trace(x.toXMLString());  // expected XML node