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

XSLT 3.0如何将product下main_image与image_*元素文本逗号拼接合并

问题原因

你的代码存在三个核心错误:

  1. 匹配到main_image节点后没有重新创建该元素标签,导致处理后的内容没有被包裹到对应的节点中,自然看不到main_image的输出。
  2. 路径引用错误:当前模板的上下文就是main_image节点本身,../../../main_image是向上回溯3层到shop节点下查找main_image,该节点不存在,判断条件永远不成立,返回空值。而所有image_*节点是和main_image同级的product子元素,需要使用../image_*才能正确选中。
  3. 拼接逻辑错误:你将image_*内容放在主图内容前,且没有统一做逗号拼接,会出现顺序错误或多余分隔符的问题。

修正后的完整代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   exclude-result-prefixes="xs"
   version="2.0">
   
   <xsl:mode on-no-match="shallow-copy"/>
   
   <xsl:template match="main_image">
      <xsl:copy>
         <xsl:value-of select="string-join((., ../image_*/text()), ',')"/>
      </xsl:copy>
   </xsl:template>
   
</xsl:stylesheet>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 05:18:05