sourceElement = ((Document)sourceNode).getDocumentElement();
}else if(sourceNode.getNodeType() == Node.ELEMENT_NODE){
sourceElement = (Element)sourceNode;
}
NamespaceResolver sourceNR = new NamespaceResolver();
sourceNR.setDOM(sourceElement);
NamespaceResolver targetNR = new NamespaceResolver();
targetNR.setDOM(targetNode);
String prefix = getPrefix(namespace, sourceNR, sourceElement);
if(prefix != null && prefix.length() >0){
xmlRootQualifiedName = prefix + ':' + name;
}
if(null == xmlRootQualifiedName && null != sourceElement) {
namespace = sourceElement.getNamespaceURI();
xmlRootQualifiedName = sourceElement.getNodeName();
}
if(xmlRootQualifiedName != null) {
Element newElement = null;
if(null == namespace) {
newElement = targetDoc.createElement(xmlRootQualifiedName);
if(null != targetNR.getDefaultNamespaceURI()) {
newElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE, Constants.EMPTY_STRING);
}
} else {
newElement = targetDoc.createElementNS(namespace, xmlRootQualifiedName);
if(null == prefix) {
prefix = newElement.getPrefix();
}
}
if(prefix != null && prefix.length() >0 && targetNR.resolveNamespaceURI(namespace) == null){
newElement.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + ':' + prefix, namespace);
}
targetNode.appendChild(newElement);
copyAttributes(sourceElement.getAttributes(),sourceElement, newElement, sourceNR, targetNR);