Solved

xsl template for option selected drop-down box

Posted on 2003-11-11
6
883 Views
Last Modified: 2008-03-03
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!
0
Comment
Question by:ephemeralz
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:rdcpro
ID: 9726265
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


0
 

Author Comment

by:ephemeralz
ID: 9726849
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!!

0
 
LVL 26

Accepted Solution

by:
rdcpro earned 20 total points
ID: 9727189
I see.  Then you could put the test on each option manually, if you want.  Make sure the XPath in the xsl:if is correct for your current context:


<OPTION>Week</OPTION>
<OPTION value="1">
    <xsl:if test="event/event-week = '1'">
     <xsl:attribute name="SELECTED">SELECTED</xsl:attribute>
    </xsl:if>
    1
</OPTION>
<OPTION value="2">
    <xsl:if test="event/event-week = '2'">
     <xsl:attribute name="SELECTED">SELECTED</xsl:attribute>
    </xsl:if>
    2
</OPTION>
etc..


You could put the logic in a separate named template, too, and use xsl:call-template to make the check and output the attribute.  Or you could use a recursive routine that added the <OPTION> elements programatically.  That way you'd have an iterator variable to test against.

http://dev.rdcpro.com/Members/rdcpro/snippets/recursionandcounting/

Regards,
Mike Sharp
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:ephemeralz
ID: 9727394
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!
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 9728294
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
0
 

Author Comment

by:ephemeralz
ID: 9730840
Thanks a lot Mike!! Greatly appreciate your help.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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