首页 > 解决方案 > 使用 XSLT 删除重复标记及其在 XML 中的子项

问题描述

所以,我想从我的 xml 中删除所有重复的标签及其子标签(如果存在)。此示例来自此处如何使用 xslt 删除重复的 xml 节点? 这与我的问题几乎相同,除了它对我不起作用的解决方案而且我不知道为什么。

示例:xml 看起来像:

<root>
       <row>
            <title>The Oscars Opening Ceremony: Live from the Red Carpet</title>  <!-- here -->
            <actors>Margot Robbie</actors>
            <actors>Kumail Nanjiani</actors>
            <actors>Timothée Chalamet</actors>
            <actors>Matthew McConaughey</actors>
            <actors>Nicole Kidman</actors>
            <actors>Saoirse Ronan</actors>
            <actors>Jennifer Garner</actors>
            <actors>Armie Hammer</actors>
            <actors>Sandra Bullock</actors>
            <actors>Gary Oldman</actors>
            <actors>Mira Sorvino</actors>
            <actors>Salma Hayek</actors>
            <actors>Mahershala Ali</actors>
            <actors>Jordan Peele</actors>
            <actors>Wendi McLendon-Covey</actors>
            <description>The Oscars Opening</description>
        </row>
       <row>
            <title>Tabaluga tivi</title>
            <actors>Ben Bledsoe</actors>
            <actors>Philipp Wimmer</actors>
            <actors>Patrick King Jr.</actors>
            <description>Tabaluga tivi</description>
        </row>
        <row>
            <title>Library of God</title>
            <actors>Peter Førde</actors>
            <actors>Lasse Vermeli</actors>
            <actors>Hilde Amundsen</actors>
            <description>Library of God</description>
        </row>
        <row>
            <title>The Oscars Opening Ceremony: Live From The Red Carpet</title> <!-- here again -->
            <actors>Mel Gibson</actors>
            <actors>Dwayne Johnson</actors>
            <actors>Nicole Kidman</actors>
            <actors>Robin Roberts</actors>
            <actors>Meryl Streep</actors>
            <actors>Justin Timberlake</actors>
            <description>Interviews with nominees, presenters and performers arriving for the awards ceremony; hosts Robin Roberts, Michael Strahan and Lara Spencer.</description>
        </row>
</root>

理想的输出结果:

<root>
        <row>
            <title>The Oscars Opening Ceremony: Live from the Red Carpet</title>  <!-- only this one at result -->
            <actors>Margot Robbie</actors>
            <actors>Kumail Nanjiani</actors>
            <actors>Timothée Chalamet</actors>
            <actors>Matthew McConaughey</actors>
            <actors>Nicole Kidman</actors>
            <actors>Saoirse Ronan</actors>
            <actors>Jennifer Garner</actors>
            <actors>Armie Hammer</actors>
            <actors>Sandra Bullock</actors>
            <actors>Gary Oldman</actors>
            <actors>Mira Sorvino</actors>
            <actors>Salma Hayek</actors>
            <actors>Mahershala Ali</actors>
            <actors>Jordan Peele</actors>
            <actors>Wendi McLendon-Covey</actors>
            <description>The Oscars Opening</description>
        </row>
       <row>
            <title>Tabaluga tivi</title>
            <actors>Ben Bledsoe</actors>
            <actors>Philipp Wimmer</actors>
            <actors>Patrick King Jr.</actors>
            <description>Tabaluga tivi</description>
        </row>
        <row>
            <title>Library of God</title>
            <actors>Peter Førde</actors>
            <actors>Lasse Vermeli</actors>
            <actors>Hilde Amundsen</actors>
            <description>Library of God</description>
        </row>
   </root>

这是我正在使用的 xslt:

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

为什么不删除重复的?感谢任何帮助。谢谢

标签: xmlxsltduplicatesnodes

解决方案


title如果title您只想比较并且它们完全相等,则只需使用该元素即可:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
  <xsl:key name="kRowByTitle" 
           match="row"
           use="title"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="row[generate-id() != generate-id(key('kRowByTitle', title)[1])]"/>

</xsl:stylesheet>

请注意,但是在您发布的示例中,第一个和第四个字母的大小写有所不同,title因此您可能还需要在 XSLT 2 及更高版本中使用该lower-case函数,或者您需要声明两个参数,其中包含您需要在上部处理的所有字母-case 和小写并使用translate.

在 XSLT 3 中,它只是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">
    
  <xsl:key name="kRowByTitle" 
           match="row"
           use="lower-case(title)"/>

  <xsl:mode on-no-match="shallow-copy"/>
  
  <xsl:template match="row[not(. is key('kRowByTitle', lower-case(title))[1])]"/>

</xsl:stylesheet>

推荐阅读