• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

E4X element manipulation

Contributor ,
Jul 30, 2019 Jul 30, 2019

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

891

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Advocate , Jul 30, 2019 Jul 30, 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 children

...

Votes

Translate

Translate
Advocate , Jul 30, 2019 Jul 30, 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> 

              

...

Votes

Translate

Translate
Advocate ,
Jul 30, 2019 Jul 30, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 30, 2019 Jul 30, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jul 31, 2019 Jul 31, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jul 31, 2019 Jul 31, 2019

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 31, 2019 Jul 31, 2019

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines