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:
<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?
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
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>
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
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
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.
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
Copy link to clipboard
Copied
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.