gpt4 book ai didi

xml - 使用 XSLT 从我的 XML 文件中删除重复元素

转载 作者:数据小太阳 更新时间:2023-10-29 02:01:37 24 4
gpt4 key购买 nike

这是一个示例,如果 ID 相同,我想删除重复的条目。我正在从系统“A”和系统“B”中提取匹配项。我希望系统“A”具有优先权(即,如果 ID 重复,则从系统“B”中删除该元素)。这是我的例子:

我得到这个结果:

<HitList>
<Hit System="A" ID="1"/>
<Hit System="A" ID="2"/>
<Hit System="A" ID="2"/>
<Hit System="B" ID="1"/>
<Hit System="B" ID="2"/>
<Hit System="B" ID="3"/>
<Hit System="B" ID="4"/>
</HitList>

I want this result (with the duplicates removed):

<HitList>
<Hit System="A" ID="1"/>
<Hit System="A" ID="2"/>
<Hit System="B" ID="3"/>
<Hit System="B" ID="4"/>
</HitList>

当前代码:

        <xsl:template match="/RetrievePersonSearchDataRequest">
<HitList>
<xsl:if test="string(RetrievePersonSearchDataRequest/SystemA/NamecheckResponse/@Status) = string(Succeeded)">
<xsl:for-each select="SystemA/NamecheckResponse/BATCH/ITEMLIST/ITEM/VISQST/NCHITLIST/NCHIT">
<Hit>
<xsl:attribute name="System"><xsl:text>A</xsl:text></xsl:attribute>
<xsl:attribute name="PersonID"><xsl:value-of select="number(
REFUSAL/@UID)"/></xsl:attribute>
</Hit>
</xsl:for-each>
</xsl:if>
<xsl:if test="string(RetrievePersonSearchDataRequest/SystemB/NamecheckResponse/@Status) = string(Succeeded)">
<xsl:for-each select="SystemB/NamecheckResponse/PersonIDSearchResponse/personID">
<Hit>
<xsl:attribute name="System"><xsl:text>B</xsl:text></xsl:attribute>
<xsl:attribute name="PersonID"><xsl:value-of select="number(.)"/></xsl:attribute>
</Hit>
</xsl:for-each>
</xsl:if>
</HitList>
</xsl:template>

最佳答案

这是一个使用键的高效 XSLT 1.0 解决方案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="kHitById" match="Hit" use="@ID"/>
<xsl:key name="kHitAById" match="Hit[@System = 'A']" use="@ID"/>

<xsl:template match=
"Hit[generate-id() = generate-id(key('kHitById',@ID)[1])]">

<xsl:copy-of select=
"key('kHitAById', @ID)[1]|current()[not(key('kHitAById', @ID))]"/>
</xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时(有意改编自提供的文档,通过在相应的 B 之前放置一些 A 使其更有趣):

<HitList>
<Hit System="B" ID="1"/>
<Hit System="A" ID="1"/>
<Hit System="B" ID="2"/>
<Hit System="A" ID="2"/>
<Hit System="A" ID="2"/>
<Hit System="B" ID="3"/>
<Hit System="B" ID="4"/>
</HitList>

产生了想要的、正确的结果:

<Hit System="A" ID="1"/>
<Hit System="A" ID="2"/>
<Hit System="B" ID="3"/>
<Hit System="B" ID="4"/>

关于xml - 使用 XSLT 从我的 XML 文件中删除重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15165158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com