Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Script to search for an XML tag and remove it

Return to Member Forum

  • Author
    Posts
    • #94303

      Hi Everyone,

      Need help InDesign masters. Please provide me a script that search for an XML tag and remove it including its content. Any help would be highly appreciated. Thanks.

    • #94317
      Loic Aigon
      Member

      <pre class=”brush: javascript”>
      var main = function() {
      var doc = app.properties.activeDocument,
      root, xes, n;
      if ( !doc ) return;
      root = doc.xmlElements[0];
      xes = root.evaluateXPathExpression ( “.//myTagName” );
      n = xes.length;

      while ( n– ) xes[n].remove();

      }

      var u;

      app.doScript ( “main()”,u,u,UndoModes.ENTIRE_SCRIPT, “The Script” );

      Where “myTagName” is the name of the tag you want to remove. Be aware that with tagged texts, only the text object will ne removed while the container will remain.

      HTH

      Loic

    • #94368
      Masood Ahmad
      Participant

      Hi Loic,

      There is an error in the script, can you please check and fix the error. I’m trying this on InDesign CS5


      <pre class="brush: javascript">
      var main = function() {
      var doc = app.properties.activeDocument, root, xes, n;
      if ( !doc ) return;
      root = doc.xmlElements[0];
      xes = root.evaluateXPathExpression ( ".//SalaryDescription" );
      n = xes.length;
      while ( n– ) xes[n].remove();
      }

      var u;

      app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );

    • #94372

      Thanks Loic. Unfortunately, I also receive an error with the script. But this one works…

      var doc = app.activeDocument;

      var myRuleSet = new Array (new Function1)
      with(doc)
      {
      var elements = xmlElements;
      __processRuleSet(elements.item(0), myRuleSet);
      }

      function Function1()
      {
      this.name = “Function1”;
      this.xpath = “//article”;
      this.apply = function(myElement, myRuleProcessor)
      {
      with(myElement)
      {
      var ec=myElement.xmlElements.count();
      $.writeln(ec);
      for(var i=0; i<ec; i++)
      {
      if(myElement.xmlElements[i].markupTag.name==”<tag_name_being_deleted>”)
      {
      myElement.xmlElements[i].remove();
      }}}
      return false;
      }

      // alert(“Process completed”);

      //Built in function starts here.
      function __processRuleSet (root, ruleSet, prefixMappingTable)
      {
      var mainRProcessor = __makeRuleProcessor(ruleSet, prefixMappingTable);

      try {
      __processTree(root, mainRProcessor);
      __deleteRuleProcessor(mainRProcessor);
      } catch (e) {
      __deleteRuleProcessor(mainRProcessor);
      throw e;
      }
      }

      function __makeRuleProcessor(ruleSet, prefixMappingTable){
      // Get the condition paths of all the rules.
      var pathArray = new Array();
      for (i=0; i<ruleSet.length; i++)
      {pathArray.push(ruleSet[i].xpath);}
      try
      {var ruleProcessor = app.xmlRuleProcessors.add(pathArray, prefixMappingTable);}
      catch(e){throw e;}
      var rProcessor = new ruleProcessorObject(ruleSet, ruleProcessor);
      return rProcessor;}

      function ruleProcessorObject(ruleSet, ruleProcessor)
      {this.ruleSet = ruleSet;
      this.ruleProcessor = ruleProcessor;}

      function __deleteRuleProcessor(rProcessor) {
      // remove the XMLRuleProcessor object
      rProcessor.ruleProcessor.remove();

      // delete the object properties
      delete rProcessor.ruleProcessor;
      delete rProcessor.ruleSet;

      // delete the object itself
      delete rProcessor;
      }

      function __processTree (root, rProcessor)
      {
      var ruleProcessor = rProcessor.ruleProcessor;
      try
      {var matchData = ruleProcessor.startProcessingRuleSet(root);
      __processMatchData(matchData, rProcessor);
      ruleProcessor.endProcessingRuleSet();}
      catch (e)
      {ruleProcessor.endProcessingRuleSet();
      throw e;}}

      function __processMatchData(matchData, rProcessor)
      {var ruleProcessor = rProcessor.ruleProcessor;
      var ruleSet = rProcessor.ruleSet;
      while (matchData != undefined)
      {var element = matchData.element;
      var matchRules = matchData.matchRules;
      var applyMatchedRules = true;

      for (var i=0; i<matchRules.length && applyMatchedRules && !ruleProcessor.halted; i++)
      {applyMatchedRules = (false == ruleSet[matchRules[i]].apply(element, rProcessor));}
      matchData = ruleProcessor.findNextMatch();}}

    • #94374
      Masood Ahmad
      Participant

      Neither this worked, sorry!

    • #94375
      Loic Aigon
      Member

      maybe some item associated with a xml element is locked or its layer locked.

Viewing 5 reply threads
  • You must be logged in to reply to this topic.
Forum Ads