使用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
相关产品推荐
相关产品推荐

