473,399 Members | 2,774 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,399 software developers and data experts.

Distinguishing empty elements

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 analyze the node
object, there is one slight difference:

XmlNode sample1;
(sample1.InnerXml == null) <-- false
(sample1.InnerXml == "") <-- true

XmlNode sample2;
(sample2.InnerXml == null) <-- true
(sample2.InnerXml == "") <-- false

As it seems, .NET built-in classes use this difference, when the node is
asked for its InnerXml, as when I output the InnerXml for this samples
document element node (it contains both kinds of "empty" tags), it renders
them differently.

So, what's actually the status of this "considered the same", but actually
"well, not *exactly* the same" two syntaxes of empty nodes? I stumbled upon
this difference, when I got <script src="abc.js"></script> rendered as
<script src="abc.js"/> (using different template solution than ASPX), and
MSIE chuckled on that one.

Rgds,

Pavils
Nov 12 '05 #1
6 6038


Pavils Jurjans wrote:

So, what's actually the status of this "considered the same", but actually
"well, not *exactly* the same" two syntaxes of empty nodes? I stumbled upon
this difference, when I got <script src="abc.js"></script> rendered as
<script src="abc.js"/> (using different template solution than ASPX), and
MSIE chuckled on that one.


Well MSIE expects HTML pages (content type text/html) and parses them
with a HTML parser, and in HTML the <script> element is supposed to have
a closing tag (end tag):
http://www.w3.org/TR/html4/interact/...ml#edef-SCRIPT
so you shouldn't take that as a criteria to judge the difference between
<script></script> and <script/> in XML.
I will have a look at the .NET DOM differences you have cited.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2


Pavils Jurjans wrote:

So, what's actually the status of this "considered the same", but actually
"well, not *exactly* the same" two syntaxes of empty nodes? I stumbled upon
this difference, when I got <script src="abc.js"></script> rendered as
<script src="abc.js"/> (using different template solution than ASPX), and
MSIE chuckled on that one.


Well MSIE expects HTML pages (content type text/html) and parses them
with a HTML parser, and in HTML the <script> element is supposed to have
a closing tag (end tag):
http://www.w3.org/TR/html4/interact/...ml#edef-SCRIPT
so you shouldn't take that as a criteria to judge the difference between
<script></script> and <script/> in XML.
I will have a look at the .NET DOM differences you have cited.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3


Pavils Jurjans wrote:

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 analyze the node
object, there is one slight difference:

XmlNode sample1;
(sample1.InnerXml == null) <-- false
(sample1.InnerXml == "") <-- true

XmlNode sample2;
(sample2.InnerXml == null) <-- true
(sample2.InnerXml == "") <-- false


I dont' get that difference here with .NET 1.1, example XML document being

<?xml version="1.0" encoding="UTF-8"?>
<root>
<empty1/>
<empty2></empty2>
</root>

test program in C# being

using System;
using System.Xml;

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}: element.InnerXml == null: {1},"
+ " element.InnerXml == \"\": {2}.",
element.Name,
element.InnerXml == null,
element.InnerXml == ""
);
}
}
}

I get the output

Element name: empty1: element.InnerXml == null: False, element.InnerXml
== "": True.
Element name: empty2: element.InnerXml == null: False, element.InnerXml
== "": True.

so here with that test InnerXml is the empty string for both element nodes.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #4


Pavils Jurjans wrote:

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 analyze the node
object, there is one slight difference:

XmlNode sample1;
(sample1.InnerXml == null) <-- false
(sample1.InnerXml == "") <-- true

XmlNode sample2;
(sample2.InnerXml == null) <-- true
(sample2.InnerXml == "") <-- false


I dont' get that difference here with .NET 1.1, example XML document being

<?xml version="1.0" encoding="UTF-8"?>
<root>
<empty1/>
<empty2></empty2>
</root>

test program in C# being

using System;
using System.Xml;

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}: element.InnerXml == null: {1},"
+ " element.InnerXml == \"\": {2}.",
element.Name,
element.InnerXml == null,
element.InnerXml == ""
);
}
}
}

I get the output

Element name: empty1: element.InnerXml == null: False, element.InnerXml
== "": True.
Element name: empty2: element.InnerXml == null: False, element.InnerXml
== "": True.

so here with that test InnerXml is the empty string for both element nodes.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #5
Yeah, your'e right... there was some glitch in my code... InnerXml is never
null..

However, there is a way how to distinguish the two "empty" elements:

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
Console.Read();
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}; Has no child nodes: {1}; Has closing tag:
{2}",
element.Name,
!element.HasChildNodes,
!element.IsEmpty
);
}
}
}

I get this output:

Element name: empty1; Has no child nodes: True; Has closing tag: False
Element name: empty2; Has no child nodes: True; Has closing tag: True
So, there *is* a way to check for truly empty elements. I find it good,
because it gives a way to communicate "null" value for string variables
whose "zero-length string" value is meaningful, and should be treated
differently than "no value".

Rgds,

Pavils

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:OV*************@tk2msftngp13.phx.gbl...


Pavils Jurjans wrote:

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 analyze the node
object, there is one slight difference:

XmlNode sample1;
(sample1.InnerXml == null) <-- false
(sample1.InnerXml == "") <-- true

XmlNode sample2;
(sample2.InnerXml == null) <-- true
(sample2.InnerXml == "") <-- false
I dont' get that difference here with .NET 1.1, example XML document being

<?xml version="1.0" encoding="UTF-8"?>
<root>
<empty1/>
<empty2></empty2>
</root>

test program in C# being

using System;
using System.Xml;

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}: element.InnerXml == null: {1},"
+ " element.InnerXml == \"\": {2}.",
element.Name,
element.InnerXml == null,
element.InnerXml == ""
);
}
}
}

I get the output

Element name: empty1: element.InnerXml == null: False, element.InnerXml
== "": True.
Element name: empty2: element.InnerXml == null: False, element.InnerXml
== "": True.

so here with that test InnerXml is the empty string for both element

nodes. --

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #6
Yeah, your'e right... there was some glitch in my code... InnerXml is never
null..

However, there is a way how to distinguish the two "empty" elements:

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
Console.Read();
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}; Has no child nodes: {1}; Has closing tag:
{2}",
element.Name,
!element.HasChildNodes,
!element.IsEmpty
);
}
}
}

I get this output:

Element name: empty1; Has no child nodes: True; Has closing tag: False
Element name: empty2; Has no child nodes: True; Has closing tag: True
So, there *is* a way to check for truly empty elements. I find it good,
because it gives a way to communicate "null" value for string variables
whose "zero-length string" value is meaningful, and should be treated
differently than "no value".

Rgds,

Pavils

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:OV*************@tk2msftngp13.phx.gbl...


Pavils Jurjans wrote:

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 analyze the node
object, there is one slight difference:

XmlNode sample1;
(sample1.InnerXml == null) <-- false
(sample1.InnerXml == "") <-- true

XmlNode sample2;
(sample2.InnerXml == null) <-- true
(sample2.InnerXml == "") <-- false
I dont' get that difference here with .NET 1.1, example XML document being

<?xml version="1.0" encoding="UTF-8"?>
<root>
<empty1/>
<empty2></empty2>
</root>

test program in C# being

using System;
using System.Xml;

public class Test20040610 {
public static void Main (string[] args) {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test20040610.xml");
checkNode(xmlDocument.SelectSingleNode("/root/empty1"));
checkNode(xmlDocument.SelectSingleNode("/root/empty2"));
}

public static void checkNode (XmlNode node) {
XmlElement element = node as XmlElement;
if (element != null) {
Console.WriteLine(
"Element name: {0}: element.InnerXml == null: {1},"
+ " element.InnerXml == \"\": {2}.",
element.Name,
element.InnerXml == null,
element.InnerXml == ""
);
}
}
}

I get the output

Element name: empty1: element.InnerXml == null: False, element.InnerXml
== "": True.
Element name: empty2: element.InnerXml == null: False, element.InnerXml
== "": True.

so here with that test InnerXml is the empty string for both element

nodes. --

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #7

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

Similar topics

0
by: Hugh Sparks | last post by:
If I configure and use two different fragment extractors on the same XML document, how can I write xslt template match patterns that distinguish which elements these fragments replaced? Details:...
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)...
23
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to...
18
by: Neal | last post by:
According to the specs (http://www.w3.org/TR/html401/struct/links.html#h-12.2), the <a> element requires an end tag. And so, when we use <A NAME="foo"> in HTML 2.0 to 4.01, it won't validate,...
2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
0
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...
3
by: Clark Spencer | last post by:
I have built a small integration app using VS .NET 2003 that extracts orderinformation from a 'webshop'. Extracting the orderinformation works fine. Appending the order elements in the...
2
by: laredotornado | last post by:
Hi, If I have a $_REQUEST variable that contains either the value "" or "0", the empty($_REQUEST) call returns the same thing -- 1. How can I distinguish between these values without causing any...
3
by: August Karlstrom | last post by:
Hi everyone, In XHTML, can any empty element <foo></foobe replaced by <foo />? Example: <table> <tr><td>1</td><td>2</td></tr> <tr><td /><td>4</td></tr> </table>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
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
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...
0
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,...
0
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...

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.