3 Replies Latest reply on Jan 16, 2008 3:05 PM by TOdd_23523523

    e4x troubles with namespaces

    TOdd_23523523
      Hello All,
      I've been trying to do something that I've considered very basic..and I've been banging my head over it for awhile so I thought I'd throw this out for you gurus...
      I'm trying to select a node from xml where a child has a specific value
      For example:
      var xml:XML =
      <communication xmlns="stuffHERE">
      <folder>
      <id>1</id>
      <folder>
      <id>2</id>
      </folder>
      <folder>
      <id>3</id>
      <folder>
      <id>4</id>
      </folder>
      </folder>
      </folder>

      In the above, I'm using an e4x expression that looks like:
      xml..folder.(id == 3)

      All works well when I don't have a namespace declared.
      When I have a name space declared, I keep getting nothing back.
      I'm using the namespace test="http..."
      use namespace test

      Any ideas on what I'm doing wrong? BTW, I've tried many, many different e4x expressions, none that work.

      Also, any techniques for debugging e4x? I've already included some dynamic e4x expression parsers that I found on the web. But I find that while I can get the expression to work fine dynamically, it still doesn't work.

      Here's my test MXML file, sorry for how ugly it looks in this window., I"m hoping you can just copy this and paste it into a new MXML file for you to look at...:

      [I'm posting the MXML on the next post to this question]
        • 1. e4x troubles with namespaces
          TOdd_23523523 Level 1
          see next msg for code
          • 2. Re: e4x troubles with namespaces
            TOdd_23523523 Level 1
            This code Might work better:
            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="horizontal">
            <mx:Script>
            <![CDATA[
            import mx.logging.targets.TraceTarget;
            import com.theriabook.util.logging.TraceLogOutput;
            private var comm:XML =
            <CommunicationHierarchy xmlns=" http://www.directTestmarketing.com/ws/schemas/communicationHierarchyService">
            <folder>
            <id>13</id>
            <recordType>FOLDER</recordType>
            <iconId>icon id</iconId>
            <label>label</label>
            <shortDescription>short description</shortDescription>
            <description>Folder 21 description</description>
            <externalId>1</externalId>
            <template>
            <id>21</id>
            <recordType>TEMPLATE</recordType>
            <iconId>icon id</iconId>
            <label>label</label>
            <shortDescription>short description</shortDescription>
            <description>Template 22 description</description>
            <externalId>1</externalId>
            </template>
            <folder>
            <id>14</id>
            <recordType>FOLDER</recordType>
            <iconId>icon id</iconId>
            <label>label</label>
            <shortDescription>short description</shortDescription>
            <description>Folder 31 description</description>
            <externalId>1</externalId>
            <link>
            <id>13</id>
            <recordType>TEMPLATE</recordType>
            <iconId>icon id</iconId>
            <label>link label</label>
            <url>link url</url>
            <shortDescription>short description</shortDescription>
            <description>description of link - 14</description>
            </link>
            </folder>
            <folder>
            <id>15</id>
            <recordType>FOLDER</recordType>
            <iconId>icon id</iconId>
            <label>label</label>
            <shortDescription>short description</shortDescription>
            <description>Folder 36 description</description>
            <externalId>1</externalId>
            <link>
            <id>15</id>
            <recordType>TEMPLATE</recordType>
            <iconId>icon id</iconId>
            <label>link label</label>
            <url>link url</url>
            <shortDescription>short description</shortDescription>
            <description>description of link - 16</description>
            </link>
            </folder>
            <link>
            <id>8</id>
            <recordType>TEMPLATE</recordType>
            <iconId>icon id</iconId>
            <label>link label</label>
            <url>link url</url>
            <shortDescription>short description</shortDescription>
            <description>description of link - 9</description>
            </link>
            <link>
            <id>12</id>
            <recordType>TEMPLATE</recordType>
            <iconId>icon id</iconId>
            <label>link label</label>
            <url>link url</url>
            <shortDescription>short description</shortDescription>
            <description>description of link - 13</description>
            </link>
            </folder>
            <template>
            <id>44</id>
            <recordType>TEMPLATE</recordType>
            <iconId>icon id</iconId>
            <label>label</label>
            <shortDescription>short description</shortDescription>
            <description>Template 45 description</description>
            <externalId>1</externalId>
            </template>
            <template>
            <id>45</id>
            <recordType>TEMPLATE</recordType>
            <iconId>icon id</iconId>
            <label>label</label>
            <shortDescription>short description</shortDescription>
            <description>Template 46 description</description>
            <externalId>1</externalId>
            </template>
            </CommunicationHierarchy>

            private namespace directTest = " http://www.directTestmarketing.com/ws/schemas/communicationHierarchyService";
            use namespace directTest;

            public var directTest2:Namespace = new Namespace(" http://www.directTestmarketing.com/ws/schemas/communicationHierarchyService");

            private function onGoClick(e:MouseEvent):void
            {

            use namespace directTest;
            var xml:XML = new XML(inputXML.text);

            // All these filters didn't work:
            // new XMLList(xml..folder.(id ==13));
            // new XMLList(xml..folder.("id" =="13"));
            //
            //
            //
            //
            //
            // These Worked:
            // new XMLList(xml.folder[0])
            // new XMLList(xml..folder.(id == 13));
            // new XMLList(xml.descendants("folder").(id == 13));
            //

            //var filterdXML:XMLList = new XMLList(directTest2::folder.(id == 13));
            var filterdXML:XMLList = new XMLList(xml.descendants("folder").(id == 13));
            outputXML.text = filterdXML.toXMLString();
            }

            ]]>
            </mx:Script>
            <mx:TraceTarget level="0" />
            <mx:TextArea id="inputXML" width="100%" height="100%" text="{comm.toXMLString()}"/>
            <mx:Button id="go" click="onGoClick(event)" />
            <mx:TextArea id="outputXML" width="100%" height="100%" />


            </mx:Application>
            • 3. Re: e4x troubles with namespaces
              TOdd_23523523 Level 1
              It seems that if in the XML I add a prefix to the namespace that things work.

              FOr example, if I make that top node look like:
              <CommunicationHierarchy xmlns:nx=" http://www.directTestmarketing.com/ws/schemas/communicationHierarchyService">
              <folder>
              ...

              Then all works as planned. The question I then have, is if someone could eloquently explain to me why having a default namespace breaks everything...


              Thanks