Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: remove multiple elements having same child element content

Reply
Thread Tools

XSLT: remove multiple elements having same child element content

 
 
Johannes Koch
Guest
Posts: n/a
 
      03-05-2004
How to remove multiple elements with the same child element content?

E.g. input:
<root>
<foo>
<bar>ABC</bar>
</foo>
<foo>
<bar>DEF</bar>
</foo>
<foo>
<bar>ABC</bar>
</foo>
<foo>
<bar>ABC</bar>
</foo>
</root>

The other foo elements with bar='ABC' should be removed.

output:
<root>
<foo>
<bar>ABC</bar>
</foo>
<foo>
<bar>DEF</bar>
</foo>
</root>
--
Johannes Koch
In te domine speravi; non confundar in aeternum.
(Te Deum, 4th cent.)
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-05-2004


Johannes Koch wrote:

> How to remove multiple elements with the same child element content?
>
> E.g. input:
> <root>
> <foo>
> <bar>ABC</bar>
> </foo>
> <foo>
> <bar>DEF</bar>
> </foo>
> <foo>
> <bar>ABC</bar>
> </foo>
> <foo>
> <bar>ABC</bar>
> </foo>
> </root>
>
> The other foo elements with bar='ABC' should be removed.
>
> output:
> <root>
> <foo>
> <bar>ABC</bar>
> </foo>
> <foo>
> <bar>DEF</bar>
> </foo>
> </root>


I think defining a key and using generate-id is one way:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xslutput method="xml" indent="yes" />

<xsl:key name="barKey" match="foo" use="bar" />

<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="foo[generate-id(.) =
generate-id(key('barKey', ./bar))]" />
</xsl:copy>
</xsl:template>

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

</xsl:stylesheet>
--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove child element that was dynamically created BananaQuaalude Javascript 2 12-23-2007 04:58 AM
Remove parent element with a child element matching a given rule patrizio.trinchini@googlemail.com XML 4 08-22-2006 11:31 AM
XSLT: iterating all child elements and accessing homonymous childrenin sibling elements Gerald Aichholzer XML 2 06-27-2006 03:46 PM
The element 'compilation' has invalid child element 'compilers'. =?Utf-8?B?Um9iZXJ0?= ASP .Net 3 11-15-2005 01:48 AM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments