You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:使用XSLT为XML中不同属性的<p>标签设置颜色

问题分析与修正

你的XSLT代码存在两个核心问题:

  • 模板已匹配<p>节点,判断属性和取值时无需额外添加p/前缀,直接引用当前节点即可
  • 颜色映射完全颠倒:你把red属性对应成了绿色(#00ff00),green属性对应成了红色(#ff0000)

修正后的XSLT代码如下:

<xsl:template match="p">
  <xsl:choose>
    <xsl:when test="@color='red'">
      <span style="color:#ff0000"><xsl:value-of select="."/></span>
    </xsl:when>
    <xsl:when test="@color='green'">
      <span style="color:#00ff00"><xsl:value-of select="."/></span>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

关键修正点说明

  • @color='red'直接检查当前<p>节点的color属性,替代原错误的p/@color='red'
  • <xsl:value-of select="."/>获取当前<p>节点的文本内容,替代原错误的select="p"
  • 修正颜色值对应关系,让red属性显示红色,green属性显示绿色

内容的提问来源于stack exchange,提问作者Russet_potato97

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 00:23:37