4 Replies Latest reply on Jun 29, 2015 7:31 AM by Loic.Aigon

    Merging xml nodes

    Loic.Aigon Adobe Community Professional

      Hi all,

       

      I am inheriting XML files where carriage returns create redundant nodes. That is to say that what should be :

       

      <tag>A

      B

      C

      D

      </tag>

       

      is actually

       

      <tag>A</tag>

      <tag>B</tag>

      <tag>C</tag>

      <tag>D</tag>


      I can certainly look for these tags and do the merging somehow but I wanted to double check if there could be any generic way of merging those kind of repeated nodes. Unfortunately XPath implementation won't allow me to use something like following-siblings::tag


      Any idea ?


      Loic

        • 1. Re: Merging xml nodes
          drerol74 Level 2

          Hi Loic,

           

          for me following-sibling works both with evaluateXpath and during XML import via XSL transformation!?

           

          e.g. evaluateXPathExpression("./tag/following-sibling::tag");

           

          Roland

          • 2. Re: Merging xml nodes
            Loic.Aigon Adobe Community Professional

            Hi Roland,

             

            That's good to know. It doesn't seem to work on XML.xpath though. But your input is helpful as I can envision to import XML in a dummy document and do the merging then export again. I wish I could remain on a pure js object though.

             

            Thanks a lot,

             

            Loic

            • 3. Re: Merging xml nodes
              drerol74 Level 2

              Hi Loic,

               

              I have not read your post carefully enough.

               

              I tried that:

               

               

              var _xmlString = "<root><h1>Bold</h1><h2>Bold</h2><tag>A</tag><tag>B</tag><tag>C</tag><tag>D</tag><strong>Strong</strong></root>",
                  _XML = new XML(_xmlString),
                  _followingTags = _XML.xpath("./tag[1]/following-sibling::tag"),
                  _destTag = _XML.child("tag")[0];
              
              _destTag.insertChildAfter(_destTag.text(),_followingTags.text());
              
              for(var i = _followingTags.length() -1; i >= 0; i-- ) {
                delete _followingTags[i];
              }
              
              $.writeln(_XML);
              

               

              Result:

               

              <root>
                <h1>Bold</h1>
                <h2>Bold</h2>
                <tag>
                  A
                  B
                  C
                  D
                </tag>
                <strong>Strong</strong>
              </root>
              

               

              Perhaps a different approach, but I do not know how the structure looks exactly..

               

              Roland

              • 4. Re: Merging xml nodes
                Loic.Aigon Adobe Community Professional

                Hi,

                 

                Well looks like following-siblings is working with xpath after all. I may have misused it.

                 

                Thanks for your obstination

                 

                Loic