473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove parent element if there are no children

CI
I have the following XML file

<Book>
<Chapters>
<Chapter number="1"/>
<Chapter number="2"/>
</Chapters>
<Colors>
<Color RGB="255"/>
<Color RGB="211" name="Red"/>
<Color name="Yellow"/>
<Color RGB="127"/>
</Colors>
</Book>

I want to get a tree with the <Colorelement s that don't have RGB
attribute:

So I came up with this XSLT sheet.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/
Transform">
<xsl:template match="@*|node( )">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Color">

<xsl:choose>
<xsl:when test="@RGB">

</xsl:when>
<xsl:otherwis e>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>

</xsl:template>

</xsl:stylesheet>

It works correctly, but if in my XML file all the <Colorelement s
have RGB attribute the resulted tree
contains empty <Colors></Colorsnode.

Anyone had an idea how to conditionally remove it?

Regards,

Michael

Apr 13 '07
15 3991

"Joe Kesselman" <ke************ @comcast.netwro te in message
news:Y8******** *************** *******@comcast .com...
p.*****@ctncorp .com wrote:
>On a side note, XSLT2 is bliss:

A minor change, but a good one: XSLT removed the distinction between
Result Tree Fragments and Node Sets (both are now grouped together as
Temporary Trees), so one doesn't need the node-set extension to convert
the former into the latter.

Outside of that, this is the same two-past solution I suggested.

C'mon people,

Do you know how much additional memory such a solution may use? Twice the
memory of one-pass solution.

Where possible, do try to "fuse" (the term is "fusion" or "deforestation" ) a
pipeline of passes into a single pass.

And, of course, I am not rediscovering the wheel here...
Cheers,
Dimitre Novatchev.
Apr 14 '07 #11
Dimitre Novatchev wrote:
Do you know how much additional memory such a solution may use? Twice the
memory of one-pass solution.
It may, or it may not, depending on the internal data model of the
processor. And for simple input documents, that really may not matter.

In fact, what I'd do if taking this approach was to do two-pass
processing only locally, on the element which requires it -- so the
overhead is both smaller and transient.

It can be taken further... but see first paragraph; before spending too
much effort on optimization, consider whether the improvement is worth
the effort. Learn to do it right, but also learn when good enough is
good enough.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 14 '07 #12
On Apr 14, 6:26 pm, "Dimitre Novatchev"
<dimit...@tpg.c om.auwrote:
"Joe Kesselman" <keshlam-nos...@comcast. netwrote in
message
news:Y8******** *************** *******@comcast .com...
p.le...@ctncorp .com wrote:
On a side note, XSLT2 is bliss:
A minor change, but a good one: XSLT removed the
distinction between Result Tree Fragments and Node Sets
(both are now grouped together as Temporary Trees), so
one doesn't need the node-set extension to convert the
former into the latter.
Outside of that, this is the same two-past solution I
suggested.

Do you know how much additional memory such a solution
may use? Twice the memory of one-pass solution.

Where possible, do try to "fuse" (the term is "fusion" or
"deforestation" ) a pipeline of passes into a single pass.
I don't know about you, but premature optimization is a sin
in my book. Go for clarity and neatness first, deal with
performance issues as they arise.

--
roy axenov

Apr 14 '07 #13
roy axenov wrote:
I don't know about you, but premature optimization is a sin
in my book. Go for clarity and neatness first, deal with
performance issues as they arise.
Especially when still teaching basic concepts.

Steve Boies: "Make it work, make it good, make it great."
(Chorus response: "In that order!")
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 14 '07 #14

"Joe Kesselman" <ke************ @comcast.netwro te in message
news:99******** *************** *******@comcast .com...
roy axenov wrote:
>I don't know about you, but premature optimization is a sin
in my book. Go for clarity and neatness first, deal with
performance issues as they arise.

Especially when still teaching basic concepts.

Steve Boies: "Make it work, make it good, make it great."
(Chorus response: "In that order!")
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Yes, and following all these recommendations I'd still prefer the most
compact and elegant solution (sorry, it happens to be the one I proposed
) )

I didn't spend any time trying to optimize -- just chose the most *natural*
solution, which for XSLT happens to be overriding the identity rule -- and
this is so for more than 90% of all cases.
Cheers,
Dimitre Novatchev.
Apr 14 '07 #15
Dimitre Novatchev wrote:
I didn't spend any time trying to optimize -- just chose the most *natural*
solution, which for XSLT happens to be overriding the identity rule -- and
this is so for more than 90% of all cases.
I agree that this is the core starting point for most XSLT design.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 14 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11779
by: Christopher Benson-Manica | last post by:
I have an HTMLInputElement in a <td>. I want to access all the <td>'s in the same row as the input element. var tr=theInputElement.parentNode.parentNode; Why isn't the input element's parent's parent the <tr> node in Firefox? Internet Explorer seems to think so. How can I get the <tr> node (so I can iterate through its children) in Firefox? Why does this stuff have to be such a humongous god-forsaken
6
3521
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" and "ns0:Company" but I do not want to delete their child nodes("w:p","w:r","w:t"). How can I do this? <ns0:Proposal> <ns0:Company> <w:p> <w:r>
0
2902
by: Walt Borders | last post by:
Hi, My problem: Merging two datasets deletes parent elements, preserves all children. I've created two dataSets. Each use the same schema, parent-child nested tables. The first dataSet is loaded with historical data read from an XML file. The second dataSet has current data filled from a dataGrid.
7
2614
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the list when a child exits. The problem I'm having is that very very occasionally and seemingly...
4
2923
by: patrizio.trinchini | last post by:
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:
3
2747
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/>
6
18980
by: pavel.repkin | last post by:
Hey! How would you do the following task? Let you have an XML tree on input. Suppose, there is a special kind of node you want to remove. Let it have "bad" name. Each "bad" node has a parent node, obviously. In case there are no children left in the parent after removal of all the "bad" nodes, the parent must also be removed. And this rule is applied to all the ancetors of the "bad" node
14
5197
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 create the child element. At the point where the script is supposed to call itself with the new properties to create the parent, it gives me the error and looks like it's looping the function continuously. It seems that "id : attrib" (line 192) is...
0
2583
by: Guzeppi | last post by:
Hi, i'm using linq to load an xml structure into my classes. the xml consists of the same node nested for multiple levels e.g. <node id="node_id01" name="node 01"> <node id="node_id0101" name="node 01 01"> <node id="node_id010101" name="node 01 01 01"> <node id="node_id01010101" name="node 01 01 01 01"> <node id="node_id0101010101" name="node 01 01 01 01 01">
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.