4 Replies Latest reply on Oct 7, 2014 12:05 AM by JulianFT

    Change content with XPath

    JulianFT Level 1

      Dear all,

       

      I am attempting to search for XML elements in my document using XPath, and then alter its contents. I do realize that this is supposed to be a rather basic action, but documentation on this matter is surprisingly scarce/unclear (or perhaps my search skills are merely lacking).

       

      Currently, I attempted to use evaluteXPathExpression, but I have no clue whether that is the right way to go. By any means, any iteration I try to make does not get through.

       

      var myDocument = app.activeDocument;
      var myRootXML = myDocument.xmlElements[0];
      var myXMLPath = myRootXML.evaluateXPathExpression("//Test[1]/Test[1]");
      myXML.insertTextAsContent("Sample text", XMLElementPosition.ELEMENT_END);
      

       

      Kind regards and thanks in advance,

       

      Julian

        • 1. Re: Change content with XPath
          JulianFT Level 1

          Anyone, with even the slightest nudge at all?

          • 2. Re: Re: Change content with XPath
            JulianFT Level 1

            For future reference for anyone with the same issue: the problem with the solutions found online was the fact that I had foolishly not included the glue code.jsx file with the script file, thus leading to an error at the __processRuleSet defined within. This admittedly not very pretty code works:.

             

            #include "glue code.jsx";
            var myDocument = app.documents.item(0);
            var myRuleSet = new Array (new ChangeTitle);
            with(myDocument){
                __processRuleSet(xmlElements[0], myRuleSet);
                 }
            
            function ChangeTitle(){
                this.name = "ChangeTitle";
                this.xpath = "/Book/Chapter/ChapterInfo/cs_ChapterInfo/ChapterTitle"; // can be any XPath String
                this.apply = function(myElement, myRuleProcessor){
                with(myElement){
                    app.findGrepPreferences.findWhat = myElement.contents;
                    app.changeGrepPreferences.changeTo = "Whatever"; //Change-to text
                myDocument.changeGrep();
                }
                return true;
                }
            }
            
            • 3. Re: Re: Re: Change content with XPath
              Dirk Becker Level 4

              For that simple task you don't need the complicated rule processor. And using grep to edit is also only for thrill seekers.

               

              Your problems:

              - evaluateXPathExpression returns an array

              - you assign that result to one variable, but then use a different one

               

              var myDocument = app.activeDocument; 
              var myRootXML = myDocument.xmlElements[0]; 
              var myXMLPath = myRootXML.evaluateXPathExpression("//Test[1]/Test[1]");
              if( myXMLPath.length>0 ) {
                var myXML = myXMLPath[0];
                myXML.contents = "Bla";
               myXML.insertionPoints.lastItem().contents = "++";
                myXML.insertTextAsContent("Sample text", XMLElementPosition.ELEMENT_END);
              }
              

               

              I never used insertTextAsContent, it might work but I provided some alternatives anyway.

               

              Hth,

              Dirk

              • 4. Re: Re: Re: Change content with XPath
                JulianFT Level 1

                Now that's elegant. Thanks so much for your help!