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

[xsl] RE: (kommersz' HTML Option Problem)


Subject: [xsl] RE: (kommersz' HTML Option Problem)
From: "Morgan, Corey" <CMorgan@xxxxxxxxx>
Date: Tue, 21 Oct 2003 09:18:27 -0600

Hello.

kommersz wrote:

> I'd like to create a callable template, with a parameter
> given, which outputs an html fragment like this:
> 
> <option value="0">January</option>
> <option value="1">February</option>
> <option value="2" selected="1">March</option>
> <option value="3">April</option>
> .
> .
> .
> <option value="11">December</option>

> My question is, if there is an easy way to enumerate months ...

Most often, you would grab such "catalog" values from some external source 
and pull them into your stylesheet using the XSL document() function.  
Usually, this list of options has some context, some rules-driven behavior.

But months aren't likely to change, so you could get away with embedding 
the list of months directly into your stylesheet, as a so-called
top-level user defined element.

So within your stylesheet, you'll define your list of months in some
structure:

  <catalog:months>
    <month>January</month>
    <month>February</month>
    ...
    <month>December</month>
  </catalog:months>
...
</xsl:stylesheet>

The "catalog" namespace distinguishes this element from content 
that would otherwise be copied to the output.  
Your <xsl:stylesheet> element will need to be changed to reflect this fact:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:catalog="http://mycompany.com/catalog"
exclude-result-prefixes="catalog">


Finally, all you need is a named template to pull it together:

<xsl:template name="month-select">
   <xsl:param name="selected-ix"/>
   <select name="month-of-birth">
     <xsl:for-each select="document('')/*/catalog:months/month">
        <option>
           <xsl:attribute name="value">
              <xsl:value-of select="position()-1"/>
           </xsl:attribute>
           <xsl:if test="$selected-ix=(position()-1)">
              <xsl:attribute name="selected">selected</xsl:attribute>
           </xsl:if>
           <xsl:value-of select="."/>
        </option>
     </xsl:for-each>
   </select>
</xsl:template>   


Note the use of the document() function.  A call to document('') indicates
that this
stylesheet itself is the source of your <month> elements.  Passing in some
other
URL would instead indicate that your <month> elements come from some 
external source.

Hope this helps.
Corey Morgan

(BTW, You may want to consider making your month values 1-based rather than
0-based.  java.util.Calendar, for example, maps January as 1).

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords
xsl