Solved

Dynamically creating selectbox in XSL

Posted on 2002-04-23
4
283 Views
Last Modified: 2012-06-22
Maybe someone can help me w/ this problem.

I'm trying to dynamically create a year selectbox in XSL. What I'd like to do is specify a start year and an end year and use a template to create the selectbox. Right now, everything is hardcoded.

Thanks in advance.
0
Comment
Question by:hobster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:jerelw
ID: 6964064
Have your code generate XML with a list of valid years, then simply generate you select

<Response>
  <YearList>
    <ValidYear>2000</ValidYear>
    <ValidYear>2001</ValidYear>
    <ValidYear>2002</ValidYear>
    <ValidYear>2003</ValidYear>
  </YearList>
</Response>

<select>
  <xsl:for-each select="Response/YearList/ValidYear">
    <option value="{ValidYear}"><xsl:value-of select="ValidYear"></option>
  </xsl:for-each>
</select>
0
 
LVL 2

Author Comment

by:hobster
ID: 6964076
That's something I want to avoid. Ideally, I just want to specify a start and end year and have xsl increment through each year to create the select box but I haven't found a way to do a Do While or For control type statement.
0
 
LVL 3

Expert Comment

by:jerelw
ID: 6965567
I hope you find a way to do i++ computations in xsl.

I tried unsuccessfully to increment that way, so I ended up having my code generate my content and have the xsl merely provide the presentation.

...I'm thinking, a [[CDATA block with JavaScript might do the trick, but I've never tried using Javascript to create my selects.

function createSelect(form, start, stop) {
  //create a select from start to stop.
}

Good luck!
0
 
LVL 6

Accepted Solution

by:
wbradney earned 100 total points
ID: 6966212
hobster,

For this kind of looping you'd need to resort to recursion. Something like:

<Response>
     <StartYear>1999</StartYear>
     <EndYear>2004</EndYear>
</Response>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <HTML>
      <BODY>
        <FORM>
          <SELECT>
            <xsl:call-template name="_build_select">
              <xsl:with-param name="start">
                <xsl:value-of select="/Response/StartYear"/>
              </xsl:with-param>
              <xsl:with-param name="end">
                <xsl:value-of select="/Response/EndYear"/>
              </xsl:with-param>
            </xsl:call-template>
          </SELECT>
        </FORM>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template name="_build_select">
    <xsl:param name="start"/>
    <xsl:param name="end"/>

    <OPTION>
      <xsl:attribute name="value">
        <xsl:value-of select="$start"/>
      </xsl:attribute>
      <xsl:value-of select="$start"/>
    </OPTION>

    <xsl:if test="$start != $end">
      <xsl:variable name="new_start">
        <xsl:value-of select="$start + 1"/>
      </xsl:variable>
      <xsl:call-template name="_build_select">
        <xsl:with-param name="start">
          <xsl:value-of select="$new_start"/>
        </xsl:with-param>
        <xsl:with-param name="end">
          <xsl:value-of select="$end"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Regards,
WMB
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question