tags:

views:

2048

answers:

6

Can an XML attribute be the empty string?

In other words, is "<element att="" />" valid XML?

+9  A: 

Yes, this is well-formed XML.

An easy way to test this (on Windows) is to save the sample in a test.xml file and open it with Internet Explorer. IE will display an error message if the document is not well-formed.

Bradley Grainger
I'm not sure I would rely on IE as the definitve test of a standard.
Martin Beckett
Don't worry about it: testing well-formed xml is something that's pretty well understood and hard to screw up.
Joel Coehoorn
As @hal10001 noted, there are several parsers that fail when given valid XML and I expect that there are several that work with invalid XML.
David Locke
+2  A: 

It is worth nothing that this is an XML attribute, not an element. An empty element would be:

</>

which is not valid XML.

Greg Hewgill
You're correct, I've edited the question to match what I intended.
David Locke
If we're being pedantic, the XML spec calls elements such as `<br></br>` and `<br/>` empty elements.
Michael Burr
+1  A: 

It should also be noted that some XML parsers will throw an error on empty string nodes and attributes instead of returning null or an empty string. So even though it might be valid, it would be better to leave it out altogether.

hal10001
That sounds like a broken parser, since a missing attribute is different from an attribute specifying an empty string (particularly when using a DTD that specifies default attribute values).
Greg Hewgill
I would agree completely :)
hal10001
What parser throws an error on an empty attribute?
Robert Rossney
REXML for Ruby throws Nil, which isn't as bad as returning an empty string, and if memory serves, SimpleXML returns an empty SimpleXML object on an empty node instead of either.
hal10001
+2  A: 

You can create an empty attribute via attname=""

You can create an empty element via

<elementName></elementName>

or

<elementName/>
Mitchel Sellers
A: 

Yes - element content can be empty, and as you used in your question there's even the special Empty-Element tag notation:

<elementname />

Except when interop with SGML is necessary, the above is equivalent to

<elementname></elementname>

Attribute values can also be empty, but attributes must always be followed by an '=' and a quoted string, even if the string contains no characters.


The definitive place for this information is the XML spec: http://www.xml.com/axml/testaxml.htm

Here's what the Annotated XML Reference has to say about empty elements:

So, is this: <img src='madonna.gif'></img> really exactly the same as <img src='madonna.gif'/>? As far as XML is concerned, they are. This decision was the occasion of much religious debate, with some feeling that there is an essential element between "point" and "container" type elements. And as the "for interoperability" note below makes clear, if you are using pre-1998 SGML software to process XML, there is a big difference.

note: the discussion on empty elements was due to the original wording of the posted question.

Michael Burr
+1  A: 

this is valid xml tag:

<mytag myattrib=""/>

this is not:

<mytag myattrib/>
coder