xsl-list
[Top] [All Lists]

Re: [xsl] Creating a html <select> dropdown menu in XSL, where the attribute of an XML element is the selected value when page loads

2010-01-12 15:27:42
At 2010-01-12 22:09 +0100, Martin Jackson wrote:
For the months, Kens question, "why do you need an algorithmic
approach?" made me realize I didn't. Instead I just made 12 static
<choose> parts, one for each month. Like this:

<xsl:template match="@month">
        <xsl:variable name="month">
                <xsl:value-of select="."/>
        </xsl:variable>

The above is very wasteful ... and it isn't even needed.

                <xsl:choose>
                        <xsl:when test="$month='januari'">
                                <option value="januari" selected="selected">
                                        januari
                                </option>
                        </xsl:when>
                        <xsl:otherwise>
                                <option value="januari">
                                        januari
                                </option>
                        </xsl:otherwise>
                </xsl:choose>

<!-- and so on -->
</xsl:template>

That can be simplified:

  <xsl:template match="@month">
    <option value="januari">
      <xsl:if test="@month='januari'>
        <xsl:attribute name="selected">selected</xsl:attribute>
      </xsl:if>
      <xsl:text>januari</xsl:text>
    </option>
    ...
  </xsl:template>

... and even easier when you use XSLT 2.0.

The solution for the days and years was indeed to use recursive template calls.

The code for this is found below, if anybody happens upon this thread
and is interested in how I solved it.

And also here:

<xsl:template name="incrementValue">
        <xsl:param name="value"/>
        <xsl:param name="invar"/>
        <xsl:param name="stop"/>
        <option value="{$value}">
           <xsl:if test="$value=$invar">
              <xsl:attribute name="selected">selected</xsl:attribute>
            <xsl:value-of select="$value"/>
        </option>
        <xsl:if test="$value &lt; $stop">
          <xsl:call-template name="incrementValue">
                <xsl:with-param name="value" select="$value + 1"/>
                <xsl:with-param name="invar" select="$invar"/>
                <xsl:with-param name="stop" select="$stop"/>
          </xsl:call-template>
        </xsl:if>
</xsl:template>

I hope this helps as it will reduce your maintenance by having less duplicated code.

. . . . . . . . . Ken

--
UBL and Code List training:      Copenhagen, Denmark 2010-02-08/10
XSLT/XQuery/XPath training after http://XMLPrague.cz 2010-03-15/19
XSLT/XQuery/XPath training:   San Carlos, California 2010-04-26/30
Vote for your XML training:   http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--