Solved

xsl template for option selected drop-down box

Posted on 2003-11-11
6
881 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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. 
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

16 Experts available now in Live!

Get 1:1 Help Now