Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2006, 08:41 PM
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Populating a Drop Down given a number attribute

Hello,

I have been struggling with this for the past few days and need your help. I have xml data that looks something like this..

<row TicketID="17633050" Available="8" EventDate="2006-12-27T19:30:00" SeatSection="51" SeatRow="M" SeatFrom="*" SeatThru="*" SeatDescription="FEDEX 1 WEEK PRIOR TO EVENT!" TicketPrice="175" BrokerPrice="175" BrokerID="0" />


Using MSXML 4.0, my XSL file currently loops through each //row node and collects the data for each attribute. I have only one problem, the @Available attribute requires me to display a drop down on the html side as a "quantity". Because the xml only provides a number, I need to create a loop that creates an <option></option> for each @Available -1 except for a value that would leave 1. ( that may sound confusing but here is an example)

Example: If the xml value is 8. My drop down list would have to include 8,6,5,4,3,2,1. I cannot have a value of 7 as an option since that leaves only 1 qty available for purchase when 7 is selected.

My current xsl file looks something like this...


<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ms="urn:schemas-microsoft-com:xslt"
    xmlns:dt="urn:schemas-microsoft-com:datatypes"
    xmlns:vbs="urn:schemas-sqlxml-org:vbs">

<xsl:template match="/">
    <xsl:call-template name="tickets"/>
</xsl:template>

 <xsl:template name="tickets">
     <table cellspacing="0" cellpadding="2">
     <tr><th width="200" align="left">Seat Location</th><th width="100" align="left">Price</th><th width="100" align="left">Quantity</th><th width="80" align="center">Buy</th></tr>
     <xsl:for-each select="//row">
     <xsl:sort select="@TicketPrice" order="ascending" data-type="number"/>
     <xsl:variable name="qty" select="@Available"/>
     <tr>
     <xsl:if test="(position() mod 2 = 1)">
     <xsl:attribute name = "bgcolor">#f4f4f4</xsl:attribute>
     </xsl:if>
     <td><b>Sec:</b><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text><xsl:value-of select="@SeatSection"/><xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;</xsl:text><b>Row:</b><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text><xsl:value-of select="@SeatRow"/></td>
     <td>$<xsl:value-of select="@TicketPrice"/> each</td>
     <td><select><option><xsl:value-of select="@Available"/></option>
      <option><xsl:value-of select="for $qty in (1 to $qty) return $qty - 1"/></option>
     </select> of <xsl:value-of select="@Available"/></td>
     <td align="center" valign="middle"><button name="buy">Buy</button></td>
     </tr>
     <tr>
     <xsl:if test="(position() mod 2 = 1)">
     <xsl:attribute name = "bgcolor">#f4f4f4</xsl:attribute>
     </xsl:if>
     <td colspan="5"><b>Notes:</b><xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text><xsl:value-of select="@SeatDescription"/></td></tr>
     </xsl:for-each>
     </table>
 </xsl:template>
</xsl:stylesheet>

Any help would be greatly appreciated as I am stuck and cannot proceed.

Thanks in advance!
_ramzy
 
Old December 26th, 2006, 07:39 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Firstly, don't use disable-output-escaping to generate a NBSP character. This will only work in environments where (a) the output of your stylesheet is serialized, and (b) the optional d-o-e capability is supported. For example, it won't work in Firefox. The hex code for NBSP is xa0 and the easiest way to output the character is to write it as #xa0; in the stylesheet.

Concerning your question, you're using XSLT 2.0 syntax so I assume you want an XSLT 2.0 solution. You're almost there with

<option><xsl:value-of select="@Available"/></option>
      <option><xsl:value-of select="for $qty in (1 to $qty) return $qty - 1"/></option>

except that this generates two option elements, one of which contains multiple values, when you actually want one option element for each value (I think). Also you surely want to generate <option value="3">3</option> rather than <option>3</option>.

So I think you want

<select>
<xsl:for-each select="1 to xs:integer(@Available)[. != xs:integer(@Available) - 1]">
  <option value="{.}"><xsl:value-of select="."/></option>
</xsl:for-ach>
</select>




Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 26th, 2006, 07:47 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I just read the post again and saw that you are using MSXML 4.0, which means you won't be able to use XPath 2.0 constructs.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 26th, 2006, 11:40 PM
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay,

What parser version can I use which will allow me to utilize the xpath 2.0 constructs?

Thanks in advance for your assistance

 
Old December 27th, 2006, 04:30 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The most widely used XSLT 2.0 processor is my own, Saxon - see http://saxon.sf.net/. The only other alternative, unless you happen to be an Eiffel user, is the Altova product - see http://www.altova.com/

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating a drop down list from an ArrayList snejsnej ASP.NET 1.0 and 1.1 Basics 1 April 20th, 2006 11:14 AM
drop down is not populating spm74 ASP.NET 1.0 and 1.1 Basics 4 March 16th, 2004 12:36 PM
Populating Drop Down based on other drop down jeffbarclay Java Databases 1 November 7th, 2003 12:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.