you can find the answer to the white space question, this is a limitation of the Saxon XML processor at the link, https://www.mathworks.com/matlabcentral/answers/94189-why-does-xmlread-introduce-extra-whitespace-into-my-xml-file [mathworks.com].
as for the second question you can use the script below:
import matlab.io.xml.dom.*
import matlab.io.xml.xpath.*
docA = parseFile(Parser,'a.xml');
stripEmptyTextNodes(docA);
config = evaluate(Evaluator,'/Configuration',docA);
modType = evaluate(Evaluator,'ModuleType',config);
removeChild(config,modType);
writer = DOMWriter;
writer.Configuration.FormatPrettyPrint = true;
writeToFile(writer,docA,'a1.xml');
function stripEmptyTextNodes(node)
import matlab.io.xml.dom.*
nodelist = getChildNodes(node);
i = 0;
while i < getLength(nodelist)
child = item(nodelist,i);
if getNodeType(child) == node.TEXT_NODE
if isempty(strtrim(getTextContent(child)))
child.getParentNode().removeChild(child);
i = i-1;
end
end
i = i+1;
stripEmptyTextNodes(child);
end
end