473,322 Members | 1,232 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,322 software developers and data experts.

get rid of empty nodes

Okay, this is probably a really stupid question - but I just can't figure it
out.

I have a large xml document which is being transformed into another using
..net xsl - some elements inside of which do not have any data at some times
but do at others. If they don't have any data, I want to suppress the
generation of the tag completely in the resulting xml.

So for example, if I have a document:

<root>
<a>alksjdfsd</a>
<b>sdkfjlsdkf</b>
<c/>
<d>askdjf</d>
</root>

and i run it throught the transform, i want:

<new_root>
<new_a>asdfasf</new_a>
<new_b>askljfs</new_b>
<new_d>asdfasdf</new_d>
</new_root>

Something simple i'm missing????

Cheers

Chris
Nov 11 '05 #1
2 1697
ch***@yahoo.com wrote:
Okay, this is probably a really stupid question - but I just can't figure it
out.

I have a large xml document which is being transformed into another using
.net xsl - some elements inside of which do not have any data at some times
but do at others. If they don't have any data, I want to suppress the
generation of the tag completely in the resulting xml.

So for example, if I have a document:

<root>
<a>alksjdfsd</a>
<b>sdkfjlsdkf</b>
<c/>
<d>askdjf</d>
</root>

and i run it throught the transform, i want:

<new_root>
<new_a>asdfasf</new_a>
<new_b>askljfs</new_b>
<new_d>asdfasdf</new_d>
</new_root>


Well, you can merely test if an element has no children nodes and
generate nothing then:

<xsl:template match="c">
<xsl:if test="node()">
<new_c>
<xsl:apply-templates/>
</new_c>
</xsl:if>
</xsl:template>

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
ch***@yahoo.com wrote:
Thanks for the reply. I figured that, but I was hoping for a global setting
as I'm very lazy!!!!

Then just don't apply templates to empty elements:
on parent element:

<!-- apply templates to all children elements, which have any children
nodes -->
<xsl:apply-templates select="*[node()]"/>

Alternatively you can provide dummy template for empty elements:
<xsl:template match="*[not(node())]" priority="10"/>
Due to high priority this template will prevail even more concrete
<xsl:template match="c">
This solution semms like what you want - just one template for empty
elememnts will supress their processing.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #3

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

Similar topics

4
by: Jyrki Keisala | last post by:
Hi, I have an XML file which I want to present as a HTML table, and I'm looking for a quick way of defaulting all empty elements in my XML file to a nbsp (using the   notation) in my XSL...
4
by: n_o_s_p_a__m | last post by:
My xml doc has many <title></title> and <title> in it, meaning the nodes have no content (although some do). How can I test for this? I tried title (doesn't work) I tried //title (doesn't work)...
6
by: Pavils Jurjans | last post by:
Hello, Here's the sample XML: <sample1></sample1> <sample2/> From many XML books and online documentations, it is said to be just different syntax for the same data. However, when we...
1
by: tMan | last post by:
whats the xpath to get all the empty nodes in a document. in the xml below i want to get the nodes <empty> and <t> all other nodes either have a child node or text in them <root> <test> <se>...
4
by: Michael Frost | last post by:
Loading and saving a xml document in .net creates whitespace in empty nodes! loading an empty node like: <node></node> ..net saves as: <node> </node>
2
by: | last post by:
When I am debugging I can see when treenode.nodes is empty because the count property of nodes will be zero, but at design time the count property is non existant. Is there anyway of checking...
3
by: tschwartz | last post by:
I'm trying to write a stylesheet which removes nodes which are empty as a result of other template processing. For example, given the following xml, I'd like to: - remove all "b" elements -...
5
by: jhurrell | last post by:
I have an XML file that contains some empty nodes like: <sample> <test/> <test1/> </sample> I am using Saxon 8.8 to transform this XML into another XML file, but I'd like to remove...
5
by: Valery | last post by:
In the following XML: <?xml version="1.0" encoding="utf-8" ?> <Plcy service="ILiability" boId ="LifePolicy, 1"> <Prem service="IPremium" boId ="RegularPremium, 1"></Prem> <L1...
11
by: David | last post by:
Hi All, I am working on a script that is theoreticaly simple but I can not get it to work completely. I am dealing with a page spit out by .NET that leaves empty tags in the markup. I need a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.