How to force removal of attributes with implied default values from DTD in Java XML DOM

佐手、 提交于 2020-03-25 18:24:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!