PDA

View Full Version : Applying stylesheets to returned XML


rbrown17
06-12-2006, 03:19 PM
Usually if you want to associate a style sheet with an xml document, you would put in the xml document :
<?xml:stylesheet href="hello.css" type="text/css" ?>

However if you have an xml document in a browser which is returned from a .cgi, how do you add a stylesheet to the returned xml document ?

I thought about using javascript but cannot find any examples.

I also thought about adding something to the request URL which is going to the .cgi service but cannot achieve anything. I came across an example which uses the amazon service. This adds a parameter to the URL:

http://xml.amazon.com/onca/xml3?locale=us&t=bobducharmeA&dev-t=dev-ID-here&
ArtistSearch=velvet%20underground&mode=music&sort=+pmrank&offer= All&type=lite&f=http://www.snee.com/xsl/awslite2txt.xsl&headers=yes
(from: http://www.xml.com/pub/a/2004/08/04/tr-xml.html )

However I think that the Amazon service provides this function where my .cgi service would not.

ealbrecht
06-14-2006, 08:43 PM
If you don't have control over the output of your XML data that is returned to you from the CGI script then you'll have to use JavaScript to transform the XML data with one of your XSL documents. In IE it's fairly easy to do this, in FireFox however you'll first have to create your own XSLT Processor.

rbrown17
06-14-2006, 09:59 PM
Thank you for your advice. I will have a try with Javascript.

It was interesting to see that you are at Edmonton, Canada. I went to Edmonton from Vancouver on a student work exchange program. I stayed at the University of Alberta Halls of Residence. I remember the massive shopping mall there. There were not many jobs there in IT when I was there. I went to Banff for a day looking for work. There was one job as a dishwasher but no where to stay! Those were the days.

ealbrecht
06-14-2006, 10:18 PM
Here is some code from one of my old files, it contains a bunch of stuff you won't need so just use what you need. Since you already have the XML data then you won't need to load an XML file, just proceed with loading your XSL file and then transforming your XML data with it. This particular example uses frames, and the XSL documents that I use create a html page with all tags. If you're xsl document only creates lets say a table or a div, then you can transform your xml to any one of your existing html elements, like a div or p tag.


var COMPLETED = 4;
var xmlDocs = new Array();
window.onload = init;

function init() {
runTransform ('offline.xml','content.xsl',content);
runTransform ('offline.xml','menu.xsl',menu);
runTransform ('offline.xml','title.xsl',titlebar);
}

/* *********************************************************************** */

function runTransform (xmlFile,xslFile,xDoc){
if(document.implementation && document.implementation.createDocument){
// Mozilla

var xsltProcessor = new XSLTProcessor();

// load the xslt file
var myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", xslFile, false);
myXMLHTTPRequest.send(null);

// get the XML document
xslStylesheet = myXMLHTTPRequest.responseXML;
xsltProcessor.importStylesheet(xslStylesheet);

// load the xml file
myXMLHTTPRequest = new XMLHttpRequest();
myXMLHTTPRequest.open("GET", xmlFile, false);
myXMLHTTPRequest.send(null);

var xmlSource = myXMLHTTPRequest.responseXML;

//transform
var result = xsltProcessor.transformToDocument(xmlSource);
var xmls = new XMLSerializer();
var output = xmls.serializeToString(result);
xDoc.document.write (output);

}else if(window.ActiveXObject){
// IE

// Load XML
xml = new ActiveXObject("MSXML2.DOMDocument");
xml.async = false
xml.load(xmlFile)

// Load XSL
xsl = new ActiveXObject("MSXML2.DOMDocument");
xsl.async = false
xsl.load(xslFile)

// Transform
xDoc.document.write( xml.transformNode(xsl) );

}else{
// Browser unknown
alert("Browser unknown");
}
}