XSLT 2.0基于多元素组合实现Report_Entry节点去重方案问询
XML多字段组合去重实现方案
你采用拼接三个字段作为分组唯一键的思路是正确的,可根据你使用的XSLT版本选择以下实现方案:
XSLT 2.0 实现(支持group-by语法)
完整转换代码如下:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/Report_Data"> <xsl:copy> <!-- 按employeeID+plan+startDate的拼接值分组 --> <xsl:for-each-group select="Report_Entry" group-by="string-join((employeeID, plan, startDate), '|')"> <!-- 每个分组仅保留第一个节点 --> <xsl:copy-of select="."/> </xsl:for-each-group> </xsl:copy> </xsl:template> </xsl:stylesheet>
XSLT 1.0 实现(兼容低版本处理器)
采用Muenchian分组法实现,代码如下:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- 定义三个字段拼接的唯一索引键 --> <xsl:key name="uniqueEntry" match="Report_Entry" use="concat(employeeID, '|', plan, '|', startDate)"/> <xsl:template match="/Report_Data"> <xsl:copy> <!-- 过滤出每个唯一键对应的第一个节点 --> <xsl:for-each select="Report_Entry[generate-id() = generate-id(key('uniqueEntry', concat(employeeID, '|', plan, '|', startDate))[1])]"> <xsl:copy-of select="."/> </xsl:for-each> </xsl:copy> </xsl:template> </xsl:stylesheet>
用以上任意一种XSLT对源XML执行转换,即可得到你期望的去重结果。
内容的提问来源于stack exchange,提问作者SreenivasBR
相关产品推荐
相关产品推荐

