Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 26th, 2007, 09:22 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using xml elements as html attributes

I have an xml document which contains a list of items for a bogus online store:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="stock.xsl"?>
<stock>
    <item>
        <name>Snickers</name>
        <price>$1.99</price>
    </item>
    <item>
        <name>Skittles</name>
        <price>$2.50</price>
    </item>
</stock>
My goal is to make an xsl file that will be able to display this file as a series of text and text boxes that will enable the user to see what the available products are (and at what price) and then input how many they would like to purchase. I am able to produce this, but with one problem: I can't figure out how to give the individual text boxes differing name attributes. Therefore, when the user submits the form, I have no idea which quantity applies to which item. Here is my attempted xsl:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="style.css" />
            </head>
            <body>

                <h2 class="center" >Items in Stock</h2>

                <br />
                <br />
                <form method="get" action="cashout.asp" >
                    <xsl:for-each select="stock/item">
                        <br />
                        <br />
                        <p class="left">
                            <xsl:value-of select="name" />
                            Qty:
                            <input type="text" name="This is what I want to dynamically change based on the <xsl:value-of select="name" value above" />
                             @ 
                            <xsl:value-of select="price" />
                        </p>
                    </xsl:for-each>
                        <br/>
                        <p class="left">
                            <input type="submit" name="submit" value="Pay" />
                        </p>
                </form>

            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>
And this is all loaded by an asp file that contains the background and other parts of the page:
Code:
<html>
<head>
<title>Store</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>


<%
'Displays the contents of the xml database of items in stock using xsl
'Load XML
set xml = Server.CreateObject("Microsoft.XMLDOM")
xml.async = false
xml.load(Server.MapPath("stock.xml"))
'stock.xml is the xml file above

'Load XSL
set xsl = Server.CreateObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load(Server.MapPath("stock.xsl"))
'stock.xsl is the xsl file above

'Transform file
Response.Write(xml.transformNode(xsl))
%>

Edit:

And for further clarification, the html I would like to view after running the asp file is:
Code:
<html>
<head>
<title>Store</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2 class="center">Items in Stock</h2>
<br>
<br>
<form method="get" action="cashout.asp">
    <br>
    <br>
    <p class="left">
        Snickers
        Qty:
        <input type="text" name="Snickers"> 
         @ 
        $1.99
    </p>
    <br>
    <br>
    <p class="left">
        Skittles
        Qty:
        <input type="text" name="Skittles"> 
         @ 
        $2.50
    </p>
    <br>
    <p class="left">
        <input type="submit" name="submit" value="Pay">
    </p>
</form>
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old June 26th, 2007, 09:42 AM
mhkay's Avatar
Wrox Author
Points: 18,252, Level: 58
Points: 18,252, Level: 58 Points: 18,252, Level: 58 Points: 18,252, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,929
Thanks: 0
Thanked 281 Times in 276 Posts
Default

There are a number of ways you can generate unique names, for example using position() or <xsl:number> or generate-id()

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old June 26th, 2007, 10:01 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the speedy reply. That's half of what I wanted to know. The other half is: How would I use this to generate a name attribute for the input text field? I tried adding it to the the xsl file as so:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <head>
                <link rel="stylesheet" type="text/css" href="style.css" />
            </head>
            <body>

                <h2 class="center" >Items in Stock</h2>

                <br />
                <br />
                <form method="get" action="cashout.asp" >
                    <xsl:for-each select="stock/item">
                        <br />
                        <br />
                        <p class="left">
                            <xsl:value-of select="name" />
                            Qty:
                            <input type="text" name="<xsl:number>" />
                             @ 
                            <xsl:value-of select="price" />
                        </p>
                    </xsl:for-each>
                        <br/>
                        <p class="left">
                            <input type="submit" name="submit" value="Pay" />
                        </p>
                </form>

            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>
But when I ran the asp file, I got an error "msxml3.dll (0x80004005)
The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document. " So I assume this is not the way to go about doing it.

Is this something that needs to be done outside of the xsl file with a script?

Reply With Quote
  #4 (permalink)  
Old June 26th, 2007, 10:17 AM
mhkay's Avatar
Wrox Author
Points: 18,252, Level: 58
Points: 18,252, Level: 58 Points: 18,252, Level: 58 Points: 18,252, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,929
Thanks: 0
Thanked 281 Times in 276 Posts
Default

><input type="text" name="<xsl:number>" />

XML doesn't allow elements within an attribute.

You need

<input type="text">
  <xsl:attribute name="name"><xsl:number/></xsl:attribute>
</input>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old June 26th, 2007, 10:22 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you that worked perfectly and it makes sense. Thanks for your time and patience.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
need html attribute based on xml attributes charles95621 XSLT 1 May 24th, 2007 05:21 PM
sequenced elements to attributes zkent XSLT 0 April 19th, 2006 08:50 AM
Elements and attributes Morrislgn XSLT 1 June 20th, 2005 11:13 AM
Elements w same name and their attributes supafly XSLT 1 May 30th, 2005 06:34 AM
compare two xml files attributes, elements k_s_ashok XML 1 June 9th, 2004 02:08 AM



All times are GMT -4. The time now is 03:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.