Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Remove parent element with a child element matching a given rule

Reply
Thread Tools

Remove parent element with a child element matching a given rule

 
 
patrizio.trinchini@googlemail.com
Guest
Posts: n/a
 
      08-17-2006
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 xmlnssi="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 xmlnssi="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 xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xslutput 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"
xmlnssi="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-element> element that matches the aforementioned
rule hase been removed, not its ancestor

Any suggestion?

Thanks a lot for your help

Patrizio

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-17-2006


http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
Joseph Kesselman
Guest
Posts: n/a
 
      08-17-2006
(E-Mail Removed) wrote:
> <xsl:template match="test-element[@type='removeme']"/>


That matches and discards a <test-element> which 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
 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      08-17-2006
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
 
Reply With Quote
 
patrizio.trinchini@googlemail.com
Guest
Posts: n/a
 
      08-22-2006

Joseph, Martin

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

 
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
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
adding new parent element by matching child attribute value madmaler@googlemail.com XML 1 02-19-2008 11:37 AM
how to add validation rule for url in the validation-rule.xml ,I added some thing like this but......... shailajabtech@gmail.com Java 0 10-12-2006 08:36 AM
Parent - Child back to parent javascript thingybob mark ASP .Net 1 03-10-2005 02:20 PM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM



Advertisments