By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

XSLT: remove multiple elements having same child element content

P: n/a
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.)
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


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 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output 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/

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.