2 Replies Latest reply on Mar 16, 2015 5:28 PM by beh_gras_beh

    Rearranging XML structure

    beh_gras_beh Level 1

      Noob question,

       

      Can you rearrange an XML structure with XSLT or scripting?

       

      Lets say I've got an clean XML structure like this:

       

      <Workbook>
           <Element_A_01>
                <Element_A_02></Element_A_02>
           </Element_A_01>
           <Element_A_03></Element_A_03>
           <Element_A_04></Element_A_04>
      
      
           <Element_B_01>
                <Element_B_02></Element_B_02>
           </Element_B_01>
           <Element_B_03></Element_B_03>
           <Element_B_04></Element_B_04>
      </Workbook>
      
      


      Things got a bit messy and the structure turns suddenly to this.

       

      <Workbook>
           <Element_A_01>
                <Element_A_02></Element_A_02>
           </Element_A_01>
           <Element_B_01>
                <Element_B_02></Element_B_02>
           </Element_B_01>
           <Element_A_03></Element_A_03>
           <Element_A_04></Element_A_04>
           <Element_B_03></Element_B_03>
           <Element_B_04></Element_B_04>
      </Workbook>
      
      

       

      Can I go back tot the original XML structure with XSLT or scripting to find a tag and place it in the way like the original clean version?

       

      Greetings from Holland

        • 1. Re: Rearranging XML structure
          Dirk Becker Level 4
          
          

          Assuming the structure is within a document:

           

          You can export and re-import the XML, and on the way apply an XSLT. This will only strip all layout information such as text attributes, and a first XSLT will take some time.

           

          You can also use a script, e.g.

          var rootXE = app.activeDocument.xmlElements[0];  
          var wbXEs = rootXE.evaluateXPathExpression("//Workbook");
          var wbXE = wbXEs[0];
          var a1XE = wbXE.xmlElements.item("Element_A_01");
          var a3XE = wbXE.xmlElements.item("Element_A_03");
          a3XE.move(LocationOptions.AFTER,a1XE);
          // refresh a3XE
          var a3XE = wbXE.xmlElements.item("Element_A_03");
          var a4XE = wbXE.xmlElements.item("Element_A_04");
          a4XE.move(LocationOptions.AFTER,a3XE);
          

           

          That script does not consider the spacing between A4 and B1.

          • 2. Re: Rearranging XML structure
            beh_gras_beh Level 1

            Thanks for the reply, it's an clear answer.


            I was hoping it could be done with XSLT and keep the text attributes, but maybe the script is an better answer I am looking for.

            The main problem is that the structure sometimes scrambled up in a nun logical way (Don't ask why, thats's a long story ).

            So what I need to do is to find the tag and place it back in the original state and it seems you're script is doing that.


            I will look in to the script tomorrow, going to sleep now (it's 01:19 here).


            Again, thanks for the help!