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

XslTransform and empty elements

Hi,

When I transform an xml-file using XslTransform, I seem to get a
lot of elements of this form:

<a> </a>
<b> </b>

But I really would like to get written out like I do when
transforming with MSXML, where I get

<a/>
<b/>

I did come up with a kludge, thanks to
http://www.mcse.ms/archive111-2004-6-755648.html, by manipulating
the XmlDocument structure before saving the file. I think it's
most easily described as code:

public static XmlDocument FixEmptyElements(XmlDocument document)
{
FixEmptyNodeRecursively(document.DocumentElement);
return document;
}

private static void FixEmptyNodeRecursively(XmlElement element)
{
if (!element.HasChildNodes && !element.IsEmpty)
element.IsEmpty = true;
foreach (XmlNode node in element.ChildNodes)
{
XmlElement el = node as XmlElement;
if (el != null)
FixEmptyNodeRecursively(el);
}
}

I just recursively go through all elements and sets IsEmpty true
on those who have no child nodes.

Is there a more elegant way to achieve this? May there be an
attribute somewhere I may set to avoid XslTransform creating
empty elements with end tags? Also, I wonder if my code may harm
other parts of my xml document, I'm kind of a newbie at this.

By the way, I know that <a/> and <a> </a> should be equal, but
I'm using 3rd party components that chokes on the former. I think
I will take it up with my vendor, but I'm not a customer yet,
just evaluating their software ;-)

- Geir
Nov 12 '05 #1
1 1658
"Geir Sørensen" <gs*@pvv.org> wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl...
When I transform an xml-file using XslTransform, I seem to get a
lot of elements of this form:

<a> </a> : : But I really would like to get written out like I do when : : <a/>


Try wrapping an XmlTextWriter around the output from the
XslTransform's Transform( ) method. This XmlTextWriter is
responsible for intercepting calls to the WriteFullEndElement( )
method, and substituting calls to WriteEndElement( ) that
produces the more compact empty element. Here's an
example:

- - - XmlTextWriterEE.cs (complete)
using System;
using System.IO;
using System.Xml;

/// <summary>
/// Wrapper that forces more compact empty element end
/// tags to be written whenever possible.
/// </summary>
public class XmlTextWriterEE : XmlTextWriter
{
public XmlTextWriterEE( TextWriter sink) : base( sink) {;}
public override void WriteFullEndElement() {
base.WriteEndElement();
}
}
- - -

Usage would work something like this,

xslDoc.Transform( xmlDoc.CreateNavigator( ),
null, new XmlTextWriterEE( Console.Out) );
Derek Harmon
Nov 12 '05 #2

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

Similar topics

2
by: John Meyer | last post by:
I have an application where I create an xml fragment using an XslTransform object. However, if I use the following output method, <xsl:output method="xml" version="1.0" encoding="UTF-8"...
3
by: Steve | last post by:
Is there any way of specifying the startMode when using the xslTransform class? We are updating code which used msxml to the system.xml classes but can find no way to specify the startMode. We...
1
by: Yuriy | last post by:
Hi, Can anybody explain the following? Say I have the following source XML and XSLT (see below). No matter what this XSLT does. It is just a sample to show a problem. the idea is that XSLT...
9
by: WT | last post by:
Hello, I have code created with .net 1.0 and migrated to 3.5. Form 2.0 the XslTransform class is obsolete and the vs2008 compiler generates warnings that these classes are absolete suggesting to...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.