xslt remove duplicate elements

本秂侑毒 提交于 2019-12-02 03:34:01

This XSLT 1.0 solution is shorter and not slower:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kByHref" match="item" use="@href"/>

  <xsl:template match="/*">
    <new_images>
      <xsl:copy-of select=
      "*[generate-id() = generate-id(key('kByHref', @href)[1])]"/>
    </new_images>
  </xsl:template>
</xsl:stylesheet>
user3813234

I got it.... happens to me all the time... got to put the for-each-group in the template for /images...

Updated XSLT 2.0...

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs fo">

  <xsl:output method="xml"/>

  <xsl:strip-space elements="*"/>

  <xsl:template match="/images">
    <new_images>
      <xsl:for-each-group select="item" group-by="@href">
        <xsl:copy-of select="current-group()[1]"/>
      </xsl:for-each-group>
    </new_images>
  </xsl:template>

</xsl:stylesheet>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!