XLST - Form Dropdown add Selected

Topics: General, XSLT
Sep 26, 2011 at 4:01 PM

I am trying to figure out how to mark the select option to be selected if the parameter value it the same as the option value.

I have a form with a drop down for selecting a "State".  The xslt function is provided a variable called "State".  What I am looking to do is to have the correct state be the Selected value of the drop down if the "State" variable is not empty.  Otherwise show "Select your state" (The first option in the select).  I know how to do this in classic asp, but getting lost in xslt.  I think I need to append an attribute, but not sure how.

Thanks!

<xsl:param name="State" select="/in:inputs/in:param[@name='State']" />

<select name="state" id="state" class="long-field" onchange="checkFieldBack(this);">
                      <option value="">Please Select</option>
                      <option value="AL">Alabama</option>
                      <option value="AK">Alaska</option>
                      <option value="AZ">Arizona</option>
                      <option value="AR">Arkansas</option>
                      <option value="CA">California</option>
                      <option value="CO">Colorado</option>
                      <option value="CT">Connecticut</option>
                      <option value="DE">Delaware</option>
                      <option value="FL">Florida</option>
                      <option value="GA">Georgia</option>
                      <option value="ID">Idaho</option>
                      <option value="IL">Illinois</option>
                      <option value="IN">Indiana</option>
                      <option value="IA">Iowa</option>
                      <option value="KS">Kansas</option>
                      <option value="KY">Kentucky</option>
                      <option value="LA">Louisiana</option>
                      <option value="ME">Maine</option>
                      <option value="MD">Maryland</option>
                      <option value="MA">Massachusetts</option>
                      <option value="MI">Michigan</option>
                      <option value="MN">Minnesota</option>
                      <option value="MS">Mississippi</option>
                      <option value="MO">Missouri</option>
                      <option value="MT">Montana</option>
                      <option value="NE">Nebraska</option>
                      <option value="NV">Nevada</option>
                      <option value="NH">New Hampshire</option>
                      <option value="NJ">New Jersey</option>
                      <option value="NM">New Mexico</option>
                      <option value="NY">New York</option>
                      <option value="NC">North Carolina</option>
                      <option value="ND">North Dakota</option>
                      <option value="OH">Ohio</option>
                      <option value="OK">Oklahoma</option>
                      <option value="OR">Oregon</option>
                      <option value="PA">Pennsylvania</option>
                      <option value="RI">Rhode Island</option>
                      <option value="SC">South Carolina</option>
                      <option value="SD">South Dakota</option>
                      <option value="TN">Tennessee</option>
                      <option value="TX">Texas</option>
                      <option value="UT">Utah</option>
                      <option value="VT">Vermont</option>
                      <option value="VA">Virginia</option>
                      <option value="WA">Washington</option>
                      <option value="DC">Washington, D.C.</option>
                      <option value="WV">West Virginia</option>
                      <option value="WI">Wisconsin</option>
                      <option value="WY">Wyoming</option>
                    </select>

Sep 27, 2011 at 8:37 AM
Edited Sep 27, 2011 at 8:37 AM

Basically attribute  "selected" can be added like:

 
	<option value="DE">
		<xsl:if test="'DE' = $state">
			<xsl:attribute name="selected">selected</xsl:attribute>
		</xsl:if>
		Delaware
	</option>

But better solution is creating list dinamically:

1) Create inline C# function

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Composite.Data;
using Composite.Data.Types;

namespace Test
{
  public static class InlineMethodFunction
  {
    public static XElement GetStates()
    {
      
      return XElement.Parse(@"<States>
  <State Id=""AL"">Alabama</State>
  <State Id=""AK"">Alaska</State>
  <State Id=""AZ"">Arizona</State>
  <State Id=""AR"">Arkansas</State>
  <State Id=""CA"">California</State>
  <State Id=""CO"">Colorado</State>
  <State Id=""CT"">Connecticut</State>
  <State Id=""DE"">Delaware</State>
  <State Id=""FL"">Florida</State>
  <State Id=""GA"">Georgia</State>
  <State Id=""ID"">Idaho</State>
  <State Id=""IL"">Illinois</State>
  <State Id=""IN"">Indiana</State>
  <State Id=""IA"">Iowa</State>
  <State Id=""KS"">Kansas</State>
  <State Id=""KY"">Kentucky</State>
  <State Id=""LA"">Louisiana</State>
  <State Id=""ME"">Maine</State>
  <State Id=""MD"">Maryland</State>
  <State Id=""MA"">Massachusetts</State>
  <State Id=""MI"">Michigan</State>
  <State Id=""MN"">Minnesota</State>
  <State Id=""MS"">Mississippi</State>
  <State Id=""MO"">Missouri</State>
  <State Id=""MT"">Montana</State>
  <State Id=""NE"">Nebraska</State>
  <State Id=""NV"">Nevada</State>
  <State Id=""NH"">New Hampshire</State>
  <State Id=""NJ"">New Jersey</State>
  <State Id=""NM"">New Mexico</State>
  <State Id=""NY"">New York</State>
  <State Id=""NC"">North Carolina</State>
  <State Id=""ND"">North Dakota</State>
  <State Id=""OH"">Ohio</State>
  <State Id=""OK"">Oklahoma</State>
  <State Id=""OR"">Oregon</State>
  <State Id=""PA"">Pennsylvania</State>
  <State Id=""RI"">Rhode Island</State>
  <State Id=""SC"">South Carolina</State>
  <State Id=""SD"">South Dakota</State>
  <State Id=""TN"">Tennessee</State>
  <State Id=""TX"">Texas</State>
  <State Id=""UT"">Utah</State>
  <State Id=""VT"">Vermont</State>
  <State Id=""VA"">Virginia</State>
  <State Id=""WA"">Washington</State>
  <State Id=""DC"">Washington, D.C.</State>
  <State Id=""WV"">West Virginia</State>
  <State Id=""WI"">Wisconsin</State>
  <State Id=""WY"">Wyoming</State>
</States>
");
    }
  }
}

 

2) Add this funtion to function call of XSltFunction

3) Write code

 

 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:in="http://www.composite.net/ns/transformation/input/1.0"
  xmlns:lang="http://www.composite.net/ns/localization/1.0"
  xmlns:f="http://www.composite.net/ns/function/1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="xsl in lang f">

  <xsl:param name="state" select="/in:inputs/in:param[@name='State']" />  
    <xsl:param name="states" select="/in:inputs/in:result[@name='GetStates']/States" />
    
  <xsl:template match="/">
    <html>
      <head>
       
      </head>

      <body>
        <select name="state" id="state" class="long-field" onchange="checkFieldBack(this);">
          <option value="">Please Select</option>
          <xsl:for-each select="$states/State">
            <option value="{@Id}">
              <xsl:if test="@Id = $state">
                <xsl:attribute name="selected">selected</xsl:attribute>
              </xsl:if>
              <xsl:value-of select="." />
            </option>
          </xsl:for-each>
        </select>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>