[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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
removing duplicate elements in xslt
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.