8 Replies Latest reply on Jul 24, 2011 8:24 AM by John Hawkinson

    Move xml Elements

    Mac_06 Level 2

      Hi

       

      I'm quite loose in scripting with xml structure. I have a requirement to move multiple elements in parent tag.

      e.g.

       

      <article>
           <ect>
           <start>
           <title>
           <p>
           <p>
           <p>
           <p>
           <title2>
           <p>
           <p>
           <p>
           <title3>
           <p>
           <p>
           <p>
           <title>
           <p>
           <p>
           <p>
           <p>
           <end>
           <body>
      
                
      

          

      In the above Indesign xml strcture you could see <body> tag at last my woried is to move all tags after <start> and before <end> into the <body> tag.

      So required structure should be-:

       

      <article>
           <ect>
           <start>
           <body>
                <title>
                <p>
                <p>
                <p>
                <p>
                <title2>
                <p>
                <p>
                <p>
                <title3>
                <p>
                <p>
                <p>
                <title>
                <p>
                <p>
                <p>
                <p>            
           <end>
      
      
      

       

       

      Thanks for reading the Thread

       

      Mac

        • 1. Re: Move xml Elements
          Techi Panda Level 2

          Hi Mac

           

          You can move XML tags Using XSLT while you import the XML file in to indesignxslt.JPG

          • 2. Re: Move xml Elements
            John Hawkinson Level 5

            True. You can also write a script that uses the XMLElement.move() method to reposition.

            There are lots of ways to do it!

            • 3. Re: Move xml Elements
              Mac_06 Level 2

               

              Thanks to both of you to take interest in issue.

               

              I am not importing any xml to my documents while I am creating temporary xml structure using style to tag, also though I am quite unfamiliar with xslt but still I tried that approach but not able to achieve my goal, so looking to script solution for this.

               

              John wrote:

              >  True. You can also write a script that uses the XMLElement.move() method to reposition.

              >  There are lots of ways to do it!

               

              Correct but, xmlElements.move(), move specific xml elements but I need to move the elements base on some condition as after <start> tag and before<end> tag whatever are there should be moved inside <body> tag.

               

              Could any of suggest the fragment to achieve this?

               

              Many Thanks

              Mac

              • 4. Re: Move xml Elements
                John Hawkinson Level 5

                I don't see what the difficulty is...

                First search out your start, end, and body nodes. You can traverse the tree yourself to do so, or use evaluateXPathExpression, or whatever.

                Then go through the tree again, and after you hit the start node, start moving nodes to be inside the body node. Perhaps you'll want to go in reverse order so that the ordering is preserved.

                 

                Is there a reason that doesn't work for you?

                • 5. Re: Move xml Elements
                  Mac_06 Level 2

                  I've done some thing like below:

                   

                  var Doc = app.activeDocument;
                  var Root = Doc.xmlElements.item(0);
                  var startElement = [];
                  var listElements = ElementsArray()
                  var Body = Root.xmlElements.add("body")
                  var che = Body.xmlElements.add("ref")
                  for(var k=0; k<listElements.length; k++){
                          Root.xmlElements.item(listElements[k]).move(LocationOptions.after, )
                      }
                  function ElementsArray()
                  {
                      for(var i=1; i<Root.xmlElements.length; i++)
                      {
                         if(Root.xmlElements[i].markupTag.name =="start")
                         {
                             var myStartElement = Root.xmlElements[i].markupTag.name
                              for(var j=i; j<Root.xmlElements.length; j++){
                                  startElement.push (Root.xmlElements[j].markupTag.name)
                                   if(Root.xmlElements[j].markupTag.name == "end")
                                   return startElement;                
                                  }       
                            }
                      }
                  }
                  

                   

                  But here I am getting couple of issue.

                  1. when I use loop to move xml elements it moves all the emements with item name in single iteration So I loose element position while I need to preserve the element position also.
                  2. For reference purpose I have to create an element "ref". since without reference xmlElement can not be moved inside the other elements. Please suggest if there any other way to move the elements.
                  3. Also it throughs an error while moving elements since listElement array contains a number of elements which are already moved
                  • 6. Re: Move xml Elements
                    John Hawkinson Level 5

                    Sorry for the delay.

                    You did not make it very easy to help you. For instance, your XML as posted is not valid XML (unclosed tags), so it cannot be imported into InDesign.

                     

                    Your script doesn't make a lot of sense. You generate listElements as an array of names of tags. But since a tag's name is not a unique way to refer to it, it's not an effective approach.

                     

                    Also, by convention, in JavaScript, we use camelCase. That means root,body,startElement,doc, not Root, Body, StartElement, and Doc.

                     

                    In your script, you create a body element. But your supplied XML already had one.

                     

                    Anyhow, there's no need to loop, you can just move the plural object. So it is really as simple as this:

                     

                    var
                        doc = app.activeDocument,
                        root = doc.xmlElements.item(0),
                        firstElement = root.xmlElements.nextItem(
                            root.evaluateXPathExpression("start")[0]),
                        lastElement = root.xmlElements.previousItem(
                            root.evaluateXPathExpression("end")[0]),
                        bodyElement = root.evaluateXPathExpression("body")[0];
                        movingElements = root.xmlElements.
                            itemByRange(firstElement,lastElement);
                    movingElements.move(LocationOptions.AT_END, bodyElement)
                    
                    • 7. Re: Move xml Elements
                      Mac_06 Level 2

                      Oops…, again in hurry did share unwanted info,

                       

                      John wrote

                       

                      >Sorry for delay

                       

                      It's OK John after all you are just helping me.

                       

                      After modify couple of line code I were able to achieve my goal

                       

                       

                      var

                          doc = app.activeDocument,

                          root = doc.xmlElements.item(0),

                          firstElement = root.xmlElements.nextItem(

                              root.xmlElements.itemByName("start")),

                          lastElement = root.xmlElements.previousItem(

                              root.xmlElements.itemByName("end")),

                          bodyElement = root.xmlElements.itemByName("body"),

                          movingElements = root.xmlElements.

                              itemByRange(firstElement,lastElement);

                      movingElements.move(LocationOptions.AT_END, bodyElement)

                       

                       

                      since xmlElements.evaluateXPathExpression(String XPath) returns xmlItem not xmlElemtents So it was giving me error.

                       

                      Many Thanks John once agian you save my tons of time.

                       

                      Mac

                      • 8. Re: Move xml Elements
                        John Hawkinson Level 5

                        Your last message is blank, except for personal information you probably did not want to include. Please use the web interface to edit your post and remove that. And perhaps using the web interface to reply is best anyhow, since I suspect your post is blank because of an email formatting issue?