未添加XSLT属性

时间:2010-03-29 08:23:59

标签: xml xslt xslt-1.0

尝试使用下面的xslt代码标记使用XSLT 1.0选择的无线电输入,但这不会产生所需的结果

desrired result

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

实际输出

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

有任何想法为什么不讨好?

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 个答案:

答案 0 :(得分:2)

这是由于您的输出模式。您是否已指示您的XSLT处理器输出HTML(而不是XML)?如果是这样,输出序列化器适用于调整HTML的特性;例如,它生成<br>而不是<br/>,并且如果属性名称相同,则可能会遗漏属性内容。

这应该不是问题;顺便说一下,这是合法的HTML。

了解更多详情;该规范有what exactly html output mode is supposed to do部分。阿蒙斯特说的其他事情......

  

html输出方法应以最小化的形式输出布尔属性(即只有一个允许值等于属性名称的属性)。例如,在样式表中写为

的开始标记
<OPTION selected="selected">
     

应输出为

<OPTION selected>

答案 1 :(得分:0)

试试这个:

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

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