3 Replies Latest reply on Apr 29, 2010 12:00 AM by Adobe-InDesign CS4

    Removing unassociated Xml elements from structure

    Marco Lugli Level 1

      Hello, I use Indesign CS3,

      I have a document containing a series of xml elements: some of these are associated with some graphic elements on the page and some are not.

      is there a way to remove all unassociated xml elements from the structure of this document by javascript?

      I tried this:

       

      main();
      function main(){
          var myDocument = app.activeDocument;
          var myRootXmlElement = myDocument.xmlElements.item(0);
          var myXmlElements = myRootXmlElement.xmlElements;

       

          function deleteUnassociated(){
              for(i = 0; i < myXmlElements.length; i++){
                  var myXmlElement = myXmlElements[i];
                 
                  if(myXmlElement.xmlContent == null){
                      myXmlElement.remove();
                  }
              }
          }

       

          deleteUnassociated();
      }

       

      but with no results..

      Any suggestions?

      Thanks

        • 1. Re: Removing unassociated Xml elements from structure
          Adobe-InDesign CS4 Level 1

          Dear Macro,


          Please use the below script....


          ///=========================== Start ====================//


              var myDocument = app.activeDocument;
          //    var myRootXmlElement = myDocument.xmlElements.item(0);
              //var myXmlElements = myRootXmlElement.xmlElements;

           

          deleteUnassociated(myDocument)

           

              function deleteUnassociated(elm){
                  for(var i = 0; i < elm.xmlElements.length; i++){
                      var myXmlElement = elm.xmlElements[i];
                    
                      if(myXmlElement.xmlContent == null){
                          elm.xmlElements[i].remove();
                      }
                  }
              deleteUnassociated(elm.xmlElements[i]);
               }

           

           


          ////====================== End =====================//



          Regards

          T.R.Harihara Sudhan

          • 2. Re: Removing unassociated Xml elements from structure
            Marco Lugli Level 1

            Sorry Harihara,

            but it doesn't work..

            • 3. Re: Removing unassociated Xml elements from structure
              Adobe-InDesign CS4 Level 1

              Dear Macro.


              var myDoc  = app.activeDocument;

               

               

              //============================Start ============================//

              Check(myDoc);

               

              function Check(elm)
              {
                  try
                   {
                       for(var i=0; i<elm.xmlElements.length; i++)
                        {
                             myXMLElement = elm.xmlElements[i];
                             if((myXMLElement.contents == "") || (myXMLElement.contents == null) || (myXMLElement.contents == undefined))
                              {
                                    elm.xmlElements[i].remove();
                                  }
                               Check(elm.xmlElements[i]);
                            }
                        }catch(e)
                          {
                              exit(0);
                              }
                  }

              //===================================== End ============================//


              Pls. use the above one...


              Regards

              T.R.Harihara Sudhan