【发布时间】:2020-11-22 09:31:29
【问题描述】:
所以,我想从我的 xml 中删除所有重复的标签及其子标签(如果存在)。这个例子来自这里How to remove duplicate xml-nodes using xslt? 这与我的问题几乎相同,除了解决方案对我不起作用而且我不知道为什么。
示例: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>
为什么不删除重复的?感谢任何帮助。谢谢
【问题讨论】:
-
也许只使用
title作为密钥?目前尚不清楚您希望使用连接键值实现什么,您似乎有多个不同顺序的actors元素。 -
@MartinHonnen 我想删除一个
并且它是孩子,每次我发现
的重复值。整个 XML 必须只有唯一的 ... -
您只需要在键声明中使用
use="title",但是请注意,对于您的示例,就纯粹的、不区分大小写的字符串比较而言,标题不是重复的,因为有Live from the Red和另一个Live from The Red。因此,您需要在 XSLT 2 及更高版本中额外使用小写字母,或者编写一个长translate调用,将 XSLT 1 中的所有字母转换为小写字母。
标签: xml xslt duplicates nodes