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

使用XSL将<ki>元素值填充至空<td>元素时输出不符合预期求助

问题根因

你当前代码出现多值拼接的核心原因是following-sibling::ki会匹配指定<p>标签之后所有同级的<ki>节点,而非仅当前<p>对应的紧邻第一个<ki>元素,因此会把后续所有<ki>的值拼接输出。

修正方案

仅需在<ki>节点的选择路径后添加[1]限定取首个匹配的紧邻元素即可,同时可删除冗余的for-each循环提升执行效率,修正后的完整XSL代码如下:

<?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:output method="xml" indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="td[ancestor::tbody[parent::table/@id = 't001'] and not(normalize-space(.)) and preceding-sibling::td/p/a/@href]">
        <xsl:variable name="tdhref" select="preceding-sibling::td/p/a/substring-after(@href, '#')"/>
        <td>
            <xsl:attribute name="style">
                <xsl:value-of select="@style"/>
            </xsl:attribute>
            <!-- 限定仅取当前<p>后第一个<ki>的值 -->
            <xsl:value-of select="ancestor::kita/kk/p[@id = $tdhref][1]/following-sibling::ki[1]"/>
            <xsl:apply-templates/>
        </td>
    </xsl:template>
</xsl:stylesheet>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 13:57:03