xsl-list
[Top] [All Lists]

Solution: Passing Cookie Values From JavaScript To XSLT

2005-09-06 07:48:06
A long time ago I asked myself if it was possible to send JavaScript cookie values to an XSL stylesheet, process that stylesheet, then display the results in a standard web browser. I spent one and a half months on this problem. One and a half months of grueling trial and error. One and a half months of banging my head against various brick walls. I sustained concussion, thankfully I also sustained a solution.

My solution was to get JavaScript to dynamically process the XSL spreadsheet. This allows the JavaScript to access the HTML page's DOM aspects, which means that I have access to any stored cookie values, and can simply pass them into the XSL spreadsheet, process the spreadsheet, and display the results.

My biggest hurdle was the fact that Internet Explorer and Gecko (Mozilla & Netscape) browsers both require different JavaScript methods to process XSL documents. My solution... Try to process XSL spreadsheet with code for Internet Explorer; if that didn't work, catch the error and move on to the Gecko code; If that didn't work, tough.

I've used this solution to produce a 100% client side book catalog that allows a user to do keyword searches by author and title. You can view the website at http://msell.customer.netspace.net.au/. If you'd like to download the entire example website, you can get it at http://msell.customer.netspace.net.au/download/bookcatalog.zip.

Below is the code used in the HTML document that gets cookie values:

function getCookie(cookieName)
{
 // declare the local variables
 var cookieIndexOne, cookieIndexTwo;
 // attempt to locate the passed cookie
 cookieIndexOne = document.cookie.indexOf(cookieName);
 cookieIndexTwo = document.cookie.indexOf(";", cookieIndexOne +
   cookieName.length + 1);
 // if the passed cookie is the last one in the string, continue
 if (!(cookieIndexOne == -1) && (cookieIndexTwo == -1))
   // set the end of the cookie value to the end of the string
   cookieIndexTwo = document.cookie.length;
 // if the passed cookie value can't be located, return a default result
 if ((cookieIndexOne == -1) || (cookieIndexTwo == -1) ||
(unescape(document.cookie.substring(cookieIndexOne + cookieName.length + 1,
     cookieIndexTwo)) == ""))
   return "All"
 // otherwise, return the passed cookie's value
 else
return unescape(document.cookie.substring(cookieIndexOne + cookieName.length
     + 1, cookieIndexTwo));
}

Below is the code used in the HTML document that processes the XSL spreadsheet:

function transformXML(xmlDocURL, xslDocURL, divID)
{
 // declare the local variables
 var xmlDoc, xslDoc, docProcessor, docCache, DocRequest, docFragment;
 // try the following
 try
 {
   // instantiate and load the xml document
   xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
   xmlDoc.async = false;
   xmlDoc.load(xmlDocURL);
   // instantiate and load the xsl document
   xslDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
   xslDoc.async = false;
   xslDoc.load(xslDocURL);
   // prepare the xsl document for transformation
   docCache = new ActiveXObject("MSXML2.XSLTemplate");
   docCache.stylesheet = xslDoc;
   // instantiate the document processor and submit the xml document
   docProcessor = docCache.createProcessor();
   docProcessor.input = xmlDoc;
   // add parameters to the xsl document
   docProcessor.addParameter("book", getCookie("book"), "");
   docProcessor.addParameter("category", getCookie("category"), "");
   docProcessor.addParameter("keyword", getCookie("keyword"), "");
   docProcessor.addParameter("sort", getCookie("sort"), "");
   docProcessor.addParameter("status", getCookie("status"), "");
   docProcessor.addParameter("topic", getCookie("topic"), "");
   docProcessor.addParameter("view", getCookie("view"), "");
// process the documents into html and submit to the passed div to the HMTL page
   docProcessor.transform();
   // divID.innerHTML = docProcessor.output;
   document.getElementById(divID).innerHTML = docProcessor.output;
 }
 // catch any errors from the above code
 catch(e)
 {
   // try the following
   try
   {
     // instantiate and load the xml document
     docRequest = new XMLHttpRequest();
     docRequest.open("GET", xmlDocURL, false);
     docRequest.send(null);
     xmlDoc = docRequest.responseXML;
     // instantiate and load the xsl document
     docRequest = new XMLHttpRequest();
     docRequest.open("GET", xslDocURL, false);
     docRequest.send(null);
     xslDoc = docRequest.responseXML;
     // instantiate the document processor and submit the xsl document
     docProcessor = new XSLTProcessor();
     docProcessor.importStylesheet(xslDoc);
     // add parameters to the xsl document
     docProcessor.setParameter(null, "book", getCookie("book"));
     docProcessor.setParameter(null, "category", getCookie("category"));
     docProcessor.setParameter(null, "keyword", getCookie("keyword"));
     docProcessor.setParameter(null, "sort", getCookie("sort"));
     docProcessor.setParameter(null, "status", getCookie("status"));
     docProcessor.setParameter(null, "topic", getCookie("topic"));
     docProcessor.setParameter(null, "view", getCookie("view"));
     // clear the passed div if anything was in it
     document.getElementById(divID).innerHTML = "";
// process the documents into html and submit to the passed div to the HMTL page
     docFragment = docProcessor.transformToFragment(xmlDoc, document);
     document.getElementById(divID).appendChild(docFragment);
   }
   // catch any errors from the above code
   catch(e)
   {
     // do nothing
   }
 }
}

Below is the code used in the XSL spreadsheet that declares the parameters being passed:

<xsl:param name="book" />
<xsl:param name="category" />
<xsl:param name="keyword" />
<xsl:param name="sort" />
<xsl:param name="status" />
<xsl:param name="topic" />
<xsl:param name="view" />

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>