首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XSLT比较和删除XML中的重复项

使用XSLT比较和删除XML中的重复项
EN

Stack Overflow用户
提问于 2016-03-16 15:08:30
回答 1查看 1.8K关注 0票数 0

我跟踪了XML文档:

代码语言:javascript
复制
<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>

我需要输出(删除重复记录):

代码语言:javascript
复制
<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设置了条件,输出就会选择所有三条记录

我的守则:

代码语言:javascript
复制
<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>

我想使用但不起作用的代码:

代码语言:javascript
复制
<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>

任何帮助都将不胜感激。对不起,这是我的第一篇文章,所以可能不是在正确的地方张贴或以正确的格式张贴。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-16 17:25:58

样式表显示了2.0版本,因此假设您确实在使用XSLT2.0过程,您可以在这里使用xsl:for-each-group。有效地通过Organization_ID、Organization_Code和Organization_Type的连接进行分组,但只输出每个组中的第一个元素,从而删除重复的元素。

试试这个XSLT

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36039603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档