Solved

XSLT for Beginners

Posted on 2004-04-12
22
595 Views
Last Modified: 2013-11-18
Hey guys,

I'm in desperate need for help!  Isn't everyone that post though :)

Ok.  I'm really new to XSLT so I'm guessing the problem I'm having is easily overcome... I hope cos I need to get this thing finished in about 5 hours.  Everything is working on my webpage except for one thing...  I need to add a button in the XSLT.  The code supplied works fine except for the button section.  It displays all the products, descriptions,etc...  What I need as well, is to have a button for each product that has the following code attached to it

Context.Items("ProductID") = @name
                             (the name of the current product name)
Server.Transfer("AddToCart.aspx")

So it calls the addtocart page and the code in that adds the product to the shopping cart.

Ok, in the code below, I've tried heaps of things to get this working but I just can't figure it out.

Guys, any help here ASAP would be greatly appreciated!  I'm a the business end of the rope at the moment so I'm really struggling. :(

Look forward to a solution.
oMavericko

[code]<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version= "1.0">
      <xsl:template match="/">
            
            <xsl:for-each select="/productlist/product">
                  <TABLE width="75%" align="center" border="1">
                        <tr>
                              <td>Name: </td>
                              <td><xsl:apply-templates select="name"/></td>
                              <td RowSpan = "4" width= "0"> <xsl:apply-templates select="image"/></td>
                        </tr>
                        <tr>
                              <td>Code: </td>
                              <td><xsl:apply-templates select="code"/>t</td>
                        </tr>
                        <tr>
                              <td>Category: </td>
                              <td><xsl:apply-templates select="category"/></td>
                        </tr>
                        <tr>
                              <td>Price: </td>
                              <td><xsl:apply-templates select="price"/></td>
                        </tr>
                        <tr>
                              <td>Description: </td>
                              <td><xsl:apply-templates select="description"/></td>
                              <td>
                                    <input type="button" onclick="Context.Items("ProductID") = prodid;Server.Transfer("AddToCart.aspx")" value="Add To Cart"/>
                              </td>
                        </tr>
                  </TABLE>
                  <p/>
            </xsl:for-each>
      </xsl:template>
      
      <xsl:template match="name">
            <b><xsl:value-of select="."/> </b>
      </xsl:template>
                  
      <xsl:template match="code">
            <xsl:value-of select="."/>
      </xsl:template>
      
      <xsl:template match="price">
            $<xsl:value-of select="."/>.00
      </xsl:template>
      
      <xsl:template match="category">
            <xsl:value-of select="."/>
      </xsl:template>
      
      <xsl:template match="description">
            <xsl:value-of select="."/>
      </xsl:template>  
                                  
      <xsl:template match="image">
            <img>
                  <xsl:attribute name="src">
                        <xsl:value-of select="." />
                  </xsl:attribute>
            </img>      
         </xsl:template>
         
</xsl:stylesheet>[/code]
0
Comment
Question by:oMaverickoVB
  • 12
  • 9
22 Comments
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805257
Your problem is the quotes. Try this:

<input type='button' onclick='Context.Items("ProductID") = prodid;Server.Transfer("AddToCart.aspx")' value='Add To Cart'/>
0
 

Author Comment

by:oMaverickoVB
ID: 10805283
mmm... I tried that.  It didn't work.  I can't seem to get any code to work here.  Is there a bit of test code I could put in there to see if I can get that working.  I'm a VB Programmer, so I'm used to using MSGBOX("test") to see if anything works.  can I do that here?
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805349
I see. So you are you trying to insert VB.net code here, in the onclick event of the link?
0
 

Author Comment

by:oMaverickoVB
ID: 10805369
yep, is that possible?  I basically need to pass the current product to a webpage.  Is this possible???  Can you show me a simple how to do this?

cheers
0
 

Author Comment

by:oMaverickoVB
ID: 10805597
sorry, I don't understand. I thought this was in the XML area?  What do I need to do?  Is there a link I need to follow or anything?
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805646
Is the product id in the xml file that you are tranforming?

If so, you could create a hidden form field on the page, in the onclick event have a javascript which passes the value to the hidden field and submits the form to AddToCart.aspx. This file's code behind would then read it and perform the needed action.

Lisa
0
 

Author Comment

by:oMaverickoVB
ID: 10805677
The product id is the product name in this case.  So it's already in the XML file.  Can you show what code I need to have using the context items and server.transfer code?  I've been told I need to do it this way.

Is it possible for you to show me on my example above?  

Thanks in advance
0
 

Author Comment

by:oMaverickoVB
ID: 10805710
Ok.  No matter what code I put in the onclick... I'm getting an error.  I put this line of code in just to see if this is where my problem lies...

           <input type='button' onclick='textbox1.text=1'></input>

All I got from this is an error.... not cool.  any reason why?
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805752
<input type='button' onclick='textbox1.value=1'></input>
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805755
You can put javascript in there, no problem.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:oMaverickoVB
ID: 10805801
ok... I've worked that part out.  I'm using VS.NET and I was using the textbox from the webforms and not the HTML toolbox.  Ok.  So I've worked that little bit out.  

But I'm still lost about how I use those 2 lines of code I need.  One to set that variable and the other to load the other page....

?? Me lost
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10805806
Is the product id in the xml file that you are tranforming?
0
 

Author Comment

by:oMaverickoVB
ID: 10805817
The product id is exactly the same as the product name which is being passed.  Is that a problem?
0
 
LVL 3

Accepted Solution

by:
ljfernandez earned 125 total points
ID: 10805975
OK, the solution I'm talking about is something like this, but it will need tweaking. I'm assuming the "." value is the product id.

 <form action="AddToCart.aspx">
<input type="hidden" id="prodID"/>

<xsl:for-each select=".">
<xsl:variable name="prodID"><xsl:value-of select="."/></xsl:variable>

<input type='button' onclick='document.getElementById("prodID").value={$prodID};document.forms[0].submit();' value='Add To Cart'/>
                                    
</xsl:for-each>
</form>

Then in the code behind of AddToCart.aspx, you have to grab the value of request.prodID.
0
 

Author Comment

by:oMaverickoVB
ID: 10805993
I'm lost dude.  Where do I put that code in?  I'm really new at this.  Is it a simple case of adding that section in somewhere to the code above?
0
 

Author Comment

by:oMaverickoVB
ID: 10806099
Is it possible for you to add the changes to my code above?  I just need to get this part working and then I can hand it in.  Everything else is working.  The AddToCart page uses the following lines of code:-

Dim prodid as string = Ctype(context.items("ProdId"),String)

and then used the label to get the ProdID:-

lblProductID.Text = ProdID

Thanks
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10806100
IMO I think your question is too complex for 125pts. The first part goes in the xsl and the second part goes in the code behind for the AddToCart.aspx file.

Lisa
0
 

Author Comment

by:oMaverickoVB
ID: 10806122
sorry, I don't have any more points.  These are my last points available.  I didn't think it was going to be this much of a problem for you.

Sorry about this then.
0
 

Author Comment

by:oMaverickoVB
ID: 10806198
Well, I'm still stuck in the same position I was at the start about 3 hours ago.  Thank you for helping anyway.  I honestly don't understand where that code goes but thank you anyway.  I just gotta stick with it and see what happens.  Sorry, I don't have anymore points to give you.


oMavericko
0
 
LVL 3

Expert Comment

by:ljfernandez
ID: 10806503
oMavericko,

Hey no problem, I was just saying that it seems the problem is a bit more complex than it seems on the surface. I can't add my code to your code because of the complexities of combining the two technologies that you are trying to use here. As I understand it, the .Net code is executed on the server side before the XSL transform takes place, which means you can't easily put the .net stuff in the html like you can with a regular .aspx page.  Sorry I couldn't be of more help to you. I feel bad taking your points if my answer doesn't help!

Best of luck with your problem,

Lisa
0
 

Author Comment

by:oMaverickoVB
ID: 10806525
hey, its ok.  I took a punt... it didn't pay off but I guess it kinda showed which way I shouldn't be heading :/

I just find it hard that I can't use those 2 lines of code in the XSL file...  I simply don't have enough knowledge of it to get it done so I'm stuffed now.




thanks anyway
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Delete from XML 10 37
replace quotes with UTF-8 character 38 68
Path of Workbook 3 45
ASP.NET reading ATOM 2 26
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

758 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

19 Experts available now in Live!

Get 1:1 Help Now