2 Replies Latest reply on Jul 23, 2015 8:54 PM by sreekarthik.k@gmail.com

    XML attribute change

    Level 1

      I need to replace the xml attribute value "M05_LEMO9433_06_SE_CH05" to M05_LEMO5401_06_SE_CH05

       

       

      In document having more than thousand entries

       

       

      I have used the below code but it is not working

       

       

      function AddReturns(){

          this.name = "AddReturns";

          this.xpath = "//p//span//a";

          this.apply = function(myElement, myRuleProcessor){

       

       

              with(myElement)

        {

        try{

                         

                    //      if (myElement.xmlAttributes.item("href").value.match ("LEMO9433 "))

                              {

                                 

                                  var fig = myElement.xmlAttributes.itemByName("href").value;

                                  var    figv =  fig.replace ("9433", "O5401")

                                  myElement.xmlAttributes.itemByName("href").value=figv;

       

       

                                  }

                         

                         

        //myElement.xmlAttributes.item("id").name = "olinkend";

        }catch(e){}

       

              }

              return true;

          }

      }

        • 1. Re: XML attribute change
          Loic.Aigon Adobe Community Professional

          Try this :

           

          var main  = function() {
            var root, 
            xes,
            n,
            ov = "M05_LEMO9433_06_SE_CH05",
            nv = "M05_LEMO5401_06_SE_CH05";
            if ( !app.documents.length ) return;
          
            root = app.activeDocument.xmlElements[0];
          
            xes = root.evaluateXPathExpression ("//*[@*='"+ov+"']" );
            n = xes.length;
          
            while ( n-- ) {
            changeAttributeValue ( xes[n], ov, nv ); 
            }
          }
          
          
          var changeAttributeValue  = function ( xe, oldValue, newValue ) {
            var xas = xe.xmlAttributes,
            xa,
            n = xas.length;
          
            while ( n-- ) {
            xa = xas[n];
            xa.value == oldValue && xa.value = newValue;
            }
          }
          
          
          main();
          
          • 2. Re: XML attribute change
            Level 1

            Thanks loicaigon

             

            I have tried like this

             

            main();

            alert("Comepleted", "Attribute Replacer");

            exit();

             

             

            function main()

            {

              var aDoc = app.activeDocument;

              var processes = new Array(

              new findKeyTerm

              );

              

             

              with(aDoc)

              {

              var element = xmlElements.item(0);

              __processRuleSet(element, processes);

              }

             

             

             

             

            function findKeyTerm()

              {

              this.name = "findKeyTerm";

              this.xpath = "//a[@href]";

              this.apply = function(caught, runningProcess)

              {

              //with(caught)

              var tmp = (caught.xmlAttributes.item("href").value);

              tmp = tmp.replace("LEMO9433", "LEMO5401");

              caught.xmlAttributes.item("href").value = tmp;

             

              $.writeln(caught.xmlAttributes.item("href").value);

             

              }

              }

            }