Michael Glavassevich wrote:
Is there any way to automatically force redunant namespace declarations
to be removed from the child elements when they are serialized?
Calling Document.normalizeDocument() with the "namespace-declarations" [1]
parameter set to false will remove all the namespace declarations from the
DOM. You could then add back the ones you really wanted and then serialize
the document. To do this more efficiently you'd have to traverse the DOM
yourself.
[1]
http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#parameter-namespace-declarations
Thanks! That is what I already did. I use simple recursive procedure to
remove all redunant namespace declarations.
---8<------------------------------
public static void normalizeNamespaces( Document document )
{
normalizeNamespaces(
document.getDocumentElement(),
new HashSet<String>() );
}
private static void normalizeNamespaces(
Element element,
Set<String> namespaces )
{
NamedNodeMap attributes = element.getAttributes();
for ( int i = 0; i < attributes.getLength(); i++ )
{
Attr attribute = ( Attr ) attributes.item( i );
if ( "http://www.w3.org/2000/xmlns/".equals(
attribute.getNamespaceURI() ) )
{
boolean newNS = namespaces.add( attribute.getValue() );
if ( !newNS )
{
element.removeAttributeNode( attribute );
}
}
}
// invoke recursively for children elements
NodeList childNodes = element.getChildNodes();
for ( int i = 0; i < childNodes.getLength(); ++i )
{
Node n = childNodes.item( i );
if ( n instanceof Element )
{
normalizeNamespaces( ( Element ) n, namespaces );
}
}
}
--->8------------------------------
Thanks again!
--
Igor Lobanov
Internal Development Engineer
SWsoft, Inc.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]