Solved

Dynamically creating selectbox in XSL

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
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. 
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now