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

如何在XSL中移除输出值末尾的<br />标签?

解决方案:移除<g:google_product_category>值末尾的<br />

下面是几个针对XSLT的具体实现方案,你可以根据自己的XSLT版本和实际场景选择:

方法1:使用replace()函数(XSLT 2.0+)

如果你的环境支持XSLT 2.0或更高版本,直接用字符串替换函数精准清除末尾的<br />,这是最直观的方式:

<xsl:element name="g:google_product_category">
  <xsl:value-of select="replace(google_shopping_category, '&lt;br /&gt;$', '')" />
</xsl:element>

这里的正则表达式'&lt;br /&gt;$'专门匹配仅出现在字符串末尾的<br />,能避免误删中间可能存在的相同标签。

方法2:使用substring-before()(兼容XSLT 1.0)

如果只能用XSLT 1.0(无正则支持),可以利用<br />在末尾的特性,截取它之前的内容:

<xsl:element name="g:google_product_category">
  <xsl:choose>
    <xsl:when test="contains(google_shopping_category, '&lt;br /&gt;')">
      <xsl:value-of select="substring-before(google_shopping_category, '&lt;br /&gt;')" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="google_shopping_category" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:element>

这个方案会先判断字符串是否包含<br />,有则截取标签前的内容,没有就直接输出原内容,避免出现空值问题。

方法3:提取纯文本内容(彻底剥离所有HTML标签)

如果google_shopping_category里可能还有其他HTML标签,不只<br />,可以直接提取节点的纯文本内容,彻底去掉所有HTML标记:

<xsl:element name="g:google_product_category">
  <xsl:value-of select="google_shopping_category/text()" />
</xsl:element>

注意:如果google_shopping_category是包含子节点的XML元素(而非纯字符串),这个方法会自动忽略所有HTML标签,只取出它的文本内容。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:18:55