RE: Removing duplicate elements a-priori?

Subject: RE: Removing duplicate elements a-priori?
From: Kay Michael <Michael.Kay@xxxxxxx>
Date: Mon, 19 Jun 2000 10:28:09 +0100
You haven't defined your requirement very clearly, but let's guess that your
requirement is to copy the first instance of each child element type of the
<doc> element.

My first attempt was:

<xsl:template match="doc">
<xsl:apply-templates select="*[not(name(.)=name(preceding-sibling::*)]"/>
</xsl:template>

But of course this doesn't work, because name() applied to a node-set
produces one name, not a set of names.

So try the Muenchian solution using keys:

<xsl:key name="namekey" match="doc/*" use="concat(generate-id(..), '/',
name()"/>

<xsl:template match="doc">
<xsl:apply-templates select="*[generate-id()=
generate-id(key('namekey', concat(generate-id(..), '/', name()))]"/>
</xsl:template>

Quicker to write than to explain. The key definition ensures that all <X>
children of the same <doc> element have the same key value, for each element
type X. The select expression includes every child of a <doc> element
provided it is the same node (established by comparing the result of
generate-id()) as the first entry in the list of nodes with that key value.

Not tested.

> 
> 	For example I want to take :
> 
> 	<doc>
> 	<employee>Bill</employee>
> 	<employee>Andy</employee>
> 	<employee>John</employee>	
> 	</doc>
> 
> 	And produce just :
> 
> 	<doc>
> 	<employee>Bill</employee>
> 	</doc>
> 
> 	without knowing that there is an employee tag in the input.
> 
> 	Any help would be most appreciated.
> 
> 	Thanks,
> 
> 	Gordon Vidaver
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread