Solved

Dynamically creating selectbox in XSL

Posted on 2002-04-23
4
281 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
  • 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
[PHP] Hash a XML file/string 3 75
installing and using WTP plugin eclipse MARS 3 111
Shredding xml into an oracle 11g Database 2 43
Issue with XSLT mapping 10 35
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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