3 Replies Latest reply on Jan 3, 2015 10:01 AM by mlavie

    Does ExtendScript Support E4X QNames?

    mlavie Level 1


      I am in the middle of converting my ActionScript extensions to JavaScript, in anticipation of the move to HTML5 extensions.

       

      I made extensive use of XML Namespaces and QNames in ActionScript, with no problem.

       

      I am now finding that ExtendScript does not fully support QNames. Only the method XML.descendants() seems to support QNames. The XML.elements(), XML.child() and XML.children() do not.

       

      For example, the following code returns 2, as expected:

      function main()

      {

      var qName     =      new QName ('ns','Son');

      var xml           = <Father name="Michael" xmlns="ns"><Son name="John"/><Son name="Tom"/></Father>;

      var sons          = xml.descendants(qName);

      return sons.length();

      }

       

      However both of the following examples return 0. According to the ECMA spec and de facto ActionScript behavior, I should have gotten 2 for these examples, too.

       

      function main()

      {

      var qName= new QName ('ns','Son');

      var xml = <Father name="Michael" xmlns="ns"><Son name="John"/><Son name="Tom"/></Father>;

      var sons = xml.children(qName);

      return sons.length();

      }

       

      function main()

      {

      var qName= new QName ('ns','Son');

      var xml = <Father name="Michael" xmlns="ns"><Son name="John"/><Son name="Tom"/></Father>;

      var sons = xml.elements(qName);

      return sons.length();

      }

       

      This is a major problem for me, as I only want the immediate child nodes of the current XML, and not n generations.

       

      All help much appreciated.

       

      Best regards for a Happy New Year!

      mlavie

        • 1. Re: Does ExtendScript Support E4X QNames?
          Dirk Becker Level 4

          Given its importance, ExtendScript should indeed get more attention.

          As you found, namespaces can be a pain. In some cases I just took the radical approach and completely strip them before any further processing.

          At least ExtendScript knows the basic idea, namespace support within InDesign XML is even worse.

          Expect a few more oddities ...

           

          The following approaches work for me:

          function main()
          {
            var xml = <Father name="Michael" xmlns="ns"><Son name="John"/><Son name="Tom" xmlns="adobted"><Son name="Jerry"/></Son></Father>;
            var ns = new Namespace("ns");
            $.writeln("a:",xml.ns::Son.length());
            default xml namespace = 'ns';
            $.writeln("b:",xml.children("Son").length());
            default xml namespace = '';
          }
          main();
          
          

           

          Happy new year,

          Dirk

          1 person found this helpful
          • 2. Re: Does ExtendScript Support E4X QNames?
            mlavie Level 1

            Hallo Dirk,

             

            Ich danke Ihnen für Ihre Hilfe.

             

            Glückliches neues Jahr!

             

            MfG,

            mlavie

             

             

            • 3. Re: Re: Does ExtendScript Support E4X QNames?
              mlavie Level 1

              Hi Dirk,

               

              Here's something that should not work (according to the E4X spec), but it does in ExtendScript:

               

              function main(){

              var qName= new QName ('Tall','Son');
              var xml = <Father name="Michael" xmlns="Tall"><Son name="John" xmlns="Short"/><Son name="Tom"/></Father>;
              var sons = xml[qName];

              return sons.length();

              }

              main();

               

              This returns the value 1, as expected.

               

              Despite the fact that this appears to be an undocumented, undependable hack, it is important for me because my legacy ActionScript code is full of QNames, and I want as little re-write as possible.

               

               

              Again, thanks!

              mlavie