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

Subject: Re: [xsl] Creating a html <select> dropdown menu in XSL, where the attribute of an XML element is the selected value when page loads
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 05 Jan 2010 12:10:01 -0500
Martin,

The root of your problem is that you are trying to implement logic that goes outside the "comfort zone" of XSLT 1.0. This doesn't make it impossible. But XSLT 1.0 was not designed on the assumption that you would need to generate anything in your results ex nihilo. This includes simple things like sequences of numbers (1-31) or months (January-December or Janvier-Decembre, etc.) Instead, it works on the premise that all the information needed by the transformation would be directly available in the source document.

Consequently, you have three general strategies open to you:

1. Switch to XSLT 2.0. Of course for reasons outside this thread, you may not be able to do this (for example, if you must run in Transformiix, which doesn't do XSLT 2.0). But XSLT 2.0 offers all the power you need and them some.

2. Use various advanced techniques, idioms and tricks that have been devised over the years in order to do what you need to do in XSLT 1.0.

This is the path you have started on by using XPaths such as (//*)[position() lt;= 31]. This is a viable approach, but for a non-expert in XSLT 1.0 it can be confusing and troublesome. For example, some of these techniques and tricks have limitations. (As David explained, the trick using (//*) has the limitation that you can count only up to the number of nodes returned by "//*", which is sometimes although not always a problem.) Understanding these limitations and your options for avoiding or getting around them (e.g.: recursive template calls, as Ken suggests) requires that you deepen your understanding of XSLT, perhaps considerably.

Are you up for this? Certainly we can help you if you are. Also, many of the things you will learn -- not the tricks themselves, but why they work -- will stand you in good stead going forward.

But it will require patience and diligence. And in the age of XSLT 2.0 it seems decreasingly rewarding to go about deliberately mastering the secrets of the ancient masters, when we have better ways.

3. Arrange it so that all the information you need is availabe in your source(s), as the design of XSLT 1.0 expects.

In your case, this means, perhaps, having a static document called by your stylesheet using the document() function, which lists everything you need. For example:

<dates>
<days>
  <day n="1"/>
  <day n="2"/>
  ...
  <day n="31"/>
</days>

<months>
  <month days="31">January</month>
  <month days="29">February</month>
  ...
  <month days="31">December</month>
</months>
</dates>

By querying into this document using XPath, you can generate all your options.

Of course, this method, and has limitations of its own. For example, covering the years between 1900 and (say) 2100: you have to have a document with a series of 201 elements in it. Bother. I'd actually use a recursive function call for that one.

But personally, I'd prefer XSLT 2.0. Not only does it have constructs with the power you need (sequences and perhaps temporary trees, which can serve as arrays etc.), it also has direct support for dates datatypes, which could be useful.

How do you want to do it?

Cheers,
Wendell

At 12:36 PM 1/4/2010, you wrote:
I'm sorry if I haven't been following the posting guidelines properly.
I'm aware that everybody posting here are volunteers and I'm grateful
for all the help I have been receiving...


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread