[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

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


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"

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">
           <xsl:attribute name="value">
              <xsl:value-of select="position()-1"/>
           <xsl:if test="$selected-ix=(position()-1)">
              <xsl:attribute name="selected">selected</xsl:attribute>
           <xsl:value-of select="."/>

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
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