9 Replies Latest reply: Jun 29, 2012 10:13 PM by Velprakash RSS

    Rename particular XML Tag

    Velprakash Community Member

      Hi Indesign Experts,

                I'm confused a lot in the below issue. I'm have a indesign document. In the xml structure, If i select a element, and trying to rename the selected item's markupTag.name, it's changing all the elements with the selected elements name....

      input.GIF

      I use the below code to run after selecting an xml Element.

       

      app.selection[0].markupTag.name="test";

      But the output look like the below.

      output.GIF

      But I want onlu to rename the selected xmlelement.

      Any idea to perform this?

      Thanks and Regards,

      Vel.


        • 1. Re: Rename particular XML Tag
          absqua Community Member

          You're changing the name of the applied tag rather than assigning a different tag. Try this:

           

          var testTag = app.activeDocument.xmlTags.item("test").isValid ? app.activeDocument.xmlTags.item("test") : app.activeDocument.xmlTags.add({name: "test"});
          app.selection[0].markupTag = testTag;
          

           

          Jeff

          • 2. Re: Rename particular XML Tag
            Dirk Becker Community Member

            Jeff is right.

             

            There is a quick and dirty way to create the tag on the fly: the "markupTag" property also accepts strings.

             

            app.selection[0].markupTag = "test";

             

            Dirk

            • 3. Re: Rename particular XML Tag
              absqua Community Member

              Thanks Dirk. That's better. For some reason I always use the more explicit object assignment in these kinds of situations. Do you know of other instances where a string assignment like that will result in a non-existant object being created on the fly? Are there any rules governing this that you can make out?

              • 4. Re: Rename particular XML Tag
                Dirk Becker Community Member

                The internal description of properties allows for alternative types. Most of the time the primary type would be the result for the getter access, while the alternative types are the accepted values for setters. I haven't searched for them in detail, but styles come to mind that are adressable by name, without creating them.

                 

                In the ObjectModel Viewer xml file these types are automatically lumped together in the comment.

                 

                Dirk

                • 5. Re: Rename particular XML Tag
                  absqua Community Member

                  Dirk Becker wrote:

                   

                  ...but styles come to mind that are adressable by name, without creating them.

                   

                  I understand that you can use strings for assignments to a lot of different properties, but, e.g. in the case of styles, if the style itself—not the JavaScript object—doesn't exist, you'll get an error; it won't create the style for you. Your example with the xmlTags is the first time I think I've seen such creation "on the fly."

                  • 6. Re: Rename particular XML Tag
                    Velprakash Community Member

                    Hi,

                           Thanks a lot Jeff...

                            I'm doing a huge program with lot of tasks. While doing that,my mind didn't strike to check if the tag is not already exist, then only to create new tag...  Thanks for all of the indesigner experts for their replies...

                    I used the following codes with ur idea.

                     

                                        var renamewith = textbox1.text;

                                       

                                        try

                                        {

                                            alert(app.activeDocument.xmlTags.item(renamewith).name);

                                            if(app.activeDocument.xmlTags.item(renamewith).name)

                                            {

                                               var testTag = app.activeDocument.xmlTags.item(renamewith);

                                            }

                                        }

                                        catch(e)

                                         {

                                            var testTag = app.activeDocument.xmlTags.add({name: renamewith});

                                         }

                     

                     

                    With Regards,

                    Vel.

                    • 7. Re: Rename particular XML Tag
                      absqua Community Member

                      I'm glad it helped. But the try/catch seems unnecessary here; the isValid property was provided for just this kind of check. I used the ternary operator construct, but in a normal if/then it would look like:

                       

                      var testTag;
                      if (app.activeDocument.xmlTags.item(renamewith).isValid) {
                          testTag = app.activeDocument.xmlTags.item(renamewith);
                      }
                      else {
                          testTag = app.activeDocument.xmlTags.add({name: renamewith});
                      }
                      

                       

                      But maybe this is only part of what you have in your try/catch in your complete script...

                      • 8. Re: Rename particular XML Tag
                        Velprakash Community Member

                        Once again I thank u for ur reply Jeff. Actually I understand the ternary operator is single step to overcome if else statement. But, In my case,if I use isValid? condition it's showing error, "Object is Invalid".So I used it as I mentioned in my previous reply. As ur guess, it's little part of my whole script...

                         

                         

                        With Regards,

                        Vel.

                        • 9. Re: Rename particular XML Tag
                          Velprakash Community Member

                          Hi Jeff,

                              I've an another issue. Can u guide me... I posted my issue in forum. Please find the following Url:

                          http://forums.adobe.com/thread/1030913

                           

                          Thanks and Regards,

                          Vel.