Reading Cookies from XSLT

776 views
Skip to first unread message

sanjay

unread,
Feb 24, 2006, 9:10:39 PM2/24/06
to XSLT

Hi,

I want to access the cookie (basically document.cookie from javascript)
from inside XSL document, something similer to the sample shown below
and I want to support both IE and Netscape.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test.xsl" type="text/xsl"?>
<ABC></ABC>

XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:funcs="some:unique:uri">

<msxsl:script language="JavaScript" implements-prefix="funcs" >
<![CDATA[
function MyFunc() {
return "read cookie and return data from cookie";
}
]]>
</msxsl:script>

<xsl:template match="/ABC">
<html>
<body>
<p><xsl:value-of select="funcs:MyFunc()"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Thanks in advance.

Sanjay

AndrewF

unread,
Feb 25, 2006, 5:03:54 AM2/25/06
to XSLT
Hi Sanjay,

You might be overcooking this a bit - why not just put in your HTML
something like the following...

<html>
<head>
<script type="text/js" language="javascript" src="remote path to
file.js"/>
<head>
<body>
<p><script>document.write(myFunc())</script></p>
</body>
</html>

This is all legitimate from an XHTML point of view, it is portable as
it means you can use standard ECMA 1.2 or something like that in your
javascript and it will make your XSL LOADS more readable....

I know you can do it this way but I reckon long term your code will be
more maintainable the other way....

Cheers
AndrewF

sanjay

unread,
Feb 27, 2006, 7:26:29 PM2/27/06
to XSLT
Thanks Andrew for your reply.

I tried this but seems that XSL does not like this. Actually it will
work inside the <xsl:template>. But I need it at the top level at the
global section.

Basically I have to set a global variable (currently I am using
<xsl:param>) with the value from cookie.

Thanks
-Sanjay

AndrewF

unread,
Feb 28, 2006, 7:37:36 AM2/28/06
to XSLT
What about passing the value as a parameter into the stylesheet?

I'm not sure exactly how to do it using MSXML from within javascript
but I know for example within .NET you create an XslArgumentList
object, add the parameters in [which could be determined by a cookie
for example] and then when you do the translation it automatically
provides the info to the XSL template....

Cheers
Andrew

sanjay

unread,
Feb 28, 2006, 8:42:50 PM2/28/06
to XSLT
You are right we could do easily from .NET or from Xlan as they support
parameter passing. But as far as I know It's not possible to do it from
CGI based application. Becuse the support for parameter passing should
come from browser. While doing it
from .NET you internally use an Active X plugin which takes care of it.

AndrewF

unread,
Mar 1, 2006, 4:55:52 AM3/1/06
to XSLT
Bugger. That is a pain you have to do it that way.

If you are using CGI as the means then why not create an XSL file which
in iteself is a template. Make the XSL valid in that in the parameter
value you put in someting like <xsl:param name="thename">[COOKIE VALUE
HERE]</xsl:param>

Then what you do is load up the XSL file as a normal file read into
memory. Pull the cookie value from the HTTP headers passed in the
request, replace the bit of text [COOKIE VALUE HERE] and then load the
XSL document from memory as a string rather than as a file...

That should work and you have sidestepped the parameter issue by making
it the default value in the document anyway.

Cheers
Andrew

sanjay

unread,
Mar 2, 2006, 5:35:49 PM3/2/06
to XSLT

You are the man. I think I got the way around by getting COOKIE from
header.
I did some ugly stuff after that but at least it's working, and there
seems
to be no other alternative.

Thanks Andrew for your help.

Reply all
Reply to author
Forward
0 new messages