Subject: RE: Removing duplicate elements a-priori?
From: Gordon Vidaver <gvidaver@xxxxxxx>
Date: Mon, 19 Jun 2000 15:35:19 -0400
|
Mike,
I'm trying to remove duplicate tags in the xml output. That is,
if the input xml file is :
<doc>
<A/>
<A/>
</doc>
The output should be:
<doc>
<A/>
</doc>
Also, I don't want to have to know there is a tag named "doc" or
"A" in the input document. It would be fine if the output duplicates were
only removed at the level of the children of the root.
I tried this, and it doesn't produce any output:
<xsl:key name="namekey" match="*" use="concat(generate-id(..), '/',
name())"/>
<xsl:template match="*">
<xsl:apply-templates
select="*[generate-id()=generate-id(key('namekey', concat(generate-id(..),
'/', name ())))]"/>
</xsl:template>
I also tried
<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>
which also didn't work.
Any help would be most appreciated...
Thanks,
Gordon
At 10:28 AM 6/19/00 +0100, you wrote:
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
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|