5 Replies Latest reply on May 8, 2013 3:33 AM by csm_phil

    Looping over all xml elements

    SimonLinden Level 1

      How do I loop over all the tagged xml elements in a story? I've created a reference to the xml elements of the story I want like this:

       

      var xmlStory = myDoc.xmlElements[0].xmlElements[0];
      

      In this structure I have elements <v>10</v> that I want to change to <v id="10">10</v>. I'd like to do this in InDesign scripting and not with XSL upon export because of the workflow I'm planning. I know I can loop over the children in the structure like this:

       

      for(var i=0;i<xmlStory.xmlElements.length;i++){
                          if(xmlStory.xmlElements[i].markupTag.name == "v"){
                                    xmlStory.xmlElements[i].xmlAttributes.add("id", xmlStory.xmlElements[i].contents);
                         }
               }
      

       

      This works fine for just the first level, but I want to find every element with the markup tag "<v>". These tags can appear at any level in the structure. How do I make sure all of them are included and not just the first child elements of the story element?

        • 1. Re: Looping over all xml elements
          csm_phil Level 4

          Hi SimonLinden,

           

          Please try the below JS code its works only CS5.5.

           

           

          #target InDesign;
          
          #include /Program Files (x86)/Adobe/Adobe InDesign CS5.5/Scripts/XML Rules/glue code.jsx// change the script path in your InDesign version CS4 or CS5
          
          var myDoc = app.activeDocument;
          
          //____________________ XML RULE SET
          var myRuleSet = new Array (new margintag); 
          with(myDoc){
              var elements = xmlElements;
              __processRuleSet(elements.item(0), myRuleSet);
          }
          
          
          function margintag(){
              this.name = margintag;
              this.xpath = "//v";
              this.apply = function(myElement, myRuleProcessor){
                  with(myElement){
                      try{
                          myElement.xmlAttributes.add("id", myElement.xmlContent.contents);
                          }catch(e){}
                      }
                  return true;
                  }
              }
          

           

           

          thx,

          csm_phil

          1 person found this helpful
          • 2. Re: Looping over all xml elements
            Cenchen Level 2

            Try this,                 

             

            #target InDesign;

            var doc = app.activeDocument;
            var xmlTag = doc.xmlElements[0];
            var vTag = xmlTag.evaluateXPathExpression("//v")
            for(var i=0;i<vTag.length;i++)
            {
                try
                {
                    vTag[i].xmlAttributes.add("id",  vTag[i].xmlContent.contents)
                 }
                catch(e){}
            }

            • 3. Re: Looping over all xml elements
              SimonLinden Level 1

              Thanks! This works perfectly. Didn't know about XPathExpressions but it seems to be just what I needed.

              • 4. Re: Looping over all xml elements
                SimonLinden Level 1

                Thanks for the suggestion cms_phil. I'm using CS6 so I couldn't get this code to work but I could do it with Cenchens suggestion.

                • 5. Re: Looping over all xml elements
                  csm_phil Level 4

                  Hi SimonLinden,

                   

                  My code its working perfectly only CS5.5. Becuase I include the path for CS5.5 version folder path. You just change the below line.

                   

                  You just change the below line and try again .

                   

                   

                  #include /Program Files (x86)/Adobe/Adobe InDesign CS6/Scripts/XML Rules/glue code.jsx// change the script path in your InDesign version CS4 or CS5
                  

                   

                  thx,

                  csm_phil