drools-eclipse/org.guvnor.eclipse.webdav/src/interface/org/eclipse/webdav/dom/ElementEditor.java
public static Node reduceNS(Node node, Namespaces parentNamespaces) throws MalformedElementException {
        Namespaces namespaces = parentNamespaces;

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            namespaces = getNamespaces(element, parentNamespaces, false);

            String nsPrefix = getNSPrefix(element);
            String nsLocalName = getNSLocalName(element);

            if (nsPrefix != null) {
                String nsName = namespaces.getNSName(nsPrefix);
                ensureNotNull(Policy.bind("ensure.missingNamespaceForPrefix", nsPrefix), nsName); //$NON-NLS-1$

                String tagName = null;
                if (nsName.equals(namespaces.getDefaultNSName())) {
                    tagName = nsLocalName;
                } else {
                    tagName = namespaces.getNSPrefix(nsName) + ":" + nsLocalName; //$NON-NLS-1$
                }

                if (!tagName.equals(element.getTagName())) {
                    Document document = element.getOwnerDocument();
                    Element newElement = document.createElement(tagName);
                    NamedNodeMap nodeMap = element.getAttributes();
                    for (int i = 0; i < nodeMap.getLength(); ++i) {
                        Attr attr = (Attr) nodeMap.item(i);
                        newElement.setAttribute(attr.getName(), attr.getValue());
                    }
                    Node child = element.getFirstChild();
                    while (child != null) {
                        element.removeChild(child);
                        newElement.appendChild(child);
                        child = element.getFirstChild();
                    }
                    element.getParentNode().replaceChild(newElement, element);
                    element = newElement;
                }
            }

            Vector oldAttributes = new Vector();
            Vector newAttributes = new Vector();
            NamedNodeMap nodeMap = element.getAttributes();
            for (int i = 0; i < nodeMap.getLength(); ++i) {
                Attr attr = (Attr) nodeMap.item(i);
                String name = attr.getName();
                String value = attr.getValue();
                String newName = name;
                nsPrefix = getNSPrefix(name);
                nsLocalName = getNSLocalName(name);

                if (nsPrefix != null && !nsPrefix.equals(XML_PREFIX)) {
                    String nsName = namespaces.getNSName(nsPrefix);
                    ensureNotNull(Policy.bind("ensure.missingNamespaceForPrefix", nsPrefix), nsName); //$NON-NLS-1$
                    String newNSPrefix = namespaces.getNSPrefix(nsName);
                    if (!newNSPrefix.equals(nsPrefix)) {
                        newName = newNSPrefix + ":" + nsLocalName; //$NON-NLS-1$
                    }
                }

                boolean newAttribute = true;
                if (parentNamespaces != null) {
                    if (nsPrefix == null && XML_PREFIX.equals(nsLocalName)) {
                        if (value.equals(parentNamespaces.getDefaultNSName())) {
                            newAttribute = false;
                        }
                    }
                    if (nsPrefix != null && XML_PREFIX.equals(nsPrefix)) {
                        if (parentNamespaces.getNSPrefix(value) != null) {
                            newAttribute = false;
                        }
                    }
                }

                oldAttributes.addElement(attr);
                if (newAttribute) {
                    newAttributes.addElement(new String[] {newName, value});
                }
            }

            Enumeration oldAttrs = oldAttributes.elements();
            while (oldAttrs.hasMoreElements()) {
                element.removeAttributeNode((Attr) oldAttrs.nextElement());
            }

            Enumeration newAttrs = newAttributes.elements();
            while (newAttrs.hasMoreElements()) {
                String[] newAttr = (String[]) newAttrs.nextElement();
                element.setAttribute(newAttr[0], newAttr[1]);
            }

            node = element;
        }

        Node child = node.getFirstChild();
        while (child != null) {
            child = reduceNS(child, namespaces);
            child = child.getNextSibling();
        }

        return node;
    }
public static Node reduceNS(Node node, Namespaces parentNamespaces) throws MalformedElementException {
        Namespaces namespaces = parentNamespaces;

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            namespaces = getNamespaces(element, parentNamespaces, false);

            String nsPrefix = getNSPrefix(element);
            String nsLocalName = getNSLocalName(element);

            if (nsPrefix != null) {
                String nsName = namespaces.getNSName(nsPrefix);
                ensureNotNull(Policy.bind("ensure.missingNamespaceForPrefix", nsPrefix), nsName); //$NON-NLS-1$

                String tagName = null;
                if (nsName.equals(namespaces.getDefaultNSName())) {
                    tagName = nsLocalName;
                } else {
                    tagName = namespaces.getNSPrefix(nsName) + ":" + nsLocalName; //$NON-NLS-1$
                }

                if (!tagName.equals(element.getTagName())) {
                    Document document = element.getOwnerDocument();
                    Element newElement = document.createElement(tagName);
                    NamedNodeMap nodeMap = element.getAttributes();
                    for (int i = 0; i < nodeMap.getLength(); ++i) {
                        Attr attr = (Attr) nodeMap.item(i);
                        newElement.setAttribute(attr.getName(), attr.getValue());
                    }
                    Node child = element.getFirstChild();
                    while (child != null) {
                        element.removeChild(child);
                        newElement.appendChild(child);
                        child = element.getFirstChild();
                    }
                    element.getParentNode().replaceChild(newElement, element);
                    element = newElement;
                }
            }

            Vector oldAttributes = new Vector();
            Vector newAttributes = new Vector();
            NamedNodeMap nodeMap = element.getAttributes();
            for (int i = 0; i < nodeMap.getLength(); ++i) {
                Attr attr = (Attr) nodeMap.item(i);
                String name = attr.getName();
                String value = attr.getValue();
                String newName = name;
                nsPrefix = getNSPrefix(name);
                nsLocalName = getNSLocalName(name);

                if (nsPrefix != null && !nsPrefix.equals(XML_PREFIX)) {
                    String nsName = namespaces.getNSName(nsPrefix);
                    ensureNotNull(Policy.bind("ensure.missingNamespaceForPrefix", nsPrefix), nsName); //$NON-NLS-1$
                    String newNSPrefix = namespaces.getNSPrefix(nsName);
                    if (!newNSPrefix.equals(nsPrefix)) {
                        newName = newNSPrefix + ":" + nsLocalName; //$NON-NLS-1$
                    }
                }

                boolean newAttribute = true;
                if (parentNamespaces != null) {
                    if (nsPrefix == null && XML_PREFIX.equals(nsLocalName)) {
                        if (value.equals(parentNamespaces.getDefaultNSName())) {
                            newAttribute = false;
                        }
                    }
                    if (nsPrefix != null && XML_PREFIX.equals(nsPrefix)) {
                        if (parentNamespaces.getNSPrefix(value) != null) {
                            newAttribute = false;
                        }
                    }
                }

                oldAttributes.addElement(attr);
                if (newAttribute) {
                    newAttributes.addElement(new String[] {newName, value});
                }
            }

            Enumeration oldAttrs = oldAttributes.elements();
            while (oldAttrs.hasMoreElements()) {
                element.removeAttributeNode((Attr) oldAttrs.nextElement());
            }

            Enumeration newAttrs = newAttributes.elements();
            while (newAttrs.hasMoreElements()) {
                String[] newAttr = (String[]) newAttrs.nextElement();
                element.setAttribute(newAttr[0], newAttr[1]);
            }

            node = element;
        }

        Node child = node.getFirstChild();
        while (child != null) {
            child = reduceNS(child, namespaces);
            child = child.getNextSibling();
        }

        return node;
    }
Removes redundant namespace declarations from the given node and all
its children to maximum depth. The node must not be
<code>null</code>.

@param node the node whose namespace declarations are to be reduced
@return     the node, or a new node (element) with an equivalent
qualified name but whose tag name has changed
@throws     MalformedElementException if the name of the given node
(element), or one of its attributes, has a namespace
prefix which could not be resolved