I am trying to delete the XML attributes which is having empty values am using E4X javascript need to achieve. Please help me on this. Code:
function xmlparse(xml) {
var children = xml.*, attributes = xml.@*, length = children.length();
for each (var child in children) {
if(child.hasComplexContent())
{
for each (var chi in child.children()) {
var c= chi.localName()
if(chi.hasSimpleContent())
{
if(chi.@value == "")
{
delete chi
}
}
}
var obj = xmlparse(child)
}
}
}
Input:
<Test>
<id value="123"/>
<Book>
<source>
<English>
<bookid value=""/>
<version>
<type>
<place>
<author value="Test123"/>
<index value="10"/>
<display value=""/>
</place>
</type>
</version>
</English>
</source>
</Book>
<Book>
<source>
<German>
<bookid value=""/>
<version>
<type>
<place>
<author value="Test143"/>
<index value=""/>
<display value="Helo"/>
</place>
</type>
</version>
</German>
</source>
</Book>
</Test>
Expected Output:
<Test>
<id value="123"/>
<Book>
<source>
<English>
<version>
<type>
<place>
<author value="Test123"/>
<index value="10"/>
</place>
</type>
</version>
</English>
</source>
</Book>
<Book>
<source>
<German>
<version>
<type>
<place>
<author value="Test143"/>
<display value="Helo"/>
</place>
</type>
</version>
</German>
</source>
</Book>
</Test>
Generally, I need to remove the value of the empty tag from the XML. Please guide me on this i tried with some sample code it doesn't work for me.
Since you want to perform the same check on every xml node, there is no need to recursively iterate through them. The descendants method will get all of them at once. Checking the hasSimpleContent method of the child ensures that it has no child elements.
function deleteEmptyElements(xml) {
for each (var child in xml.descendants()) {
if (child.hasSimpleContent() && child.@value.toString() == '') {
delete child[0];
}
}
}