I want to transform HTML nodes from a XHTML page with an XSL stylesheet.
here's the function I use:
function XSLT(xml,xsl) {
var tmpElt = document.createElement("div");
if (window.ActiveXObject){
//Version IE
tmpElt.innerHTML = xml.transformNode(xsl);
}else{
//Version ECMA
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
var result = xsltProcessor.transformToFragment(xml, document);
var serializer = new XMLSerializer();
var str = serializer.serializeToString( result );
tmpElt.innerHTML = str;
}
return tmpElt;
}
this will work on chrome as long as xml is not an HTMLElement. precisely, this line:
var result = xsltProcessor.transformToFragment(xml, document);
returns null on chromium[most of the time]. On Firefox it behaves properly.
I tried this workaround:
- serialize xml with outerHTML ( and even with XMLSerializer::serializeToString() )
- Parse it with a new DOMParser()
- XSLT the result
but unfortunately outerHTML and innerHTML won't generate proper XHTML, and don't render valid XML ( it won't close single tags like <hr /> ).,XMLSerializer also won't close single tags, which is quite surprising to me.
I noticed the HTML node transformations will exceptionnaly works when the actual HTML doesn't contain any self closing tags. So I supect Chrome to internally serialize the HTML with XMLSerializer or outerHTML before the XSLT.
Here's the JSFiddle: http://jsfiddle.net/eVbv7/
So, how can I XSLT HTML nodes on Chrome? I need either a way to serialize XHTML nodes properly or any workaround will do.