11 Replies Latest reply on Oct 11, 2013 8:10 AM by hirsute

    [CS5.5:JS] - evaluateXPathExpression not working - Reg.

    Adobe-InDesign CS4 Level 1

      Dear All,

       

      I'm working in the below code, but It will return "zero" only.

       

       

      //================== Code====================//

      var mydoc = app.activeDocument;

      var rootElement = mydoc.xmlElements.item(0);

      var linkElementList = rootElement.evaluateXPathExpression("//chapter/sections/section");

      $.writeln(linkElementList.length);

       

      //==================== End : Code====================//

       

      I'm collection all "section" nodes, but it throws "zero" [undefind]...

       

       

      Does anyone know the proper syntax to use with this?, Please kindly help and advance thanks...

       

       

      Thanks & Regards

      T.R.Harihara SudhaN

        • 1. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
          csm_phil Level 4

          Hi Hari,

           

          Please try the below modified your JS code. Its working fine.

           

           

          var mydoc = app.activeDocument;
          var rootElement = mydoc.xmlElements.item('chapter');
          var linkElementList = rootElement.evaluateXPathExpression("//sections/section");
          $.writeln(linkElementList.length);
          

           

          thx,

          csm_phil

          • 2. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
            Pooja_IDDev Level 1

            Hi csm_phil,

             

                 Above code is not working in my case it is still returning "zero" only. Is there any other way to use evaluateXPathExpression..

             

            Please help

            thanks in advance.

            Pooja

            • 3. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
              csm_phil Level 4

              Hi Pooja,

               

              My above its working on InDesign CS5.5 version. May I know your InDesign version. Also, can you post the your document snapshot if you like.

               

              I will try and let you know once you posted the snapshot of your document or your script.

               

              thx,
              csm_phil

              • 4. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                Pooja_IDDev Level 1

                Hi csm

                 

                            I am using CS5.5 version only. I need to find the all "ref" nodes , which are under root node.

                 

                                    var rootElement = doc.xmlElements.item('root');    

                                    var items = rootElement.xmlElements.item('/root//ref');

                 

                but i am still getting zero length.

                 

                Thanks in advance.

                Pooja

                • 5. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                  hirsute

                  Hi,

                   

                  I also have this problem with CS6. Here is my code:

                   

                  var uia = app.scriptPreferences.userInteractionLevel;

                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;

                  var doc = app.activeDocument;

                  var root = doc.xmlElements[0];

                  $.writeln(root); // [object XMLElement]

                  $.writeln(root.markupTag.name); // document

                   

                  var endnotes = root.evaluateXPathExpression("/endnote"); //return

                  $.writeln(endnotes); // nothing

                   

                  var endnotes1 = root.evaluateXPathExpression("//endnote");

                  $.writeln(endnotes1); // nothing

                   

                  var endnotes2 = root.evaluateXPathExpression("endnote");

                  $.writeln(endnotes2); // nothing

                   

                  var self = root.evaluateXPathExpression(".");

                  $.writeln(self); // nothing

                  app.scriptPreferences.userInteractionLevel =  uia;

                   

                   

                   

                  The evaluateXPathExpression does not even find the self:node() ... I different elements in structure and it never worked.

                  Somebody have an idea^

                   

                  I include a snapshot of my structure in Indesign CS6.

                  Capture d’écran 2013-10-08 à 15.57.42.png

                  Thanks guys.

                   

                  hirsute

                  • 6. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                    Andreas Jansson Level 2

                    [I'm not sure... but some of these questions might be better off in individual threads]

                     

                    Answer to Pooja:

                     

                    I would assume that your structure looks like this:

                     

                    <root>

                      <ref>

                      <ref>

                      <ref>

                     

                    When you set the rootElement variable in your code, you get a reference to the 'root' element. If you go on from that element (to get the next level of items, under the root) you should not include the name "root".

                     

                    This would work:

                    var rootElement = app.activeDocument.xmlElements.item('root');

                    var items = rootElement.xmlElements.item('ref');

                    items.getElements().length;  // returns "2" with the XML as above

                     

                    To get all "ref" elements under the root, no matter their level (that is if you have "refs" on deeper levels then the one direcly under root), you could use an xpath expression like this, which will return an array:

                    var rootElement = app.activeDocument.xmlElements.item('root');

                    var items = rootElement.evaluateXPathExpression('//ref');

                    • 7. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                      Andreas Jansson Level 2

                      Girsute, you should remove the initial "/" from this line:

                       

                      var endnotes = root.evaluateXPathExpression("/endnote");

                       

                      The XPath expression already starts from the root.

                      • 8. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                        hirsute Level 1

                        Hi Andreas, thanks for your answer.

                         

                        My structure look like this:

                        <document>

                          <endnote>

                          <endnote>

                          <endnote>

                         

                        As you can see in my original post, that line works so I have no problem getting the root element:

                        var docElement = doc.xmlElements[0];

                        $.writeln(docElement); // [object XMLElement]

                        $.writeln(docElement.markupTag.name); // returns "document" - Good! I have the document element!

                         

                        And I can get the endnote elements with this:

                        var items = docElement.xmlElements.item('endnote');

                        $.writeln(items.getElements().length);  // returns "507" - So endnote elements are under document....

                         

                        So one of these should also work, but none does:

                        var endnotes = docElement.evaluateXPathExpression("/endnote");

                        $.writeln(endnotes); // returns nothing

                         

                        var endnotes1 = docElement.evaluateXPathExpression("//endnote");

                        $.writeln(endnotes1); // returns nothing

                         

                        var endnotes2 = docElement.evaluateXPathExpression("endnote");

                        $.writeln(endnotes2); // returns nothing

                         

                        This does not work either!

                        var self = docElement.evaluateXPathExpression(".");

                        $.writeln(self); // returns nothing!!

                         

                         

                        So I think the problem is with the evaluateXPathExpression().

                         

                        Any suggestions?

                         

                        Thanks!

                        • 9. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                          Andreas Jansson Level 2

                          All your examples except the first one work for me, you just have to know that it's arrays that you are dealing with when you check the results from your evaluateXPathExpression calls.

                           

                          A single slash character "/" should be the root node of the document. But I guess it's almost as a meta level, superior to the "document"-reference set to the variable docElement, in your example. Meaning that "/document" will work, but not "/endnote". I'm not 100% sure, but the "/" might never be able to find anything else but the element that evaluateXPathExpression is "run from".

                           

                          var endnotes = docElement.evaluateXPathExpression("/endnote");

                          $.writeln(endnotes); // returns nothing (since there are no endnote root elements).

                           

                          // Returns three "endnote" XMLElements from any level under docElement

                          var endnotes1 = docElement.evaluateXPathExpression("//endnote");

                          $.writeln(endnotes1); // Three elements

                          $.writeln(endnotes1.length);

                           

                          // Returns three "endnote" XMLElements from the level directly  under docElement

                          var endnotes2 = docElement.evaluateXPathExpression("endnote");

                          $.writeln(endnotes2);

                          $.writeln(endnotes2.length);

                          $.writeln(endnotes2[0].markupTag.name); // Writes out the name of the first element returned

                           

                          //The last example returned an array with length = 1, and checking its name returned "document":

                           

                          self[0].markupTag.name

                          Result: document

                           

                          Message was edited by: Andreas Jansson. Changes in the text about the slash character. It does not go to the top of the whole XML document in InDesign, it just acts as a container of docElement (in the example) and nothing else. In the example it happens to be the same as the document root.

                          • 10. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                            Green4ever Level 3

                            Hi All,

                             

                            As per the screen-shot "var root = doc.xmlElements[0];" will gives you the Doctype element not the root element "document". Due to this it is not returning the correct result.

                             

                            Replace that with the following line,

                            var root = doc.xmlElements.item('document');    

                             

                             

                            ~Green4ever


                            • 11. Re: [CS5.5:JS] - evaluateXPathExpression not working - Reg.
                              hirsute Level 1

                              Thanks Green4ever,

                               

                              I tested with your suggestion and I get the same result. evaluateXpathExpression still doesn't work.

                               

                              hirsute