Link to home
Start Free TrialLog in
Avatar of QuinnDester
QuinnDesterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Creating and passing hidden fields XSLT

I have created a hiden field in XSLT that is included withing the <a> tages of my link
this seems to be working correctly and when source is viewed the hidden field is present on the page.
when the link is clicked to redirects to the correct page. but i cant pick up the passed veriable

code below

Thanks
XSLT code:
           <a>
 
  <xsl:attribute name="href"><xsl:text>redirect.aspx</xsl:text></xsl:attribute>Buy Now
  
    <input>
  <xsl:attribute name="id"><xsl:text>productLink</xsl:text></xsl:attribute><xsl:attribute name="name"><xsl:text>productLink</xsl:text></xsl:attribute><xsl:attribute name="type"><xsl:text>hidden</xsl:text></xsl:attribute><xsl:attribute name="runat"><xsl:text>server</xsl:text></xsl:attribute><xsl:attribute name="value"><xsl:value-of select="yahoo:Url"/></xsl:attribute>
  </input>
  </a>
 
Code in the redirect.aspx to pick up the veriable
 
 
      string target = Request["productLink"];
       if (target == null)
       {
           Label1.Text = "value not retrieved";
       }
       else
       {
 
       Label1.Text = target;
        }

Open in new window

SOLUTION
Avatar of eirikurh
eirikurh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QuinnDester

ASKER

This isnt using .net controls, its html transformed by XSLT there are no associated controls in the pages, the hidden field is a normal html hidden field that should pass the value by post,

(the get method can not be used unless the value can be URL encoded within the XSL, then it would be a simple matter of appending the value to the URL as an attribute)

the link goes directly to the redirect page and the hidden field should pass through the value via post. whether it is or not i cant test as i cant pick up the value on the redirect.aspx
This looks wrong for simple HTML:
 <xsl:attribute name="runat"><xsl:text>server</xsl:text></xsl:attribute>
I don't think it is a problem - it probably will be ignored. It is not needed, however.
Does your redirect.aspx display "Value not received" or blank? If blank, it may be the value-of select="yahoo:Url" is not set but you say you have viewed the source so I assume it is correct. If "not received", then there is some other issue. Maybe set the form method to GET so you can see the parameters on the url in the browser, or you can use a tool like fiddler or HttpWatch to see all the parameters.
Gary Davis
Looking further...maybe you are not using a form but instead, an "<a href=...>". In that case, you want all the parameters to be part of the href attribute
<xsl:text>redirect.aspx?productLink="</xsl:text><xsl:value-of select="yahoo:Url"/>"
Or something along that line. There are no attributes to the <a> for type, value, etc. Basically, you are mixing up the <form> and the <a>.
Gary Davis
unles i can url encode the yahoo:url it cant be added to the href,

I am working on ways that i could possibly encode the URL (by using extention objects - as you may have noticed :) )

i am not using a form to pass the hidden values, it is inside the ending </a> tag as in html this causes it to be submited on clicking the link,

i cant use a form directly as this breaks the html in .net

i seem to be running around in circles here and hitting a brick wall every time .
but i am determind to find a way of doing this..
What i have so far:

I have created a simple class that encodes the url and returns it:

namespace XSLExtensionObject
{
public class UrlEncodeXSL
{
    public string EncodeUrl(string offerurl)
      {
        string s = HttpUtility.UrlEncode(offerurl);
        return s;
      }
}
}

i have created my XsltArgumentList

        XsltArgumentList xsltArgList = new XsltArgumentList();
        XSLExtensionObject.UrlEncodeXSL XSLExtensionObject = new XSLExtensionObject.UrlEncodeXSL();
        xsltArgList.AddExtensionObject("urn:XSLExtensionObject", XSLExtensionObject);
        Response.ContentType = "text/html";

i have included the namspace in the xslt


    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:user="urn:comparionsplus-com:xslt"
    xmlns:yahoo="urn:yahoo:prods"
    xmlns:XSLExtensionObject="urn:XSLExtensionObject">
    <xsl:output method="html"/>

i call the class in my xslt here

          <xsl:variable name="offerurl">
      <xsl:value-of select="yahoo:Url" />
      </xsl:variable>
          <xsl:variable name="offer-url-encoded">
      <xsl:value-of select="XSLExtensionObject:UrlEncodeXSL($offerurl)" />
      </xsl:variable>

and i get this error

[XslTransformException: Extension object 'urn:XSLExtensionObject' does not contain a matching 'UrlEncodeXSL' method that has 1 parameter(s).]

at least i have it to the point where the 2 are communicating, just need to get them talking the same lanuage
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Gertone, thank you for looking at this, you were right i was calling the class not the method,

I have corrected that and no longer get any errors, how ever i am not getting the return either.

the url's are blank in the resulting html file.. can you see anything that would cause that

i have tested the class and it is working and returning the correct encoded value. but its not being implememnted in the xslt,

the xslt with the code changes you suggested is as follows
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:user="urn:comparionsplus-com:xslt"
    xmlns:yahoo="urn:yahoo:prods"
    xmlns:XSLExtensionObject="urn:XSLExtensionObject">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <body>
                <xsl:apply-templates select="yahoo:ProductSearch/yahoo:Products"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="yahoo:ProductSearch/yahoo:Products">
 
            
        <xsl:apply-templates select="yahoo:Product/yahoo:Offer"/>
    </xsl:template>
    	<xsl:variable name="offerurl">
					<xsl:value-of select="yahoo:Url" />
				</xsl:variable>
    				<xsl:variable name="offer-url-encoded">
					<xsl:value-of select="XSLExtensionObject:EncodeUrl($offerurl)" />
				</xsl:variable>
 
    <xsl:template match="yahoo:Products/yahoo:Product/yahoo:Offer">
<div class="clear"></div>
<div class="products">
      <table align="center" class="style2" bgcolor="ffffff" cellpadding="3" border="1">
        <tr>
            <td align="center" class="style23" rowspan="3">
                     <a>
      
       <xsl:attribute name="href">redirect.aspx?target=<xsl:value-of select="$offer-url-encoded"/>
       </xsl:attribute>
       
       <img>
       <xsl:attribute name="src" >
       
       <xsl:value-of select="yahoo:ListImage/yahoo:Url"/>
       </xsl:attribute> 
       <xsl:attribute name="border"> <xsl:value-of select="0"/>
       </xsl:attribute>
       <xsl:attribute name="alt">
       <xsl:value-of select="yahoo:ProductName"/>
       </xsl:attribute>
       </img>
       <input><xsl:attribute name="name"><xsl:text>productLink</xsl:text></xsl:attribute><xsl:attribute name="type"><xsl:text>hidden</xsl:text></xsl:attribute><xsl:attribute name="value"><xsl:value-of select="$offer-url-encoded"/></xsl:attribute></input>
       </a></td>
            <td class="style27" colspan="3">
                <xsl:value-of select="yahoo:ProductName"/></td>
            <td class="style19">
                Price</td>
            <td align="center" class="style9">
                 <xsl:value-of select="yahoo:Price"/> GBP</td>
        </tr>
        <tr>
            <td colspan="3" rowspan="2">
 
              <xsl:value-of select="translate(normalize-space(yahoo:Summary), '#', '')"/>
            </td>
            <td class="style19">
                Delivery</td>
            <td align="center" class="style9">
                <xsl:value-of select="yahoo:DeliveryCost"/> GBP</td>
        </tr>
        <tr>
            <td align="center" class="style10" colspan="2">
               
                <xsl:value-of select="yahoo:Availability"/></td>
        </tr>
        <tr>
            <td class="style23">
                Category</td>
            <td align="center" colspan="2">
                <a><xsl:attribute name="href">home.aspx?catid=<xsl:value-of select="yahoo:Category/@id"/>&amp;title=<xsl:value-of select="translate(normalize-space(yahoo:Category/yahoo:Name), ' ', '_')"/></xsl:attribute><xsl:value-of select="yahoo:Category/yahoo:Name"/></a></td>
            <td align="center" colspan="2"> 
                <xsl:value-of select="yahoo:Merchant/yahoo:Name"/>
 </td>
            <td align="center" class="style10" colspan="2">
           <div> 
           <a>
 
  <xsl:attribute name="href"><xsl:text>redirect.aspx</xsl:text></xsl:attribute>Buy Now
  
    <input>
  <xsl:attribute name="id"><xsl:text>productLink</xsl:text></xsl:attribute><xsl:attribute name="name"><xsl:text>productLink</xsl:text></xsl:attribute><xsl:attribute name="method"><xsl:text>post</xsl:text></xsl:attribute><xsl:attribute name="type"><xsl:text>hidden</xsl:text></xsl:attribute><xsl:attribute name="runat"><xsl:text>server</xsl:text></xsl:attribute><xsl:attribute name="value"><xsl:value-of select="yahoo:Url"/></xsl:attribute>
  </input>
  </a>
 
  
</div>
</td>
        </tr>
    </table>
</div>
         <br />
    </xsl:template>
    
 
</xsl:stylesheet>

Open in new window

Gertone, further testing has shown that the class is being called but that the value isnt being passed to it

is there a way of putting breakpoints into the xslt so i can see what is happening there and at what point its failing
I use Stylus Studio for xslt development and debugging (www.stylusstudio.com). Inserting a breakpoint: http://www.stylusstudio.com/docs/v2009R2/d_start35.html
Gary
Do you know if this intergrates into Visual Studio
It would need to intergrate into Visual Stuido to allow breakpoints as the xslt uses a .net class to manipulate data,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Gary

Didnt know you could do that in VS :)

the debugging of the xslt in vs is giving me this error, but i am not sure it can access the class of the application when debugged seperatly.

System.Xml.Xsl.XslTransformException was unhandled by user code
  Message="Cannot find the script or external object that implements prefix 'urn:XSLExtensionObject'."
  Source="System.Data.SqlXml"
  LineNumber=0
  LinePosition=0
  StackTrace:
       at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
       at System.Xml.Xsl.CompiledQuery.Query.offer-url-encoded(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) in C:\Documents and Settings\Chris\My Documents\Visual Studio 2008\WebSites\WebSite1\XSLTFile.xslt:line 25
       at System.Xml.Xsl.CompiledQuery.Query.Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
       at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
       at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
       at System.Xml.Xsl.XmlILCommand.Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
       at System.Xml.Xsl.XslCompiledTransform.Transform(XmlReader input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)
       at Microsoft.XslDebugger.Host.Executor.Execute(String[] args)
  InnerException:
Ok sussed it and got it working :) thanks for the help guys, ill share the points on this one.

debuging in VS when using extension objects, you need to set the debug property when instainitating the XslCompiledTransform to true. like this.

XslCompiledTransform xslt = new XslCompiledTransform(true);

this includes the xslt debug engine in the normal debug procedure and makes your extention objects available to the xslt.

i got the extension objects working by adding the call directly to the template that was using them, they dont seem to work if added the a veriable then called using a veriable (strange but i am not familiar with using veriables in xslt so it may be something i did or didnt do)

Again thanks for all your help  i can now pass the url as redirect.aspx?target=url

Everybody contributed something that helped me figure the answer out, thank you