4 Replies Latest reply on Sep 5, 2014 9:55 AM by Dirk Becker

    MN Elements in XML Jobs

    BEGINNER_X Level 3

      Hi All,

       

      I need to pull out the following marginal tags in XML jobs.

       

      I hope problem is in coding 18th line.

       

      Screenshot:

       

      Screen Shot 2014-07-17 at 11.14.24 AM.png

       

       

      Coding is below:

       

      var myDoc = app.activeDocument;  
      
      var myLib = app.libraries[0];
      
      var myXMLElement = app.activeDocument.xmlElements[0].evaluateXPathExpression("//marginalnote");
      
      alert(myXMLElement.length);
      
      for(i=myXMLElement.length-1; i>0; i--)
      
      {
              var myText = myXMLElement[i].texts[0];
              
              alert(myText.contents)
              
              var myLibraryAsset = app.libraries[0].assets.item("box").placeAsset(myXMLElement[i].insertionPoints[-1])[0];
              
              var myTextMove = myXMLElement[i].texts[0].move(LocationOptions.AT_END, myLibraryAsset.insertionPoints[0])           //Problem in this only
              
               myLibraryAsset.appliedObjectStyle = myDoc.objectStyles.item("Box")
      
              myLibraryAsset.fit(FitOptions.frameToContent)
      
          }
      
      
      
      
      
      
      
      
      

       

      Thanks in advance

      Siraj

        • 1. Re: MN Elements in XML Jobs
          BEGINNER_X Level 3

          Any update for the request????

           

          Error screenshot is below,

           

          Screen Shot 2014-07-18 at 3.46.21 PM.png

          • 2. Re: Re: MN Elements in XML Jobs
            Dirk Becker Level 4

            As I read it from your source, without trying to reproduce the environment:

             

            When you place your library asset, the result (a placed "box" page item, not a library asset any more) ends up within the xml element. Within means anchored within the contents text via a special character. That special character is your problem, because it is is also contained in your text[0]. So the error message means you can't move that anchoring character into the anchored box.

             

            To solve the problem, place the library asset outside of the xml element. I'd also tag the frame rather than leaving the tag empty within the text. There's a nice method call for the combination of those operations.

            You can even further optimize if you place the "box" only once, e.g. on an unused master page, then utilize the second parameter of placeIntoInlineCopy() to produce copies. I think it is a bug that after my "false" I still have to remove tf1.

             

            var xe = app.activeDocument.xmlElements.item(0).evaluateXPathExpression("//P")[0];

            var box = app.libraries[0].assets.item("box");

            var tf1 = box.placeAsset(xe.storyOffset)[0];

            var tf2 = xe.placeIntoInlineCopy(tf1,false);

            tf1.remove();

            • 3. Re: Re: Re: MN Elements in XML Jobs
              BEGINNER_X Level 3

              Hi Dirk,

               

              Thank You for the very late response...

               

              With the help of your code, I got the solution.

               

              var myDoc = app.activeDocument;   
              var myLib = app.libraries[0]; 
              var myXMLElement = app.activeDocument.xmlElements[0].evaluateXPathExpression("//marginalnote"); 
                  alert(myXMLElement.length); 
                   
                  for(i=myXMLElement.length-1; i>0; i--) 
                  { 
                          var myText = myXMLElement[i].texts[0]; 
                          var box = app.libraries[0].assets.item("box")
                          var tf1 = box.placeAsset(myXMLElement[i].storyOffset)[0];
                          var tf2 = myXMLElement[i].placeIntoInlineCopy(tf1,false);
                          tf1.remove();
                          tf2.appliedObjectStyle = app.activeDocument.objectStyles.item("Box");
                          tf2.fit(FitOptions.frameToContent);
                      } 
              
              

               

               

              Learning XML Workflow:

               

              myXMLElements = myGetXMLElements("//marginalnote");
              
              myXMLElements = myGetXMLElements("//marginalnote[@type='mn4']");
              
              function myGetXMLElements(myXPath) {
                  var myXMLElements = new Array;
                  var myRuleProcessor = app.xmlRuleProcessors.add([myXPath]);
                      try {
                          var myMatchData = myRuleProcessor.startProcessingRuleSet(app.documents.item(0).xmlElements.item(0));
                          while (myMatchData != undefined) {
                              var myElement = myMatchData.element;
                              myXMLElements.push(myElement);
                              myMatchData = myRuleProcessor.findNextMatch();
                          }
                  myRuleProcessor.endProcessingRuleSet();
                          myRuleProcessor.remove();
                          return myXMLElements;
                      } catch (myError) {
                          myRuleProcessor.endProcessingRuleSet();
                          myRuleProcessor.remove();
                          throw myError;
                      }
              }
              }
              

               

               

              Regards

              Siraj

              • 4. Re: Re: Re: MN Elements in XML Jobs
                Dirk Becker Level 4

                I'm glad that helps. Some more hints:


                Read about try-catch-finally. If you move your myRuleProcessor cleanup code (both from try and catch clause) into a "finally" clause, you need not catch/rethrow any more. e.g.

                try {

                  somethingBad();

                } finally {

                  // even executed when an exception comes along

                }

                 

                Since CS4 there is also evaluateXPathExpression() which would be a direct replacement for myGetXMLElements().