代码之家  ›  专栏  ›  技术社区  ›  invertedSpear

为什么这个XML查询不起作用?

  •  1
  • invertedSpear  · 技术社区  · 15 年前

    下面是一些测试用例的代码。我不明白为什么前两个查询会产生结果,但第三个查询不会。有什么想法吗?

    <?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>
    

    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>;
    

    1 回复  |  直到 15 年前
        1
  •  3
  •   Michael Brewer-Davis    15 年前

    你会得到一个结果,只是 XML.toString trace() )返回simple的内容 XML XMLList XML 节点。)

    要了解差异,请执行以下操作:

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