tags:

views:

76

answers:

2

Trying to mark radio inputs as selected with XSLT 1.0 using the below xslt code but this does not produced the desired result

desrired result

<input type="radio" value="available" title="email" selected="selected" />

Actual output

  <input type="radio" value="available" title="email" selected />

Anyone any ideas why not please?

XSLT

<xsl:variable name="selected">selected</xsl:variable>
  <xsl:for-each select="item">
   <tr>

     <td><xsl:value-of select="title" /></td>

     <td>
       <input type="radio" value="available" >
       <xsl:attribute name="name">
        <xsl:value-of select="title" />
       </xsl:attribute>
       <xsl:if test="category='available'">
         <xsl:attribute name="selected">
          <xsl:value-of select="$selected"/>
         </xsl:attribute>
       </xsl:if>
       </input>
     </td>

     <td>
       <input type="radio" value="unavailable" >
       <xsl:attribute name="name">
        <xsl:value-of select="title" />
       </xsl:attribute>
       <xsl:if test="category='unavailable'">
        <xsl:attribute name="selected">
         <xsl:value-of select="$selected"/>
        </xsl:attribute>
       </xsl:if>
       </input>
     </td>


     <td>
       <input type="radio" value="warning" >

       <xsl:if test="category='warning'">
        <xsl:attribute name="selected">
            <xsl:value-of select="$selected"/>
           </xsl:attribute>
           <xsl:attribute name="name">
        <xsl:value-of select="title" />
       </xsl:attribute>
       </xsl:if>
       </input>
     </td>

   </tr>

   </xsl:for-each>
+2  A: 

This is due to your output mode. Have you instructed your XSLT processor to output HTML (rather than XML)? If so, the output serializer is adapted to adjust for the idiosyncracies of HTML; so that for instance it generates <br> rather than <br/> and that it may leave out attribute content if identical to attribute name.

This shouldn't be a problem; it's legal HTML, by the way.

For more details; the spec has a section on what exactly html output mode is supposed to do. Amonst other things it says...

The html output method should output boolean attributes (that is attributes with only a single allowed value that is equal to the name of the attribute) in minimized form. For example, a start-tag written in the stylesheet as

<OPTION selected="selected">

should be output as

<OPTION selected>
Eamon Nerbonne
Thanks very much, that was it. Also realised I should be using checked="checked"
Shaun Hare
Ah yes, that mistake makes it easy to trip over XSLT arcana ;-) - good catch! (You might be able to catch that kind of error using the HTML validator - that should probably detect attributes that don't make sense.)
Eamon Nerbonne
A: 

Try this:

<xsl:variable name="selected" select="selected"/>

http://www.w3schools.com/xsl/el_variable.asp

philcolbourn