473,770 Members | 6,322 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 #1
15 3989
Anyone had an idea how to conditionally remove it?

Adding a check for no-children-exist to your RGB-attribute-exists test
ought to do it, right?


--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Apr 13 '07 #2
<Colors>
<Color RGB="255"/>
<Color RGB="211" name="Red"/>
<Color name="Yellow"/>
<Color RGB="127"/>
</Colors>
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?
Don't think about "removing", think about "not copying".

You want to copy the <Colorselemen t only if it contains something
other than a <Colorelement with an RGB attribute.

Complication: Removing the <Colorelement s from the above example does
NOT make <Colorsempty, even if you remove Yellow -- because it still
contains the whitespace text nodes between the elements.

It's possible to solve this in a single pass, but I think it's going to
be ugly. You may want to think about making it simpler by running a
second stylesheet that removes <Colorselemen ts that contain only
whitespace... or, if you're willing to rely on the nodeset extensions,
running two passes within a single stylesheet by first styling part or
all of the document into a variable and then styling from the variable
to your actual output. Look for examples of use of the exslt:node-set
extension for information about how to do this.


--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Apr 14 '07 #3
CI
On Apr 13, 4:59 pm, Joseph Kesselman <keshlam-nos...@comcast. net>
wrote:
<Colors>
<Color RGB="255"/>
<Color RGB="211" name="Red"/>
<Color name="Yellow"/>
<Color RGB="127"/>
</Colors>
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?

Don't think about "removing", think about "not copying".

You want to copy the <Colorselemen t only if it contains something
other than a <Colorelement with an RGB attribute.

Complication: Removing the <Colorelement s from the above example does
NOT make <Colorsempty, even if you remove Yellow -- because it still
contains the whitespace text nodes between the elements.

It's possible to solve this in a single pass, but I think it's going to
be ugly. You may want to think about making it simpler by running a
second stylesheet that removes <Colorselemen ts that contain only
whitespace... or, if you're willing to rely on the nodeset extensions,
running two passes within a single stylesheet by first styling part or
all of the document into a variable and then styling from the variable
to your actual output. Look for examples of use of the exslt:node-set
extension for information about how to do this.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Thanks Joe, I appreciate it.

I posted this problem to another group and Dimitre Novatchev
suggested the following stylesheet:

------------------------------------------------------------------------------------------
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="ye s" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="node()|@ *">
<xsl:copy>
<xsl:apply-templates select="node()| @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Colors[not(Color[not(@RGB)])]"/>
<xsl:template match="Color[@RGB]"/>
</xsl:stylesheet>
------------------------------------------------------------------------------

Regards,

Michael


Apr 14 '07 #4
CI wrote:
<xsl:template match="Colors[not(Color[not(@RGB)])]"/>
OK, that will certainly discard <Colorselemen ts that do not contain a
<Colorwhich does not have the RGB attribute. If the only things that
will be in <Colorsis <Colorelement s and whitespace, that should
work. If there's anything else which might appear there, you may wind up
deleting some elements that you wanted to keep.

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

"Joe Kesselman" <ke************ @comcast.netwro te in message
news:RK******** *************** *******@comcast .com...
CI wrote:
> <xsl:template match="Colors[not(Color[not(@RGB)])]"/>

OK, that will certainly discard <Colorselemen ts that do not contain a
<Colorwhich does not have the RGB attribute. If the only things that
will be in <Colorsis <Colorelement s and whitespace, that should work.
Of course, and this is exactly what the OP wanted! :o)
Cheers,
Dimitre Novatchev
Apr 14 '07 #6
On Apr 14, 3:46 am, "CI" <mdani...@yahoo .comwrote:
On Apr 13, 4:59 pm, Joseph Kesselman
<keshlam-nos...@comcast. netwrote:
[...]
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?
Don't think about "removing", think about "not
copying".
[...]
I posted this problem to another group and Dimitre
Novatchev suggested the following stylesheet:

<xsl:template match="Colors[not(Color[not(@RGB)])]"/>
<xsl:template match="Color[@RGB]"/>
This is a very clean and neat solution,--which is hardly
surprising,--but I always cringe a bit when I have to
express the same thing twice in the code. The following
looks a bit scarier, but has what counts as virtue with me:
it's re-uses the condition that both your rules are
dependent upon:

<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="Colors">
<xsl:variable name="color-elements"
select="Color[not(@RGB)]"/>
<xsl:if test="$color-elements">
<xsl:copy>
<xsl:apply-templates select="$color-elements"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

YMMV.

--
Pavel Lepin

Apr 14 '07 #7
On Apr 14, 10:22 am, p.le...@ctncorp .com wrote:
On Apr 14, 3:46 am, "CI" <mdani...@yahoo .comwrote:
On Apr 13, 4:59 pm, Joseph Kesselman
<keshlam-nos...@comcast. netwrote:
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?
Don't think about "removing", think about "not
copying".
I posted this problem to another group and Dimitre
Novatchev suggested the following stylesheet:

This is a very clean and neat solution,--which is hardly
surprising,--but I always cringe a bit when I have to
express the same thing twice in the code. The following
looks a bit scarier, but has what counts as virtue with me:
it's re-uses the condition that both your rules are
dependent upon:
On a side note, XSLT2 is bliss:

<xsl:styleshe et version="2.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="post-pass-1">
<xsl:apply-templates mode="pass-1"/>
</xsl:variable>
<xsl:apply-templates select="$post-pass-1"
mode="pass-2"/>
</xsl:template>
<xsl:template match="@*|node( )" mode="pass-1">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"
mode="pass-1"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Color[@RGB]" mode="pass-1"/>
<xsl:template match="@*|node( )" mode="pass-2">
<xsl:copy>
<xsl:apply-templates select="@*|node ()"
mode="pass-2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Colors[not(*)]" mode="pass-2"/>
</xsl:stylesheet>

--
Pavel Lepin

Apr 14 '07 #8
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.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 14 '07 #9
Dimitre Novatchev wrote:
Of course, and this is exactly what the OP wanted! :o)
Granted. But I tend to practice defensive programming, so I look for the
maximally robust solution...
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Apr 14 '07 #10

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

Similar topics

3
11777
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
2613
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
5194
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
9591
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...
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
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
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
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...
1
3969
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
3
2816
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.