4 Replies Latest reply on Aug 22, 2011 8:00 AM by absqua

    Namespace in XML

    Andreas Jansson Level 2

      Hi,

       

      How should you deal with namespaces in XML? As soon as there is a namespace (and there is in the string I currently get...), I can't manage to read any data out from the XML object I put the string inside.

       

      Here is a samle piece of code I've been trying around with:

       

      var myString = '<ArrayOfInt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/MyTest/MyTest"><int>0</int>  <int>0</int>  <int>0</int></ArrayOfInt>'
      var myStringNoNS = '<ArrayOfInt>  <int>0</int>  <int>0</int>  <int>0</int></ArrayOfInt>'
      
      var myXML = XML(myString);
      var myXMLNoNs = XML(myStringNoNS);
      
      $.writeln(myXML.children().length());
      // 0
      
      $.writeln(myXMLNoNs.children().length());
      // 3
      
      // Trying to remove the namespace part using the built-in method for it:
      var myXML2 = myXML.removeNamespace(myXML.namespace());
      $.writeln(myXML.namespace() == myXML2.namespace() );
      // True... So the Namespace was not removed.
      
      // Writing out namespace(): http://tempuri.org/MyTest/MyTest
      

       

      My question is:

      Primarily: how do I read the contents of the nodes as they exist in myXML

      Or if that is not possible: How do I get rid of the namespace using a built-in function such as removeNamespace.

       

      Of cource I could make some string replacements in the original xml string, or alter the incoming data... but that's not my question. That's more of a last way out.

       

       

      Thanks,

      Andreas

        • 1. Re: Namespace in XML
          absqua Level 4

          See https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X#Handlin g_namespaces

           

          It looks like adding

           

          default xml namespace "http://tempuri.org/MyTest/MyTest"
          

           

          before your myXML declaration will allow you to access its non-namespaced (or default namespaced) children.

           

          I can't say I understand the syntax, but it works.

           

          Jeff

          • 2. Re: Namespace in XML
            Andreas Jansson Level 2

            Great!

             

            (There's just a missing "=" in your example)

             

            default xml namespace = "http://tempuri.org/MyTest/MyTest"
            

             

            Thank you!

            • 3. Re: Namespace in XML
              Andreas Jansson Level 2

              Sometimes it works... but very often the ExtendScript ToolKit "hangs" when I try to run, and sometimes I just get "Execution finished" as result, and nothing was written out in the console window...

              If I press F11 to enter debugging instead of running, the test script (with the default namespace line) becomes inaccessible almost every run, and I have to restart Indesign and / or ESTK. I have restarted my computer several times as well, the last couple of hours. I have also trashed the InDesign preferences, but the problem persists.

               

              Also, the default namespace seems to stick to the InDesign session or something... since removing it and running the same script (in the ESTK by pressing F5) returns the same (correct) result, without the default namespace. And the debugger/InDesign freezes/hangs even with the default xml statement removed, as long as I have tried to run with it before.

               

              Can these problems be repeated, are they known?

               

              These tests I'm doing in CS4 (since the code need to work for CS4).

              • 4. Re: Namespace in XML
                absqua Level 4

                Whoops. Sorry about the missing "=". I can reproduce your problems running the script (on CS4 on the Mac) from the ESTK, but only if I'm running it in a persistent custom engine. I assume you are too, which would explain why the default xml namespace persists across invocations of your script. You may want to get that namespace declaration (and whatever you're doing that depends on it) out of the global namespace if setting it for a whole session is a problem.

                 

                I've run across at least one other bug running scripts involving JavaScript XML in the ESTK, but that one was in CS5. It was similar behavior: a script that would work fine double-clicking it in the Scripts palette would hang indefinitely in the ESTK.