[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Solution: Passing Cookie Values From JavaScript To XSL

Subject: Solution: Passing Cookie Values From JavaScript To XSLT
From: Michael <msell@xxxxxxxxxxxxxxx>
Date: Wed, 07 Sep 2005 00:48:06 +1000
javascript setparameter
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" />

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.