XSL中for-each循环过滤重复节点

本文深入探讨了XSL语法中如何使用xsl:key和generate-id()函数来筛选具有相同属性的节点,通过示例代码详细解析了这一过程,为读者提供了实用的XSLT编程技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近用到xsl语法,想要根据某个属性去过滤具有相同属性的节点,去网上查找xsl资料很少。

直接上示例代码。

<xsl:key name="testDistinct" match="Tests/Test/TestA" use="@attr"/>
<xsl:template match="/">
    <xsl:for-each select="TestA[generate-id() = generate-id(key('testDistinct', @attr))]">
        <xsl:value-of select="."/>
    </xsl:for-each>
</xsl:template>

注意:

xsl:key定义在xsl:template外,与xsl:template平级;

select所选节点与match节点保持一致;

函数generate-id()用于返回唯一标识指定节点的字符串值。

 

参考资料:https://www.cnblogs.com/jaxu/archive/2011/11/28/2265868.html

<!-- 表格处理(增强版) --> <xsl:template match="w:tbl"> <fo:table table-layout="fixed" width="100%" border-collapse="collapse"> <xsl:for-each select="w:tblGrid/w:gridCol"> <fo:table-column column-width="{@w:w div 20}pt"/> </xsl:for-each> <fo:table-body> <xsl:apply-templates select="w:tr"/> </fo:table-body> </fo:table> </xsl:template> <!-- 表格行(过滤被合并单元格) --> <xsl:template match="w:tr"> <fo:table-row> <!-- 精确过滤被合并单元格:跳过所有标记为continue的单元格 --> <xsl:apply-templates select="w:tc[not(w:tcPr/w:vMerge/@w:val='continue')]"/> </fo:table-row> </xsl:template> <!-- 单元格模板(完整版) --> <xsl:template match="w:tc"> <fo:table-cell border="1pt solid black" padding="2pt" display-align="center"> <!-- 处理列合并 --> <xsl:if test="w:tcPr/w:gridSpan"> <xsl:attribute name="number-columns-spanned"> <xsl:value-of select="w:tcPr/w:gridSpan/@w:val"/> </xsl:attribute> </xsl:if> <!-- 处理行合并 --> <xsl:if test="w:tcPr/w:vMerge[@w:val='restart']"> <!-- 计算实际跨行数 = 下方连续标记为continue的单元格数 + 1 --> <xsl:variable name="rowSpan"> <xsl:value-of select="count(following::w:tr/w:tc[1][w:tcPr/w:vMerge/@w:val='continue']) + 1"/> </xsl:variable> <xsl:attribute name="number-rows-spanned"> <xsl:value-of select="$rowSpan"/> </xsl:attribute> </xsl:if> <fo:block linefeed-treatment="ignore" white-space-collapse="true"> <xsl:apply-templates select=".//w:p"/> </fo:block> </fo:table-cell> </xsl:template>我这个提示The column-number or number of cells in the row overflows the number of fo:table-columns specified for the table. (See position 1:9340),请直接给出修改后的整体代码
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值