Solved

Creating and passing hidden fields XSLT

Posted on 2009-07-11
15
2,264 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:QuinnDester
15 Comments
 
LVL 3

Assisted Solution

by:eirikurh
eirikurh earned 100 total points
Comment Utility
If you wish to use link (GET method), to go directly to redirect.aspx you have to pass the productLink to the query string
 <a>
<xsl:attribute name="href"><xsl:text>redirect.aspx?productLink={yahoo:Url}</xsl:text></xsl:attribute>Buy Now
</a>


If you want to hide the parameter you need to change your link to submit the current form
<xsl:attribute name="onclick">formName.submit();return false;</xsl:attribute>
And then on the server side. use Server.Transfer to Redirect to redirect.aspx
if (!IsPostBack)
  Server.Transfer("Redirect.aspx")
0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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
0
 
LVL 18

Expert Comment

by:Gary Davis
Comment Utility
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
0
 
LVL 18

Expert Comment

by:Gary Davis
Comment Utility
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
0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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..
0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 200 total points
Comment Utility
The method is not UrlEncodeXSL, the class is
The method is EncodeUrl

So, it could be a simple typo
<xsl:value-of select="XSLExtensionObject:EncodeUrl($offerurl)" />
could work.
possibly you need to pass the class as well
<xsl:value-of select="XSLExtensionObject:UrlEncodeXSL.EncodeUrl($offerurl)" />

Allthough you might need to create an object first
something like
<xsl:variable name="encoder" select="XSLExtensionObject:UrlEncodeXSL.new()"/>
(I am not sure you have a constructor with the class)
and pass the object along with the method
<xsl:value-of select="XSLExtensionObject:EncodeUrl($encoder,$offerurl)" />
The later is how I make it work using Java and Saxon

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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

0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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
0
 
LVL 18

Expert Comment

by:Gary Davis
Comment Utility
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
0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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,
0
 
LVL 18

Assisted Solution

by:Gary Davis
Gary Davis earned 200 total points
Comment Utility
No, I don't think it integrates into VS, though it can generate C# code for doing transformations.
However, check this URL out: http://msdn.microsoft.com/en-us/library/ms255605(VS.80).aspx:
Debugging XSLT

You can use the Visual Studio debugger to debug XSLT. The debugger supports setting breakpoints, viewing XSLT execution state, and so on. The debugger can be used to debug a style sheet, or to debug an XSLT transformation invoked from another application.

Gary
0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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:
0
 
LVL 3

Author Comment

by:QuinnDester
Comment Utility
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

0
 
LVL 3

Author Closing Comment

by:QuinnDester
Comment Utility
Everybody contributed something that helped me figure the answer out, thank you
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now