5 Replies Latest reply on Oct 8, 2015 7:51 AM by indesignp36139849

    Indesign Javascript and xml structure

    smgaetano

      Hi to all

      I am new to program javascript in Indesign, and I am looking for some example on how modify the xml structure inside a document using a javascript script.

       

      i.e.

      if I have a structure like this in the original document:

      root

      section

      tag1

      tag2

      tag3

       

      I need to transform it in this way( this is just an example, may be more complex):

      root

           section

                tag1

                tag2

                tag3

       

      Any help on where or how to solve this is very appreciate.

      Thanks a lot

        • 1. Re: Indesign Javascript and xml structure
          absqua Level 4

          Download the SDK. There is somewhere within it a folder of sample scripts that show how to manipulate a document's xml structure.

           

          Jeff

          • 2. Re: Indesign Javascript and xml structure
            smgaetano Level 1

            Thanks, I have already done.

            But I have not found enought info on solving my problems.

            This is why I am looking here for an help.

             

             

            • 3. Re: Indesign Javascript and xml structure
              absqua Level 4

              I think the MoveXMLElement.jsx script there shows pretty well what's involved. Here's something that addresses something closer to your example:

               

              var root = app.activeDocument.xmlElements[0];
              root.xmlElements.item("tag1").move(LocationOptions.AT_END, root.xmlElements.item("section"));
              root.xmlElements.item("tag2").move(LocationOptions.AT_END, root.xmlElements.item("section"));
              root.xmlElements.item("tag3").move(LocationOptions.AT_END, root.xmlElements.item("section"));
              

               

              You want to be careful about the order you do things and when you grab references to the xml structure objects, since you're changing them as you go (i.e. root.xmlElements[3] might refer to tag3 when you start, but will be invalid once you start moving things into section).

               

              If the xml structure in your InDesign document is the result of an xml import, you might consider transforming the xml as you import it. It's faster and more natural there.

               

              Hope that helps,

              Jeff

              • 4. Re: Indesign Javascript and xml structure
                liedzeit Level 2

                This is a bit clumsy but maybe you get the idea:

                 

                var root=app.activeDocument.xmlElements[0];//"real" Root

                var myroot=root.xmlElements.itemByName("root");

                var section=root.xmlElements.itemByName("section");

                var t1=root.xmlElements.itemByName("tag1");

                 

                section.move(LocationOptions.AT_BEGINNING,myroot);

                section=myroot.xmlElements.itemByName("section");

                t1.move(LocationOptions.AT_BEGINNING,section);

                t1=section.xmlElements.itemByName("tag1");

                var t2=root.xmlElements.itemByName("tag2");

                t2.move(LocationOptions.AFTER,t1);

                 

                 

                Ralf

                • 5. Re: Indesign Javascript and xml structure
                  indesignp36139849 Level 1

                  Could some one please let me know if i can add a new xmlTag between to existed xmlTags  using insertionPoints ?

                   

                   

                  A textFrame is holding two tags ,

                   

                  Tag1

                  Tag2

                   

                  now i would like to place Tag3 between this two tags in the same textFrame.