5 Replies Latest reply on Jan 14, 2017 7:09 AM by Oriup

    Find an xml element by attribute

    gopaljay78 Level 1

      Hi ,

       

      I am writing an javascript to find all <informalfigure> xml element that has no "type" attribute. When I run my script, it display an error message:

      Error: Adobe InDesign cannot processXpath expression ""

       

      my script is:

       

      #target indesign-6.0
      #include "glue code.jsx"
      var myDocument = app.activeDocument;
      var alltags = new Array;
      var i1 = 0;

       

          var myXPath =  "//informalfigure[not(@type)]";
          var myRuleSet = new Array (new ProcessProduct);
          var myDoc = app.activeDocument;   
          with(myDocument) {
              try {
                  var elements = xmlElements;
                  __processRuleSet(elements.item(0), myRuleSet);
                  }
              catch (err) {
                  alert (err);
                  }
              }

       

      alert(alltags.length)

       

      function ProcessProduct()
      {
          this.name = "ProcessProduct";
          this.xpath = myXPath;
          this.apply = function(myElement, myRuleProcessor)  {
              alltags[i1++] = myElement;
              }
          return true;
          }

       


      Can any one tell me where it is wrong.

       

      Thanks,

      Gopal

        • 1. Re: Find an xml element by attribute
          PankChat Level 1

          Aaagh!!! XPath Limitation in ID I guess.

           

          Your XPath statement (I guess) is not supported in ID  "//informalfigure[not(@type)]"

          though you can do //informalfigure[@type]" or //informalfigure", but I can understand that you are interested only in "informalfigure" elements with no "type" attributes.

          • 2. Re: Find an xml element by attribute
            Adobe-InDesign CS4 Level 1

            Dear PankChat

             

            I'm having the doubt about can we use XML NameSpaces in Javascript. Is it possible?

             

            In my XPath Statement is "ce:figure[@id]"

             

            //======================= Start Script =======================//

            #target indesign-6.0
            #include "D:\\program files\\Adobe InDesign CS4\\Scripts\\XML Rules\\glue code.jsx"
            var myDocument = app.activeDocument;
            var alltags = new Array;
            var i1 = 0;

                var myXPath =  "//ce:figure[@id]";
                var myRuleSet = new Array (new ProcessProduct);
                var myDoc = app.activeDocument;
                with(myDocument) {
                    try {
                        var elements = xmlElements;
                        __processRuleSet(elements.item(0), myRuleSet);
                        }
                    catch (err) {
                        alert (err);
                        }
                    }

            alert(alltags.length)

            function ProcessProduct()
            {
                this.name = "ProcessProduct";
                this.xpath = myXPath;
                this.apply = function(myElement, myRuleProcessor)  {
                    alltags[i1++] = myElement;
                    }
                return true;
                }

            //======================= End Script =======================//

             

            OutPut though InDesign :     Error: Adobe Indesign cannot process ^1XPath expression '^2'

            I checked so many ways, but I can't get the solution Please let me know your comments and suggestion...

             

            Regards

            T.R.Harihara Sudhan

            • 3. Re: Find an xml element by attribute
              PankChat Level 1

              Try changing your XPath statement

               

                  var myXPath =  "//ce:figure[@id]";

              to

                  var myXPath =  "//figure[@id]";

               

              Weird, I know but will work I hope. Let me know.

              • 4. Re: Find an xml element by attribute
                Adobe-InDesign CS4 Level 1

                Dear Pankaj,

                 

                Many thanks for this quick mail, I already checked with what you mentioned in this blog. I got the nice answers.

                But my queries and doubt regarding for this XMLNameSpace only. How to use the NameSpace in this particular area.

                Please kindly check and let me know your suggestion / comments.

                 

                Regards,

                T.R.Harihara Sudhan

                • 5. Re: Find an xml element by attribute
                  Oriup Level 1

                  Hi,

                   

                  Can I also find and change attributes?

                   

                  Sumit