Solved

xsl template for option selected drop-down box

Posted on 2003-11-11
6
890 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

756 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