views:

60

answers:

1

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?

+3  A: 

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
Michael Brewer-Davis
argh... I've spent way too long on this today. I knew trace calls the toString() automatically but I didn't realize toString switched to content when dealing with simple nodes.Thanks
invertedSpear