Re: [xsl] Removing Duplicate Nodes

Subject: Re: [xsl] Removing Duplicate Nodes
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Sun, 18 Mar 2012 11:15:40 +0100
agiuswiltonites@xxxxxxxx wrote:

Existing XML:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9";>
<url>
<loc>http://www.example.com/first</loc>
<lastmod>2012-01-27T10:18:26+00:00</lastmod>
<priority>0.90</priority>
<changefreq>daily</changefreq>
</url>
<url>
<loc>http://www.example.com/second</loc>
<lastmod>2012-03-16T07:38:42+00:00</lastmod>
<priority>0.60</priority>
<changefreq>weekly</changefreq>
</url>
<url>
<loc>http://www.example.com/second</loc>
<lastmod>2012-03-16T08:36:44+00:00</lastmod>
<priority>0.60</priority>
<changefreq>weekly</changefreq>
</url>
<url>
<loc>http://www.example.com/third</loc>
<lastmod>2012-03-16T08:37:09+00:00</lastmod>
<priority>0.60</priority>
<changefreq>weekly</changefreq>
</url>
</urlset>

Desired Output:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9";>
<url>
<loc>http://www.example.com/first</loc>
<lastmod>2012-01-27T10:18:26+00:00</lastmod>
<priority>0.90</priority>
<changefreq>daily</changefreq>
</url>
<url>
<loc>http://www.example.com/second</loc>
<lastmod>2012-03-16T07:38:42+00:00</lastmod>
<priority>0.60</priority>
<changefreq>weekly</changefreq>
</url>
<loc>http://www.example.com/third</loc>
<lastmod>2012-03-16T08:37:09+00:00</lastmod>
<priority>0.60</priority>
<changefreq>weekly</changefreq>
</url>
</urlset>

The can be done with Muenchian grouping as follows:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:s09="http://www.sitemaps.org/schemas/sitemap/0.9";
  exclude-result-prefixes="s09"
  version="1.0">

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

<xsl:key name="url-by-loc" match="s09:url" use="s09:loc"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

<xsl:template match="s09:urlset">
<xsl:copy>
<xsl:apply-templates select="s09:url[generate-id() = generate-id(key('url-by-loc', s09:loc)[1])]"/>
</xsl:copy>
</xsl:template>


</xsl:stylesheet>


--


	Martin Honnen --- MVP Data Platform Development
	http://msmvps.com/blogs/martin_honnen/

Current Thread