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 November 27th, 2007, 01:41 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default repetitive instruction, more efficient solution?

Is there a more efficient way to build a pulldown menu where the selected option depends on a parameter passed in? I have defined a template, but it seems that there must be a more efficient way to go.

Currently, the template is:

<xsl:template name="hour_pulldown">


<xsl:param name="i"/>

<select name="select">
    <option><xsl:if test="$i = 0"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>00</option>
    <option><xsl:if test="$i = 1"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>01</option>
    <option><xsl:if test="$i = 2"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>02</option>
    <option><xsl:if test="$i = 3"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>03</option>
    <option><xsl:if test="$i = 4"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>04</option>
    <option><xsl:if test="$i = 5"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>05</option>
    <option><xsl:if test="$i = 6"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>06</option>
    <option><xsl:if test="$i = 7"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>07</option>
    <option><xsl:if test="$i = 8"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>08</option>
    <option><xsl:if test="$i = 9"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>09</option>
    <option><xsl:if test="$i = 10"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>10</option>
    <option><xsl:if test="$i = 11"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>11</option>
    <option><xsl:if test="$i = 12"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>12</option>
    <option><xsl:if test="$i = 13"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>13</option>
    <option><xsl:if test="$i = 14"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>14</option>
    <option><xsl:if test="$i = 15"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>15</option>
    <option><xsl:if test="$i = 16"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>16</option>
    <option><xsl:if test="$i = 17"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>17</option>
    <option><xsl:if test="$i = 18"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>18</option>
    <option><xsl:if test="$i = 19"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>19</option>
    <option><xsl:if test="$i = 20"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>20</option>
    <option><xsl:if test="$i = 21"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>21</option>
    <option><xsl:if test="$i = 22"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>22</option>
    <option><xsl:if test="$i = 23"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>23</option>
    <option><xsl:if test="$i = 24"><xsl:attribute name="selected">selected</xsl:attribute></xsl:if>24</option>
</select>
</xsl:template>
 
Old November 27th, 2007, 01:45 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Are you using XSLT 1.0 or 2.0?

 
Old November 27th, 2007, 01:49 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for not clarifying that in my previous post.

I am using XSLT 2.0.

 
Old November 27th, 2007, 02:03 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Then you should be able to code it easily e.g.
Code:
<xsl:template name="hour_pulldown">

<xsl:param name="i"/> 
<select name="select">
  <xsl:for-each select="0 to 24">
    <option>
      <xsl:if test=". eq $i">
        <xsl:attribute name="selected" select="'selected'"/>
      </xsl:if>
      <xsl:value-of select="if (. lt 10) then concat('0', .) else ."/>
    </option>
  </xsl:for-each>
</select>
</xsl:template>
 
Old November 27th, 2007, 02:41 PM
Authorized User
 
Join Date: May 2007
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. Definitely more efficient with 2.0.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Repetitive regex mark.gargan BOOK: Beginning Regular Expressions 2 August 6th, 2008 03:31 AM
repetitive xml - sorry, I'm new to this... JamesHurrell XSLT 4 March 12th, 2008 04:24 PM
XSLT Table Row with repetitive first cell the cause XSLT 5 July 3rd, 2007 01:29 PM
processing instruction vs. element bcogney XML 6 April 10th, 2007 03:46 PM





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