我跟踪了XML文档:
<root>
<Organization>
<Organization_ID >111111</Organization_ID>
<Organization_Code>ABC</Organization_Code>
</Organization>
<Organization>
<Organization_ID >111111</Organization_ID>
<Organization_Code>ABC</Organization_Code>
</Organization>
<Organization>
<Organization_ID >111111</Organization_ID>
<Organization_Code>ABCD</Organization_Code>
<Organization_Type>Test</Organization_Type>
</Organization>
</root>我需要输出(删除重复记录):
<root>
<Organization>
<Organization_ID>111111</Organization_ID>
<Organization_Code>ABC</Organization_Code>
</Organization>
<Organization>
<Organization_ID>111111</Organization_ID>
<Organization_Code>ABCD</Organization_Code>
<Organization_Type>Test</Organization_Type>
</Organization>
</root>我已经写了一段代码,下面可以这样做。我的问题是,我们需要比较所有的子元素,看它们是否是完全重复的。一旦我为Organization_Type设置了条件,输出就会选择所有三条记录
我的守则:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Organization">
<xsl:if
test="
(not(following::Organization[Organization_ID = current()/Organization_ID])
or not(following::Organization[Organization_Code = current()/Organization_Code])
)">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>我想使用但不起作用的代码:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Organization">
<xsl:if
test="
(not(following::Organization[Organization_ID = current()/Organization_ID])
or not(following::Organization[Organization_Code = current()/Organization_Code])
or not(following::Organization[Organization_Type = current()/Organization_Type])
)">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>任何帮助都将不胜感激。对不起,这是我的第一篇文章,所以可能不是在正确的地方张贴或以正确的格式张贴。
发布于 2016-03-16 17:25:58
样式表显示了2.0版本,因此假设您确实在使用XSLT2.0过程,您可以在这里使用xsl:for-each-group。有效地通过Organization_ID、Organization_Code和Organization_Type的连接进行分组,但只输出每个组中的第一个元素,从而删除重复的元素。
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="root">
<xsl:copy>
<xsl:for-each-group select="Organization" group-by="concat(Organization_ID, '|', Organization_Code, '|', Organization_Type)">
<xsl:apply-templates select="." />
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/36039603
复制相似问题