*VBH* wrote:
This is probably a silly question but...
It's not a silly question.
I have an XML file which processes quite happily with XSL when opened
on its own.
'quite happily' depends on whether or not the client has an XML parser
and XSLT processor available!
However, I just want to include it in an ASP file. But when I do an
SSI, the XML is not reformatted by the XSL.
As in: <!-- #INCLUDE FILE="2003-06.XML" -->
The XML file includes <?xml-stylesheet type="text/xsl"
href="blog2.xsl"?>
Which works fine when the XML file is opened on its own. However
when I try it as an SSI, I just get the text as one long string.
Because the client has not received a valid XML document (I'm assuming
that your page does not just consist of the include statement - if it
does then the problem you describe is probably a content-type problem
instead)
Its for a site front page and is the XML blog for "what's new". I've
written my own blogging app which outputs xml files. I don't want to
put a load of processing into the front page that I have already
handled with XSL.
So how do you do an ASP SSI of an XML document and get it to fire the
XSL?
Here's what I do for this exact same process:
if (XMLFileHasBeenUpdatedSinceLastTime) {
TransformTheXMLIntoHTML();
SaveTheResultOfThisTransformationToDisk();
Response.Write(theResultOfTheTransformation)
}
else {
Server.Execute(outputFromPreviousTransformation)
}
I can post all of the necessary script if you like - it's JScript ASP
and there's quite a bit of it. To start with, here's just the
transformation function I use to transform the XML via XSLT into
(X)HTML:
(I'm posting this with word-wrap turned off, but some newsreaders may
still wrap it)
function TransformXMLWithAddParam(xmlFilePath, xslFilePath, paramName, paramValue) {
try {
var xslt = new ActiveXObject("Msxml2.XSLTemplate.4.0");
var xmlObj = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlObj.async = false;
xmlObj.resolveExternals = false;
var xmlDoc = xmlObj.load(xmlFilePath);
if (!xmlDoc) {
throw new Error(1, "Unable to load XML file \"" + xmlFilePath + "\"");
}
if (xmlObj.parseError.errorCode != 0) {
throw new Error(2, "\"" + xmlObj.parseError.reason + "\", line=" + xmlObj.parseError.line);
}
else {
// Load style sheet.
var xslObj = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.4.0" );
xslObj.async = false
xslObj.resolveExternals = false;
xslObj.load(xslFilePath);
xslt.stylesheet = xslObj;
if (!xslObj) {
throw new Error(3, "Unable to load XSL file \"" + xslFilePath + "\"");
}
if (xslObj.parseError.errorCode != 0) {
throw new Error(4, "\"" + xslObj.parseError.reason + "\", line=" + xslObj.parseError.line);
}
else {
// Add parameter to XSL sheet
var xslProc = xslt.createProcessor();
xslProc.input = xmlObj;
if (typeof paramName == "string") {
// STRING PARAMETERS USED
xslProc.addParameter(paramName, paramValue);
}
else if (typeof paramName == "object") {
// ARRAY PARAMETERS USED
for (var i in paramName) {
xslProc.addParameter(paramName[i], paramValue[i]);
}
}
xslProc.transform();
return(xslProc.output);
}
}
}
catch (err) {
throw new Error(err.number, "Function TransformXMLWithAddParam() failed with parameters xmlFilePath=\"" + xmlFilePath + "\", xslFilePath=\"" + xslFilePath + "\", paramName=\"" + paramName + "\", paramValue=\"" + paramValue + "\". Message=\r\n" + err.description);
}
}
--
Andrew Urquhart
- FAQ:
http://www.aspfaq.com
- Contact:
http://andrewu.co.uk/contact/