473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2880
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: UndoMiel | last post by:
Hi, I have a situation where i would like to validate the occurance of certain elements, based on the value of an attribute. What is the "best" way to handle such validations? I am fairly new...
5
by: Patient Guy | last post by:
In my reading of the Strict and Transitional DTD for HTML 4.0, the table row (TR) elements are contained within table section elements: THEAD, TFOOT, and TBODY. The table section elements are...
2
by: Rob T. | last post by:
Given the following in a stylesheet: #parent { width: 50px; height: 50px; } #child {
6
by: Boobie | last post by:
Is there any other way to remove a node/element other than going into the Parent and do "removeChild" ? thanks (need to remove a div of id "foobar")
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
3
by: Markus | last post by:
Hi! I wanted to select a subset of nodes (list = selectNodes("parent/child") from a XmlDocument, then remove all (parentNode.removeAll();) child-nodes and insert the previous selected nodes...
3
by: patrizio.trinchini | last post by:
Hi, how can remove sibling elements based on the value of an attribute ? For instance, gven the XML document: <root> <parentElment> <testElement name="A"> <removableElement/>
14
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.