[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] selecting HTML Options


Subject: RE: [xsl] selecting HTML Options
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 18 Jan 2005 09:45:51 -0000

Try this:

<xsl:variable name="skeleton">
  <option value="00">JAN</option>
  <option value="01">FEB</option>
  ...
</xsl:variable>

<xsl:template name="monthOptions">
        <xsl:param name="name"/>
        <xsl:param name="value"/>
  <select name="{$name}" 
    <xsl:apply-templates select="$skeleton/option" mode="o">
      <xsl:with-param name="value" select="$value"/>
    </
  </select>
</

<xsl:template match="option" mode="o">
 <xsl:param name="value"/>
 <xsl:copy>
   <xsl:copy-of select="@value"/>
   <xsl:if test="@value=$value">
     <xsl:attribute name="selected">selected</xsl:attribute>
   </xsl:if>
 </xsl:copy>
</xsl:template>

Not much shorter in the end, but perhaps more satisfying. Needs the
xx:node-set extension if using XSLT 1.0.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Chris_Graham@xxxxxxxxxxx [mailto:Chris_Graham@xxxxxxxxxxx] 
> Sent: 18 January 2005 04:52
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] selecting HTML Options
> 
> There has to be an easier way to select <option> elements inside a 
> <select> element that this:
> 
> <!-- Generate the Month combo box -->
> <xsl:template name="monthOptions">
>         <xsl:param name="name"/>
>         <xsl:param name="value"/>
> <select name="{$name}">
> 
>     <xsl:if test="'00' = $value"><option value='00'></option></xsl:if>
>     <xsl:if test="not('00' = $value)"><option value='00' selected=
> "selected"></option></xsl:if>
>     <xsl:if test="'01' = $value"><option 
> value='01'>JAN</option></xsl:if>
>     <xsl:if test="not('01' = $value)"><option value='01' selected=
> "selected">JAN</option></xsl:if>
>     <xsl:if test="'02' = $value"><option 
> value='02'>FEB</option></xsl:if>
>     <xsl:if test="not('02' = $value)"><option value='01' selected=
> "selected">FEB</option></xsl:if>
> 
>     ...
> 
> </select>
> </xsl:template>
> 
> Can anyone point me in the right direction? I'd rather avoid 
> calling java 
> directly... :-(
> 
> -Chris
> 
> 
> 
> **************************************************************
> *********************
> This email contains information confidential to AAMI Limited. 
> If you are not the intended recipient, you must not disclose 
> or use the information in it. If you have received this email 
> in error, please notify us immediately by return email, and 
> delete this email and any attached documents.
> AAMI Limited is not responsible for any changes made to a 
> document other than those made by AAMI Limited or for the 
> effect of the changes on the document meaning.
> **************************************************************
> *********************


Current Thread
Keywords