如何使用 xslt 删除重复的 xml 节点?

2024-01-11

当所有变量都使用 xslt 完全匹配时,我想删除重复项。

在此 xml 中,应删除节点 3,因为它是节点 1 的完美副本。

<root> 
    <trips> 
      <trip> 
        <got_car>0</got_car> 
        <from>Stockholm, Sweden</from> 
        <to>Gothenburg, Sweden</to> 
        <when_iso>2010-12-06 00:00</when_iso> 
      </trip>
      <trip> 
        <got_car>0</got_car> 
        <from>Stockholm, Sweden</from> 
        <to>New york, USA</to> 
        <when_iso>2010-12-06 00:00</when_iso> 
      </trip>
      <trip> 
        <got_car>0</got_car> 
        <from>Stockholm, Sweden</from> 
        <to>Gothenburg, Sweden</to> 
        <when_iso>2010-12-06 00:00</when_iso> 
      </trip>
      <trip> 
        <got_car>1</got_car> 
        <from>Test, Duncan, NM 85534, USA</from> 
        <to>Test, Duncan, NM 85534, USA</to> 
        <when_iso>2010-12-06 00:00</when_iso> 
      </trip> 
    <trips> 
<root>

经过更好的设计,这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="kTripByContent" match="trip"
             use="concat(got_car,'+',from,'+',to,'+',when_iso)"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="trip[generate-id() !=
                              generate-id(key('kTripByContent',
                                              concat(got_car,'+',
                                                     from,'+',
                                                     to,'+',
                                                     when_iso))[1])]"/>
</xsl:stylesheet>

Output:

<root>
    <trips>
        <trip>
            <got_car>0</got_car>
            <from>Stockholm, Sweden</from>
            <to>Gothenburg, Sweden</to>
            <when_iso>2010-12-06 00:00</when_iso>
        </trip>
        <trip>
            <got_car>0</got_car>
            <from>Stockholm, Sweden</from>
            <to>New york, USA</to>
            <when_iso>2010-12-06 00:00</when_iso>
        </trip>
        <trip>
            <got_car>1</got_car>
            <from>Test, Duncan, NM 85534, USA</from>
            <to>Test, Duncan, NM 85534, USA</to>
            <when_iso>2010-12-06 00:00</when_iso>
        </trip>
    </trips>
</root>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 xslt 删除重复的 xml 节点? 的相关文章

随机推荐