RE: [xsl] putting selected="selected" in the right place

Subject: RE: [xsl] putting selected="selected" in the right place
From: "M. David Peterson" <m.david@xxxxxxxxxx>
Date: Thu, 25 Mar 2004 20:06:46 -0700
Not sure what your source XML looks like but if it looked like this:

<?xml version="1.0"?>
<select>
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
  <option value="d" selected="true">d</option>
  <option value="e">e</option>
  <option value="f">f</option>
  <option value="g">g</option>
  <option value="h">h</option>
</select>

You could just create two template, one that matched "option[@selected]"
and one that matched "option[not(@selected)]".  So this XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
  <xsl:apply-templates select="select"/>
</xsl:template>
<xsl:template match="select">
  <xsl:element name="select">
    <xsl:apply-templates select="option"/>
  </xsl:element>
</xsl:template>
<xsl:template match="option[@selected]">
  <xsl:element name="option">
    <xsl:attribute name="selected">true</xsl:attribute>
    <xsl:attribute name="value"><xsl:value-of
select="@value"/></xsl:attribute>
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>
<xsl:template match="option[not(@selected)]">
  <xsl:element name="option">
    <xsl:attribute name="value"><xsl:value-of
select="@value"/></xsl:attribute>
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

Would give you this output:

<select>
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
  <option selected="true" value="d">d</option>
  <option value="e">e</option>
  <option value="f">f</option>
  <option value="g">g</option>
  <option value="h">h</option>
</select>

Best of luck!

<M:D/>

-----Original Message-----
From: Donal Regan [mailto:donal_regan10@xxxxxxxxxxx] 
Sent: Thursday, March 25, 2004 7:07 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] putting selected="selected" in the right place

Hello,
Thank you for all the help on previous questions, it
really is appreciated.

Now,I want to display a select box with a default date
determined by some XML input such as 

<Booking>
<checkIn>31-03-2004</checkIn>
<Booking>

How do I  put a ' selected="selected" ' statement in
the appropriate option tag without having an "if"
statement around each option?
Is this the right way to go about it?Should I be
thinking of using recursive templates?


<xsl:template match="checkIn">
	<table border="0" cellspacing="0" cellpadding="2">
                    <tr>
                    <td nowrap="nowrap"> <select
name="year" class="box60">
                        <option>2004</option>
                        <option>2005</option>
                    </select>
                    </td>
                    <td nowrap="nowrap"> <select
name="month" class="box40">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                        <option>12</option>
                    </select>
                    </td>
                    <td nowrap="nowrap"><select
name="day" class="box40">
                        <option>1</option>
                        <option>2</option>
                        <option>3</option>
                        <option>4</option>
                        <option>5</option>
                        <option>6</option>
                        <option>7</option>
                        <option>8</option>
                        <option>9</option>
                        <option>10</option>
                        <option>11</option>
                        <option>12</option>
                        <option>13</option>
                        <option>14</option>
                        <option>15</option>
                        <option>16</option>
                        <option>17</option>
                        <option>18</option>
                        <option>19</option>
                        <option>20</option>
                        <option>21</option>
                        <option>22</option>
                        <option>23</option>
                        <option>24</option>
                        <option>25</option>
                        <option>26</option>
                        <option>27</option>
                        <option>28</option>
                        <option>29</option>
                        <option>30</option>
                        <option>31</option>
                    </select>
                    </td>
                    </tr>
                </table> 

</xsl:template>


	
	
		
___________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html

Current Thread