[xsl] Removing Duplicate Nodes

Subject: [xsl] Removing Duplicate Nodes
From: "agiuswiltonites@xxxxxxxx" <agiuswiltonites@xxxxxxxx>
Date: Sun, 18 Mar 2012 07:09:14 GMT
II am using XSLT 1.0 in an asp environment. I searched the faq and archives
and have not found an example I can go by. I am trying to figure out how to
remove duplicate <loc>. If <loc> is a duplicate then remove the containing
<url> and all its contents. I have my xslt somewhat working. It removes the
<loc> if it is a duplicate, but leaves the leftover <lastmod>, <priority>, and
<changefreq> for each duplicate. Any help would be greatly appreciated. Jake

Here is my XSLT Code:

<?startSampleFile ?>
<!-- xq495.xsl: converts xq494.xml into xq496.xml -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0"
xmlns:s="http://www.sitemaps.org/schemas/sitemap/0.9";
exclude-result-prefixes="s">

<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:key name="kloc" match="s:loc"
use="." />
<xsl:key name="klastmod" match="s:lastmod"
use="../s:loc"/>
<xsl:key name="kpriority" match="s:priority"
use="../s:loc"/>
<xsl:key name="kchangefreq" match="s:changefreq"
use="../s:loc"/>

<xsl:template match="/*">

<xsl:for-each select=
"*/s:loc[generate-id()
=
generate-id(key('kloc',.)[1])]">
<xsl:value-of select="."/>
<xsl:for-each select="key('klastmod', .)">
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:for-each select="key('kpriority', .)">
<xsl:value-of select="."/>
</xsl:for-each>
<xsl:for-each select="key('kchangefreq', .)">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

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>

____________________________________________________________
3 Free Credit Scores
Equifax&#174;: Help Guard Your Identity. Sign up for a 30 Day Free Trial!
http://thirdpartyoffers.juno.com/TGL3131/4f658a7e411d721ec39est03vuc

Current Thread