468,766 Members | 1,300 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

Remove parent element with a child element matching a given rule

Hi all,

I'm new to XSLT and maybe my problem have a very trivial answer, but I
need an expert that point me in the right direction.
What I would obtain is to remove all the elements that have a child
element with an attribute set at a given value; maybe an example is
more self-explaining...

Given the following input XML document:

<?xml version="1.0" encoding="UTF-8"?>
<sample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNameSpaceSchemaLocation="sample.xsd">
<parent-element>
<child-element name="a child element"/>
<test-element type="removeme">This should be removed</test-element>
</parent-element>
<parent-element>
<child-element name="a child element"/>
<test-element type="keepme">This should be keept</test-element>
</parent-element>
</sample>

I would obtain the following output XML document:

<?xml version="1.0" encoding="UTF-8"?>
<sample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNameSpaceSchemaLocation="sample.xsd">
<parent-element>
<child-element name="a child element"/>
<test-element type="keepme">This should be keept</test-element>
</parent-element>
</sample>

where the first occurrence of the element 'parent-element' has been
removed because one of its child elements (namely the <test-element>
element) has the attribute 'type' whose value is 'removeme'.

I've defined the following XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="test-element[@type='removeme']"/>
</xsl:stylesheet>

but all I'm able to obtain is the following document:

<?xml version="1.0" encoding="UTF-16"?>
<sample xsi:noNameSpaceSchemaLocation="sample.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent-element>
<child-element name="a child element"></child-element>
</parent-element>
<parent-element>
<child-element name="a child element"></child-element>
<test-element type="keepme">This should be keept</test-element>
</parent-element>
</sample>

where only the <test-elementelement that matches the aforementioned
rule hase been removed, not its ancestor

Any suggestion?

Thanks a lot for your help

Patrizio

Aug 17 '06 #1
4 2623
pa****************@googlemail.com wrote:
<xsl:template match="test-element[@type='removeme']"/>
That matches and discards a <test-elementwhich has the specified
attribute (and all its descendants, of course).

If you want to match the parent of that test-element, say so: Any
element which has a test-element child which has the specified type:
<xsl:template match="*[test-element/@type='removeme']"/>
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Aug 17 '06 #2


pa****************@googlemail.com wrote:

<xsl:template match="test-element[@type='removeme']"/>
That should be
<xsl:template match="parent-element[test-element[@type='removeme']]"/>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 17 '06 #3
Martin Honnen wrote:
<xsl:template match="parent-element[test-element[@type='removeme']]"/>
More specific than my use of *, and thus better in most cases unless
test-element will appear in a wide variety of parent elements.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Aug 17 '06 #4

Joseph, Martin

thanks a lot for your precious help, your suggested solutions works
perfectly!

Aug 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Boobie | last post: by
6 posts views Thread by Nikhil Patel | last post: by
3 posts views Thread by Markus | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.