Cookie and XSLT preview

Topics: General, Troubleshooting, XSLT
Jul 20, 2011 at 8:12 PM

I am trying to call in a cookie value, but it is not working.  Am I missing something.  I will post what I did below.  Thanks in advance.


Created an XSLT Function.

Created a Function Call - Composite.Web.Request.CookieValue.TEST

Created a template to print the value - <xsl:value-of select="@TEST" />   I also tried in an input: {@TEST}


Either of these worked, but when I preview the function it list the cookie and value:


<!-- Function Call Result (0 ms), XPath /in:inputs/in:result[@name='TEST'] -->
    <in:result name="TEST">TEST VALUE</in:result>


Thanks for your help!



Jul 21, 2011 at 12:46 AM

Did you by chance set the FallbackValue parameter to "TEST VALUE"?

FallbackValue let you specify what you get when the cookie has no value and if you do not have the cookie set and you gave "TEST VALUE" as a fallback value, then the result makes sense.

Jul 21, 2011 at 2:36 AM
Edited Jul 21, 2011 at 3:15 AM

I did not set up a Fallback Value.  The Cookie was there as I had already run the page that was to create the cookie.  So the cookie was created (I verified this by looking at the cookie from the browser options).  This is why the Cookie was found when I previewed the function.  So the cookie is being created and found in the input screen when I look at the function preview, but it will not display the value in the preview output screen or  when I add the function to a page. 

Jul 21, 2011 at 8:47 AM


You wrote: Created a template to print the value - <xsl:value-of select="@TEST" />   I also tried in an input: {@TEST}

I am not sure that your XSLT template is correct.

Below is example how to render value from the function call Composite.Web.Request.CookieValue:

<xsl:template match="/">
        <xsl:variable name="cookieValue" select="/in:inputs/in:result[@name='CookieValue']" />
        <xsl:value-of select="$cookieValue" />
  or <input type="text" value="{$cookieValue}" /> </body> </html> </xsl:template>

Show us your XSLT template, please.

Jul 21, 2011 at 2:37 PM
Edited Jul 21, 2011 at 3:13 PM
Thanks for your help.  If I have a cookie with multiple values, am I able to display each value?  If so, how can I do this using he  Composite.Web.Request.CookieValue: feature.

Cookie: Test
Value 1: Address=123 way st
Value 2: Zipcode=12345

Thanks again.
Jul 21, 2011 at 5:24 PM

In this case function Composite.Web.Request.CookieValue will return cookie value like this:

<in:result name="CookieValue">Address=123 way st&amp;Zipcode=12345</in:result>

I can recommend to write your own C# function like this:
public static XElement MultipleCookieValue(string cookieName)
            XElement result = null;
            if (HttpContext.Current.Request.Cookies[cookieName] != null)
                result = new XElement("CookieInfo");
                var values = HttpContext.Current.Request.Cookies[cookieName].Values;
                for (int i = 0; i < values.Count; i++)
                    result.Add(new XAttribute(values.AllKeys[i], values[i]));
            return result;

then in XSLT template you can use it like this:

  <xsl:variable name="cookieInfo" select="/in:inputs/in:result[@name='MultipleCookieValue']/CookieInfo" />
        <input type="text" value="{$cookieInfo/@Address}" />
        <input type="text" value="{$cookieInfo/@ZipCode}" />

Jul 21, 2011 at 7:20 PM

Thank you for all your help. 

One last thing, my cookie is storing a value (date) in a format like 12/12/2010.  When I display the value of the cookie - it is showing it as %2312%2F12%2F1210%23.  But if I call the cookie from a plain page outside the CMS it displays it correctly 12/12/2010.  Is there a way to get the value to display correctly in my XSLT function?


Thanks again.

Jul 22, 2011 at 7:51 AM

>> When I display the value of the cookie - it is showing it as %2312%2F12%2F1210%23. 

I could not reproduce this. Could you show how you get and display your date Cookie.

Jul 22, 2011 at 2:59 PM
Edited Jul 22, 2011 at 3:08 PM


I posted my code below.  The end user will complete a form (only 1 input - a date selected from a date picker).  
The form is then submitted to processes the date entered with a SQL stored procedure to obtain the needed data.  
The data is then set as cookies.  Both the form page and XSLT are within the CMS. The form is then submitted to a Classic ASP page to process the SQL request and store the data into cookies.  
I used classic asp backend as I am still new to ASP.NET.  I would really like it in ASP.NET so I can use session variables instead of cookies - but I am stuck with cookies until I learn more coding.  
If I run this process outside the CMS, the data is stored as cookies and when I call the cookie the values that are in date format (12/12/2010) are displayed just fine.

Thanks again.


XSLT that displays the cookie value
xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""
	exclude-result-prefixes="xsl in lang f">

	<xsl:template match="/">
<xsl:variable name="how_old" select="/in:inputs/in:result[@name='how_old']" />
<xsl:variable name="Conception" select="/in:inputs/in:result[@name='Conception']" />
<xsl:variable name="DueDate" select="/in:inputs/in:result[@name='DueDate']" />
<xsl:variable name="Milestone" select="/in:inputs/in:result[@name='Milestone']" />
<xsl:variable name="DATE" select="/in:inputs/in:result[@name='DATE']" />
<xsl:variable name="CFS" select="/in:inputs/in:result[@name='CFS']" />
<xsl:variable name="Genetic" select="/in:inputs/in:result[@name='Genetic']" />
<xsl:variable name="CVS" select="/in:inputs/in:result[@name='CVS']" />
<xsl:variable name="First_Tri" select="/in:inputs/in:result[@name='First_Tri']" />
<xsl:variable name="Amnio" select="/in:inputs/in:result[@name='Amnio']" />
<div id="caculator">
  <div class="resultbox">
     <div id="PC-Hdr-Result">
           <div class="title">Your Results</div>
     <div id="MyCalulation">
                  <table width="100%" cellspacing="10">
            <td width="30%">DATE ENTERED:</td>
            <td width="27%"><input type="text" value="{$DATE}" disabled="disabled" /></td>
            <td width="43%" class="babyage">Your baby is <xsl:value-of select="$how_old" /> weeks old</td>
            <td>Approx. Conception Date:</td>
            <td><input value="{$Conception}" disabled="disabled" /></td>
            <td>Approx. Due Date:</td>
            <td><input value="{$DueDate}" disabled="disabled" /></td>


Jul 25, 2011 at 7:50 AM

>>  my cookie is storing a value (date) in a format like 12/12/2010.  When I display the value of the cookie - it is showing it as %2312%2F12%2F1210%23.

The value %2312%2F12%2F1210%23 looks like URL encoded and if to use HttpUtility.UrlDecode( "%2312%2F12%2F1210%23") method to get decoded value it will look like  #12/12/1210# .
It doesn't match with  12/12/2010.  Make sure that you try to get correct Cookie value and that  your DatePicker returns correct value and stores in Cookie.