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

如何在XSLT中仅针对首个value-of元素移除嵌套的credit子节点?

移除XSLT输出中嵌套标签的重复内容

原始XML文档

<doc>
 <article>
  <photo>
   <caption>
    <p>
     caption text<credit>photo copyright</credit>
    </p>
   </caption>
  </photo>
 </article>
</doc>

credit标签嵌套在p标签内,当前使用的XSLT代码如下:

当前XSLT代码

<xsl:template match="/doc">
   <caption>
       <xsl:for-each select="article/photo">
           <xsl:value-of select="caption/p"/>
           <xsl:text>&#xa;</xsl:text>
           <xsl:value-of select="caption/p/credit"/>
       </xsl:for-each>
   </caption>
</xsl:template>

运行后得到的输出存在内容重复:

当前输出

<caption>
    caption text photo copyright
    photo copyright
</caption>

需求

需要让第一个<xsl:value-of>仅保留p标签内的文本,移除嵌套的credit标签及其内容;第二个<xsl:value-of>保留credit的内容,最终输出无重复内容且两者换行:

<caption>
    caption text
    photo copyright
</caption>

解决方案

修改第一个<xsl:value-of>的选择器,指定只获取p标签下的文本节点(而非整个p元素的所有内容),就能排除credit子元素的内容:

<xsl:template match="/doc">
   <caption>
       <xsl:for-each select="article/photo">
           <!-- 仅选取p标签下的文本节点,自动排除credit子元素 -->
           <xsl:value-of select="caption/p/text()"/>
           <xsl:text>&#xa;</xsl:text>
           <xsl:value-of select="caption/p/credit"/>
       </xsl:for-each>
   </caption>
</xsl:template>

这样运行后就能得到符合需求的输出,既保留了caption文本和版权信息的换行,又避免了内容重复。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 17:25:00