16 Replies Latest reply on Mar 2, 2010 7:16 PM by Mac_06

    Remove xml element using JS[CS3]

    Mac_06 Level 2

      Hi, I have an xml structure where i want to remove some paritcular element named "extlink".

      I do have one script with me but it doesn't wipe out all specific xml element from structure.

       

      var myDoc = app.activeDocument;

      var foundtext = 0
      Query_Remove (myDoc);
      alert ("You have removed " + foundtext + " " +" Link !!")

      exit (0);

       

       

      function Query_Remove(elm)
      {
      try
      {
      for(var i=elm.xmlElements.length-1; i>=0; i--)
      {
           if((elm.xmlElements[i].markupTag.name == "extlink") && (elm.xmlElements[i].xmlElements[0].markupTag.name == "http"))
         {
          var Store_CitAttri = elm.xmlElements[i].xmlElements[0].xmlAttributes.item("c_style").value;
           if(Store_CitAttri == "URL")
            {
            elm.xmlElements[i].remove();

           foundtext = foundtext + 1;
           
           
           
            }  
         }
         Query_Remove(elm.xmlElements[i]);

      }
      catch (e){

      }

       

       

      Some time it skip the element from all figure captions and some time from body text and some time it works perfectly

      Could any one figure it out why this script behaviour is not consistent as i am new to scripting and not getting any idea about this.

       

       

      Thanks

      Mac

        • 1. Re: Remove xml element using JS[CS3]
          Shonkyin Level 3

          Hi Mac

           

          I have checked your script and its look ok.

          Some time it skip the element from all figure captions and some time from body text and some time it works perfectly

           

          When it skip in some places, please make sure your markup tag name is "extlink" and its first xml element name is "http". If both conditions true then it will check for "http" makrup tag attribute value is "URL". If all condition true then it will remove the "extlink" tag.

           

           

          Shonky

          • 2. Re: Remove xml element using JS[CS3]
            [Jongware] Most Valuable Participant

            The script does not attempt to remove every occurrence of extlink.

             

            The "if" clause before the actual 'remove' line first checks if the extlink tag contains another tag "http". If so, it checks if that tag has an attribute "URL". If that's also the case the tag "extlink" is removed. If one or the other condition is false, the extlink tag is not removed. Is this what is happening now?

             

            Do you want the script to be re-written to completely remove all occurrences of "extlink"?

            • 3. Re: Remove xml element using JS[CS3]
              Mac_06 Level 2

              Thaks, for reply Jongware

               

              Yes, your observation is absolutly correct,

               

              This script looks all conditions match and then remove the element but in some extent inspite of matching all conditions it skips some elements.

               

              Any help would be appriciated

               

              Many Thanks in advance

              Mac

              • 4. Re: Remove xml element using JS[CS3]
                [Jongware] Most Valuable Participant

                Okay -- I'm glad Shonky came to the exact same conclusion!

                 

                Can you show us a screen grab of the XML Structure tree where some of the items are not removed after running the script?

                • 5. Re: Remove xml element using JS[CS3]
                  Mac_06 Level 2

                  Please refer the attached link to see the tree view of xml structure

                   

                  http://dl.dropbox.com/u/4847331/Scripts/xml_prob.doc

                   

                  Script has removed all "extlink" from body text but skipt from all figure captions.

                   

                  Hey, surprisigly I am not getting the option for attachment the file at forum

                   

                   

                  Thanks

                  Mac

                  • 6. Re: Remove xml element using JS[CS3]
                    Mac_06 Level 2

                    Hey, I am trying to attach the xml snap grab but there is no option for attachement any idea???

                     

                     

                    Mac

                    • 7. Re: Remove xml element using JS[CS3]
                      Mac_06 Level 2

                      Oh, Silly question asked

                       

                      there is an announcement about temporary disable attachment option on adobe site.

                      • 8. Re: Remove xml element using JS[CS3]
                        Shonkyin Level 3

                        Your attached link not working.

                         

                         

                        Shonky

                        • 9. Re: Remove xml element using JS[CS3]
                          Mac_06 Level 2

                          Any update on this???

                           

                          Any other idea to complete this task.???

                           

                          Please use above given link now it's working sorry for inconvenience caused.

                           

                          Mac

                          • 10. Re: Remove xml element using JS[CS3]
                            Shonkyin Level 3

                            Hi,

                             

                            Try below code:

                             

                             

                            var myDoc = app.activeDocument;

                            var foundtext = 0
                            Query_Remove (myDoc);
                            alert ("You have removed " + foundtext + " " +" Link !!")

                            exit (0);

                             

                             

                            function Query_Remove(elm)
                            {
                            try
                            {
                            for(var i=elm.xmlElements.length-1; i>=0; i--)
                            {
                                 if((elm.xmlElements[i].markupTag.name == "http") && (elm.xmlElements[i].parent.markupTag.name == "extlink"))
                               {
                                var Store_CitAttri = elm.xmlElements[i].xmlAttributes.item("c_style").value;
                                 if(Store_CitAttri == "URL")
                                  {
                                  elm.xmlElements[i].remove();

                                 foundtext = foundtext + 1;
                                 
                                
                                 
                                  }  
                               }
                               Query_Remove(elm.xmlElements[i]);

                            }
                            catch (e){

                            }

                            }

                            Shonky

                            • 11. Re: Remove xml element using JS[CS3]
                              Mac_06 Level 2

                              Thanks for your reply shonky But it doesn't seems to work.

                               

                              AFAK there is some problem releted with loop because If I run the same

                              script again on same file it deletes some more tags.

                               

                              Is there any other work around please help.

                               

                              Mac

                              • 12. Re: Remove xml element using JS[CS3]
                                Mac_06 Level 2

                                Any help on this, still awating ???

                                 

                                Mac

                                • 13. Re: Remove xml element using JS[CS3]
                                  Steven.. Level 3

                                  Did You Try Looping Backwards

                                  • 14. Re: Remove xml element using JS[CS3]
                                    Mac_06 Level 2

                                    Thanks for your reply steven but in my first disscussion code also I used

                                    backward looping and I tried with forward looping too.

                                    • 15. Re: Remove xml element using JS[CS3]
                                      [Jongware] Most Valuable Participant

                                      Okay, after peering over your code and trying a few things out I see one immediate problem and one possible improvement.

                                       

                                      The problem lies in this:

                                       

                                      1. if condition is true, remove item

                                      2. check recursively

                                       

                                      The problem is .. if an item is removed, the following recursive check will always fail. However, the entire loop is wrapped into a try..catch, so InDesign will not alert you that it failed. Instead, it will continue with what's after the loop, exactly because the try..catch is around the entire loop!

                                       

                                      Change the loop code to this

                                       

                                      if((elm.xmlElements[i].markupTag.name == "http") && (elm.xmlElements[i].parent.markupTag.name == "extlink"))
                                      {
                                          var Store_CitAttri = elm.xmlElements[i].xmlAttributes.item("c_style").value;
                                          if(Store_CitAttri == "URL")
                                          {
                                            elm.xmlElements[i].remove();

                                             foundtext = foundtext + 1;

                                             continue;
                                          }
                                      }
                                      Query_Remove(elm.xmlElements[i]);

                                       

                                      so the recursive checking is skipped. You can also remove the entire try..catch block, as this may hide additional errors ...

                                       

                                      The improvement is: as I started reading this thread from the top again, shouldn't you be removing the "extlink" items, rather than just the "http" ones inside?

                                      If so, remove the parent of the found item (which is always the "extlink"):

                                            elm.xmlElements[i].parent.remove();

                                      -- and, as this will break the loop (because you just removed the set of elements it was working in!), replace 'continue' with 'return'.
                                      Hope this helps

                                       

                                      • 16. Re: Remove xml element using JS[CS3]
                                        Mac_06 Level 2

                                        Many....... thanks, Jongware it's great and work fine.

                                         

                                         

                                         

                                         

                                        Mac