8 Replies Latest reply on Sep 15, 2012 8:45 AM by beh_gras_beh

    Rename XML tags

    beh_gras_beh Level 1

      Dear People,

       

      Is there a way to find and replace all XML items.

       

      find xmlTags.item("tag1") replace to xmlTags.item("tag2") ?

       

      Greetings

        • 1. Re: Rename XML tags
          Fred Goldman Level 3

          var myElements = myElement.evaluateXPathExpression("//tag1");

          for(var x = myElements.length -1;x>=0;x--)

          myElements[x].markupTag = myDoc.xmlTags.item("tag2");

          1 person found this helpful
          • 2. Re: Rename XML tags
            beh_gras_beh Level 1

            Dear Fred,

             

            Thanks for the help.

            I know it is a stupid question but why is the script giving me a error myElement is undifined.

            Because of the myElement.evaluateXPathExpression?

             

            But maybe I thought it was the path? ("//Workbook/tag1")?

            What am I doing wrong?

             

            Schermafbeelding 2012-09-09 om 22.34.27.png

             

            P.s. Forgot to mention i'm on CS5 (MAC).

            • 3. Re: Rename XML tags
              Fred Goldman Level 3

              All the variables that start with "my" are my own variables. You should

              replace those with a whatever variable you are working with.

               

              For example:

               

              var myElement = app.activeDocument.xmlElements[0];

              1 person found this helpful
              • 4. Re: Rename XML tags
                beh_gras_beh Level 1

                Thanks,

                 

                Still learning, but am I going the right way?

                 

                 

                var myDoc = app.activeDocument;
                
                var myElement = myDoc.xmlElements.item(0);
                var myElements = myElement.evaluateXPathExpression("//tag1");
                
                for(var x = myElements.length -1;x>=0;x--)
                
                myElements[x].markupTag = myDoc.xmlTags.item("tag2");
                
                • 5. Re: Rename XML tags
                  Fred Goldman Level 3

                  Looks good!

                  • 6. Re: Rename XML tags
                    beh_gras_beh Level 1

                    Edit.

                     

                    Have it!!!!

                     

                     

                    var myDoc = app.activeDocument;
                    
                    var myElement = myDoc.xmlElements.item(0);
                    var myElements = myElement.evaluateXPathExpression("//tag1");
                    
                    for(var x = myElements.length -1;x>=0;x--)
                    
                    myElements[x].markupTag.name =("tag2");
                    

                     

                     

                    Thank you very much for the help!

                    As you can see Im still trying to understand how Javascript works.

                     

                     

                    Again thanks.

                    • 7. Re: Rename XML tags
                      Kasyan Servetsky Level 5

                      I think it makes no sense to loop through all the xml-elements to change the name of a tag. As in the UI, you have to do this only once:

                       

                      var myDoc = app.activeDocument;
                      var myElement = myDoc.xmlElements.item(0);
                      var myElements = myElement.evaluateXPathExpression("//tag1");
                      if (myElements.length > 0) myElements[0].markupTag.name = "tag2" ;
                      

                      Or even simpler:

                      var myDoc = app.activeDocument;
                      var myTag = myDoc.xmlTags.itemByName("tag1");
                      myTag.name = "tag2";
                      

                      On the other hand, if you want to apply a different tag to an xml-element, you can do it like so:

                      var myDoc = app.activeDocument;
                      var myElement = myDoc.xmlElements.item(0);
                      var myElements = myElement.evaluateXPathExpression("//tag1");
                      
                      for (var x = myElements.length -1;x>=0;x--)
                                      myElements[x].markupTag = myDoc.xmlTags.itemByName("tag2") ;
                      
                      1 person found this helpful
                      • 8. Re: Rename XML tags
                        beh_gras_beh Level 1

                        Thanks!

                         

                        That is very usefull!

                         

                        Greetings!