contentType: function(path) {
if (path.indexOf('.xml') > -1 || path.indexOf('.xsl') > -1 || path.indexOf('.xslt') > -1) {
return "application/xml"
} else if (path.indexOf('.xhtml') > -1) {
return "application/xhtml+xml"
} else if (path.indexOf('.html') > -1) {
return "text/html"
} else if (path.indexOf('.js') > -1) {
return "text/javascript"
} else if (path.indexOf('.css') > -1) {
return "text/css"
} else {
return "text/plain"
}
},
readFileToString: function(fileObj) {
// |file| is nsIFile
var data = "";
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);
fstream.init(fileObj, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish
let (str = {}) {
let read = 0;
do {
read = cstream.readString(0xffffffff, str); // read as much as we can and put it in str.value
data += str.value;
} while (read != 0);
}
cstream.close(); // this closes fstream
return data;
},
// path must be absolute, and NOT have the "file://" protocol indicator.
// returns an nsIFile object, not the data. For the data, you have to read to a string, use above function
openFile: function (path) {
// open the file stream
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path)
return file
},
// opens an xml file and returns dom-parsed XML object.
openXML: function(path) {
var xmlFile = openFile(path)
var xmlAsString = readFileToString(xmlFile)
domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"].createInstance(Components.interfaces.nsIDOMParser);
try {
var xml = domParser.parseFromString(xmlAsString, contentType(path))
} catch(e) {
log("openXML: error opening xml " + path + ": " + e)
}
return xml
},
getXSLTProc: function (xsltFile) {
var xml = openXML(xsltFile)
} catch (e) {
log("Problem loading " + xsltFile + " : " + e)
throw e
}
xsltTransform = Components.classes["@mozilla.org/document-transformer;1?type=xslt"].createInstance(Components.interfaces.nsIXSLTProcessor)
try {
xsltTransform.importStylesheet(xml)
} catch (e) {
log("Problem importing " + xsltFile + ": " + e)
throw e
}
return xsltTransform
},
saveXML: function (doc, path, bogus) {
var serializer = Components.classes["@mozilla.org/xmlextras/xmlserializer;1"].createInstance(Components.interfaces.nsIDOMSerializer);
var xml
// you can see here my attempt to force xhtml to be saved as xhtml
if (path.indexOf('.xhtml') > -1) {
xml = document.implementation.createDocument('', '', document.implementation.createDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"))
} else {
xml = document.implementation.createDocument('', '', doc.doctype)
}
xml.appendChild(xml.importNode(doc.documentElement, true))
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path)
var output = Components.classes['@mozilla.org/network/file-output-stream;1'].createInstance(Components.interfaces.nsIFileOutputStream)
output.init(file, 0x20 | 0x02 | 0x08, 0665, 0)
serializer.serializeToStream(xml, output, 'UTF-8')
output.flush()
output.close()
},
transformiix: function(xslt, xml, out, param) {
// open my xml
var xmlDoc = openXML(xml);
// get xlstprocessor
var xsltProc = getXSLTProc(xslt);
// do the transform
var outXML = xsltProc.transformToDocument(xmlDoc);
// save my resulting xhtml
saveXML(outXML, out)
},
My stylesheet has the output instruction
<xsl:output encoding=“UTF-8” media-type=“application/xhtml+xml”/>
And my file is being saved as an .xhtml file.
Even with all this, when I make the file, and try to open in Firefox using location.replace(href), I see this:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
(followed by my xhtml raw code)
The first two lines of the xhtml are:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=“http://www.w3.org/1999/xml”>
If I manually change the xmlns attribute to “http://www.w3.org/1999/xhtml” it will display properly.
What more do I have to do to make the file be saved correctly?