5 Replies Latest reply on Jul 9, 2018 7:21 AM by airprint

    how can i find and replace xml tags?


      Hi, i am using xml in my workflow and want to be able to remove certain tags if they contain particular text.


      here is an example of my xml structure…








      I am using this initial structure to set the paragraph styles to be followed when the xml data is imported.


      This leaves DEFAULT in place wherever an entry doesn't have any content for that field.


      I want to be able to import my XML then run a script that removes any tags that include DEFAULT, - I need the entire xml tag to be removed not just the text, if i do a normal find and replace it will only remove the text not the tags which is causing problems with styling. I also want to remove the end of para/return (^p) that i've placed at the end of the line. So it would be the same as opening up story editor and removing the content + tags + hard return in there, but i want to automate the process…


      So i think this is what i need to search for in each case



      and i want to replace it with nothing ""


      Can this be done through scripting (ideally javascript)?


      I have a little knowledge of javascript but am not sure how to search and target that kind of string in indesign...


      using indesign cs5


      many thanks

        • 1. Re: how can i find and replace xml tags?
          Jump_Over Level 5



          Script should do it in two steps:

          1. find all occurences of i.e. ">DEFAULT<"

          2. remove whole paragraph which is a found_text's container.


          For example this way -JS - (a textFrame filled with your text should be selected) :


          var mStory = app.selection[0].parentStory;
          app.findTextPreferences =  null;
          app.findTextPreferences.findWhat = ">DEFAULT<";
          var myF = mStory.findText();
          var count = myF.length;
          while (count--)



          • 2. Re: how can i find and replace xml tags?
            Altper Level 1

            Hi, thanks for you help, this has worked very well

            • 3. Re: how can i find and replace xml tags?
              Trevorׅ Adobe Community Professional

              Sorry Jumpy


              I did not like your 2 stage looped code whether it  works or not!


              This is a bit more efficient .  One could make it shorter but I think this is safer.


              app.changeGrepPreferences = app.findGrepPreferences = null;
              app.findGrepPreferences.findWhat =  "\\s*<.+>DEFAULT</.+>\r";
              • 4. Re: how can i find and replace xml tags?
                Jump_Over Level 5

                Hi Trevor,


                Basically, I agree.

                2 stage code is used because textFind() is in it. Grep is more efficient

                but it needs a deeper knowledge, which is a mother of safety, in fact.


                Since "\s" catches "\r" ==> this Grep removes a  previous "end of paragraph".

                I am not sure it is a goal, but maybe it is...

                Questions & answers on our forum are "out of context" most often

                • 5. Re: how can i find and replace xml tags?
                  airprint Level 1



                  I have a problem which is a bit different. I want to replace part of my text within an XML tag across multiple XML files. For example, in the following tag I want to replace "../../../../Resource" with "../../../../Library". I don't want to make any changes to the tag or remaining text.




                  Can you suggest how this can be done using JavaScript?