Link to home
Start Free TrialLog in
Avatar of ephemeralz
ephemeralz

asked on

xsl template for option selected drop-down box

I have a drop down list box that allows user to select Week 1-5. It also grab data from the xml tree - "root/event/event-week" I want to create a xsl template on checking to see if the value match the "event-week" if it is, then option selected = "event-week"
If there's not a single match, it would set to default, <OPTION>Week</OPTION>

So this is what I have and is not doing the job:

<td><select name="event-week">
<xsl:if test="{week} == ''">
<OPTION SELECTED "" value="{week}">
<xsl:value-of select="event/event-week"/>
</xsl:if>
<OPTION>Week</OPTION>
<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
<OPTION value="3">3</OPTION>
<OPTION value="4">4</OPTION>
<OPTION value="5">5</OPTION>
</select>
</td>

Thanks!
Avatar of rdcpro
rdcpro
Flag of United States of America image

This is incorrect:

<OPTION SELECTED "" value="{week}">

all attributes must be quoted.  So do it like:

<OPTION SELECTED="SELECTED' "" value="{week}">

But in y our case, you are building the <OPTION> elements from the XML, right?  So you have a parameter set, called "pSelectedWeek" that you can use to test.  Something like:

<td><select name="event-week">
<!-- not sure exactly what you want to do here...a select for all events, or only for unique weeks? -->
<xsl:for-each select="event">
  <OPTION value="{event-week}">
    <xsl:if test="$pSelectWeek = current()/event-week">
     <xsl:attribute name="SELECTED">SELECTED</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="event-week"/>
  </OPTION>
</xsl:for-each>
</select></td>


By the way, current coding standards use lower case for HTML...

Regards,
Mike Sharp


Avatar of ephemeralz
ephemeralz

ASKER

Hi Mike,
I have an update record form. What happen is, a user would select a specific record that they would like to update. Then it would call the update record form. The update record form, would grab the data from the xml tree structure for that specific record.  The xml tree structure would only have one event:
<root>
     <event>
         <event-id>1</event-id>
         <event-title>welcome title</event-title>
         <event-day>1</event-day>
         <event-week>3</event-week>
    </event>

Since this event is on day 1 week 3, the drop-down box on the update form would have the selected value on week 3, and the drop-down box for day would have the selected value on day 1. I'm not generating the option values based from the xml. I want the drop-down box to display the value that match the xml data. And the drop-down box is manually created through html, something like this:

<OPTION>Week</OPTION>
<OPTION value="1">1</OPTION>
<OPTION value="2">2</OPTION>
<OPTION value="3">3</OPTION>
<OPTION value="4">4</OPTION>
<OPTION value="5">5</OPTION>

Thanks!!

ASKER CERTIFIED SOLUTION
Avatar of rdcpro
rdcpro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Mike,

Can you please show me how to create a separate named-template and use xsl:call-template to check and output the attribute.

Thanks!
Given this XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="selectboxoption.xslt"?>
<root>
    <event>
        <event-id>1</event-id>
        <event-title>welcome title</event-title>
        <event-day>1</event-day>
        <event-week>3</event-week>
    </event>
</root>

This XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template name="tCounter">
        <xsl:param name="pCounter"/>
        <option value="{$pCounter}">
            <xsl:if test="event/event-week = $pCounter">
                <xsl:attribute name="selected">selected</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="$pCounter"/>
        </option>
        <xsl:if test="$pCounter &lt; 5">
            <xsl:call-template name="tCounter">
                <xsl:with-param name="pCounter" select="$pCounter + 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    <xsl:template match="root">
        <select name="event-week">
            <option>Week</option>
            <xsl:call-template name="tCounter">
                <xsl:with-param name="pCounter" select="1"/>
            </xsl:call-template>
        </select>
    </xsl:template>
</xsl:stylesheet>

produces this output:

<select name="event-week"><option>Week</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option></select>

Notice that using:

    <xsl:output method="html"/>

causes the selected="selected" attribute to be modified on output to conform with HTML 3.2.

Regards,
Mike Sharp
Thanks a lot Mike!! Greatly appreciate your help.