Solved

xsl template for option selected drop-down box

Posted on 2003-11-11
6
878 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

18 Experts available now in Live!

Get 1:1 Help Now