问题
As I reported elsewhere on Stack Overflow, I'm parsing a legacy modular XHTML 1.1 document and the DTD is adding all sorts of default attributes such as version="-//W3C//DTD XHTML 1.1//EN"
. Some of these are even inappropriate, such as xml:space="preserve"
.
I'm writing a utility to clean up the DOM after parsing, but I forgot that the DOM will automatically add back default attributes from the DTD if I remove them. So if I call Element.removeAttributeNS(null, "version")
on the document element, for example, it just adds back version="-//W3C//DTD XHTML 1.1//EN"
and I'm back where I started.
How can I force the DOM in Java to remove an attribute, even if the DTD indicates that attribute has an implied value? Or how can I just change/remove the DTD from the DOM tree so that implied attributes don't automatically show up?
I have succeeded in created a new, empty document without a DTD; importing the old document element and its descendants; and then replacing the root element in the new document with the imported element tree, but this is a lot of overhead and too inefficient. Is there a more efficient workaround or solution?
来源:https://stackoverflow.com/questions/60688446/how-to-force-removal-of-attributes-with-implied-default-values-from-dtd-in-java