?
Solved

Dynamically creating selectbox in XSL

Posted on 2002-04-23
4
Medium Priority
?
285 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:
Wayne Bradney earned 400 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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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