【问题标题】:XSLT Attribute not being added未添加 XSLT 属性
【发布时间】:2010-03-29 08:23:59
【问题描述】:

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

想要的结果

<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>

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

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

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

    更多详情;该规范有一个关于what exactly html output mode is supposed to do 的部分。除了它所说的其他事情......

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

    <OPTION selected="selected">
    

    应该输出为

    <OPTION selected>
    

    【讨论】:

    • 非常感谢,就是这样。还意识到我应该使用checked="checked"
    • 啊,是的,这个错误很容易被 XSLT arcana 绊倒;-) - 很好! (您也许可以使用 HTML 验证器捕获这种错误 - 这可能会检测到没有意义的属性。)
    【解决方案2】:

    试试这个:

    &lt;xsl:variable name="selected" select="selected"/&gt;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-11
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多