#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    1
    Rep Power
    0

    How to do HTML selects with xslt?


    Lets say I'm going to have a form with a select with 12 options, option values being the numbers ov months and option labels being names of months.

    I have a xml document saying (among other things) which option should be selected.

    Beforementioned form and notably select are to be constructed with a xsl stylesheet.

    One solution would be like this:
    Code:
    ...
    <select>
    
    <xsl:element name="option">
      <xsl:attribute name="value">1</xsl:attribute>
      <xsl:if test="month = 1">
         <xsl:attribute name="selected">selected</xsl:attribute>
      </xsl:if>
      January
    </xsl:element>
    
    <xsl:element name="option">
      <xsl:attribute name="value">2</xsl:attribute>
      <xsl:if test="month = 2">
         <xsl:attribute name="selected">selected</xsl:attribute>
      </xsl:if>
      February
    </xsl:element>
    
    ...(and so on)
    ie. too much typing
    Do you know a better way to solve this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    new york
    Posts
    84
    Rep Power
    13
    i would probably use a looping construct there combined with a switch/case construct...
    Code:
    <!--  ====  THE SWITCH   ==== -->
    <xsl:choose>
          <xsl:when test="first_condition_true">
                  <!-- do this -->
          </xsl:when>
          <xsl:when test="second_cond_true">
                  <!-- do this -->
           </xsl:when>
    <!--  ....... -->
           <xsl:otherwise>
                 <!--all tests failed.. do this -->
           </xsl:otherwise>
    </xsl:choose>
    
    <!--  ====  THE LOOP   ==== -->
    <xsl:for-each select="element">
        <!--  do something -->
    </xsl:for-each>

IMN logo majestic logo threadwatch logo seochat tools logo