After receiving the SVG document through XHR, you will have a separate XML document in the xhr.responseXML property. Since you cannot legally move nodes from one document to another, you will need to import the part you want from one document into the target document before you can use it as part of this document.
The easiest way to do this is to use document.importNode() :
var clone = document.importNode(nodeFromAnotherDoc,true);
However, this does not work for IE9 . To work around this error, you can alternatively use this function to recursively recreate the node hierarchy in the selected document:
function cloneToDoc(node,doc){ if (!doc) doc=document; var clone = doc.createElementNS(node.namespaceURI,node.nodeName); for (var i=0,len=node.attributes.length;i<len;++i){ var a = node.attributes[i]; clone.setAttributeNS(a.namespaceURI,a.nodeName,a.nodeValue); } for (var i=0,len=node.childNodes.length;i<len;++i){ var c = node.childNodes[i]; clone.insertBefore( c.nodeType==1 ? cloneToDoc(c,doc) : doc.createTextNode(c.nodeValue), null ); } return clone; }
You can see an example of using XHR to extract an SVG document and both node import methods on my website: http://phrogz.net/SVG/fetch_fragment.svg
source share