Skip to main content
Keith_Gilbert
Inspiring
July 30, 2019
Answered

E4X element manipulation

  • July 30, 2019
  • 4 replies
  • 1183 views

I'm trying to use E4X to do some HTML cleanup and manipulation, and am missing something basic I think.

Given:

var myXML = <html>

    <head>

        <title>body</title>

    </head>

    <body id="body" lang="en-US">

       <div>

            <div class="Chapter">

                <h2>

                    Summary

                </h2>

                <p>

                    asdf jaskdlf jksdlf jkdsal fjklaf

                </p>

                <p>

                    jkdlf; ajskdfl ;asjkfl sajdfk ;adjskfl;

                </p>

            </div>

        </div>

    </body>

</html>;

var myChapter = myXML.body.div.div.(attribute("class")=="Chapter");

var myParent = myChapter.parent();

I've figured out how to locate elements and attributes in the XML. But I can't seem to move the content in the XML. For example, I'd like to move the <div class="Chapter"> element and it's children in place of the parent <div> (effectively removing the redundant div) but can't figure it out. I've wrestled with insertChildBefore() and replace(), but can't seem to work it out.

Oh great minds...any suggestions?

This topic has been closed for replies.
Correct answer Sunil Yadav

Well Upto now I've not encountered any exception yet in previous code as well, but there might be chance of exception.

You can loop this other way as well like this and keep on refreshing allChapters variable to avoid any exception:

var myXML = <html> 

 

    <head> 

        <title>body</title> 

    </head> 

    <body id="body" lang="en-US"> 

       <div> 

            <div class="Chapter"> 

                <h2> 

                    Summary 

                </h2> 

                <p> 

                    asdf jaskdlf jksdlf jkdsal fjklaf 

                </p> 

                <p> 

                    jkdlf; ajskdfl ;asjkfl sajdfk ;adjskfl; 

                </p> 

            </div> 

        </div> 

    </body> 

</html>;

////////// insert All childrens here after that delete those elements ----

var allChapters = myXML.body.div.div.(attribute("class")=="Chapter");

for(var j = 0; j < allChapters.length(); j){

    var myChapter = allChapters;

    for(var i = myChapter.elements().length()-1; i >=0 ; i--){

        myChapter.parent().insertChildAfter(myChapter, myChapter.elements());

        }

    delete allChapters;

    //After deleting element find again because there might be chance of allChapters variable could be undefined or invalid---

    allChapters = myXML.body.div.div.(attribute("class")=="Chapter");

    }

alert(myXML);

Best

Sunil

4 replies

Keith_Gilbert
Inspiring
August 1, 2019

Thanks everyone for the help! Thankfully, in this particular application, I can assume the HTML will be valid XML. This got me moving in the right direction. Looking at Sunil's helpful routine made me realize I had misunderstood how insertChildAfter functioned.

TᴀW
Legend
July 31, 2019

Hi Keith,

It's worth remembering, though, that HTML is not always going to be valid XML.

https://stackoverflow.com/questions/5558502/is-html5-valid-xml

Ariel

id-extras.com | InDesign tools & scripts for typesetters, form designers, and translators
Sunil Yadav
Sunil YadavCorrect answer
Legend
July 31, 2019

Well Upto now I've not encountered any exception yet in previous code as well, but there might be chance of exception.

You can loop this other way as well like this and keep on refreshing allChapters variable to avoid any exception:

var myXML = <html> 

 

    <head> 

        <title>body</title> 

    </head> 

    <body id="body" lang="en-US"> 

       <div> 

            <div class="Chapter"> 

                <h2> 

                    Summary 

                </h2> 

                <p> 

                    asdf jaskdlf jksdlf jkdsal fjklaf 

                </p> 

                <p> 

                    jkdlf; ajskdfl ;asjkfl sajdfk ;adjskfl; 

                </p> 

            </div> 

        </div> 

    </body> 

</html>;

////////// insert All childrens here after that delete those elements ----

var allChapters = myXML.body.div.div.(attribute("class")=="Chapter");

for(var j = 0; j < allChapters.length(); j){

    var myChapter = allChapters;

    for(var i = myChapter.elements().length()-1; i >=0 ; i--){

        myChapter.parent().insertChildAfter(myChapter, myChapter.elements());

        }

    delete allChapters;

    //After deleting element find again because there might be chance of allChapters variable could be undefined or invalid---

    allChapters = myXML.body.div.div.(attribute("class")=="Chapter");

    }

alert(myXML);

Best

Sunil

Loic.Aigon
Legend
July 31, 2019

Or you could use xslt either by import/export XML with a specific xsl file or calling xslt/saxon per command line.

XSL may be clearer for important XML repurposing. At some level of complexity, I guess this is how I would tackle things.

Sunil Yadav
Legend
July 31, 2019

Hi,

Try this code,

var myXML = <html>

    <head>

        <title>body</title>

    </head>

    <body id="body" lang="en-US">

       <div>

            <div class="Chapter">

                <h2>

                    Summary

                </h2>

                <p>

                    asdf jaskdlf jksdlf jkdsal fjklaf

                </p>

                <p>

                    jkdlf; ajskdfl ;asjkfl sajdfk ;adjskfl;

                </p>

            </div>

        </div>

    </body>

</html>;

////////// insert All childrens here after that delete those elements ----

var allChapters = myXML.body.div.div.(attribute("class")=="Chapter");

for(var j = allChapters.length()-1; j >= 0; j--){

    var myChapter = allChapters;

    for(var i = myChapter.elements().length()-1; i >=0 ; i--){

        myChapter.parent().insertChildAfter(myChapter, myChapter.elements())

        }

    delete allChapters;

    }

alert(myXML);

Best

Sunil