views:

2107

answers:

9

I wasnt aware of a difference, but a coworker says there is, although he can't back it up. What's the difference if any?

+22  A: 

There is a difference, yes. Xml that adheres to the xml standard is considered well formed, while xml that adheres to a DTD is considered valid.

Kilhoffer
Or an XML Schema, or RelaxNG, or Schematron, for that matter.
Torsten Marek
Probably worth pointing out that well-formedness is a prerequisite for validity.
David Dorward
@David: Excellent point!
Kilhoffer
+12  A: 

Valid XML is XML that succeeds validation against a DTD.

Well formed XML is XML that has all tags closed in the proper order and, if it has a declaration, it has it first thing in the file with the proper attributes.

In other words, validity refers to semantics, well-formedness refers to syntax.

So you can have invalid well formed XML.

Vinko Vrsalovic
A: 

Well, XML that isn't well formed, sort of by definition, isn't XML. Poeple usually refer to valid XML as XML that adheres to a certian scheama (XSD or DTD).

Charles Graham
Why so many down votes? This answer is fuzzy but correct.
bortzmeyer
+1  A: 

I'll add that valid XML also implies that it's well-formed, but well-formed XML is not necessarily valid.

Joel Coehoorn
+3  A: 

Well-Formed XML is XML that meets the syntactic requirements of the language. Not missing any closing tags, having all your singleton tags use <whatever /> instead of just <whatever>, and having your closing tags in the right order.

Valid XML is XML that uses a DTD and complies with all its requirements. So if you use an attribute improperly, you violate the DTD and aren't valid.

All valid XML is well-formed, but not all well-formed XML is valid.

ZachPruckowski
+2  A: 

XML is well-formed if meets the requirements for all XML documents set out by the standards - so things like having a single root node, having nodes correctly nested, all nodes having a closing tag (or using the empty node shorthand of a slash before the closing angle bracket), attributes being quoted etc. Being well-formed just means it adheres to the rules of XML and can therefore be parsed properly.

XML is valid if it will validate against a DTD or schema. This obviously differs from case to case - XML that is valid against one schema won't be valid against another schema, even though it is still well-formed.

If XML isn't well-formed it can't be properly parsed - parsers will simply throw an exception or report an error. This is generic and it doesn't matter what your XML contains. Only once it is parsed can it be checked for validity. This domain or context dependent and requires a DTD or schema to validate against. For simple XML documents, you may not have a DTD or schema, in which case you can't know if the XML is valid - the concept or validity simply doesn't apply in this case. Of course, this doesn't mean you can't use it, it just means you can't tell whether or not it's valid.

Simon Forrest
+6  A: 

As others have said, well-formed XML conforms to the XML spec, and valid XML conforms to a given schema.

Another way to put it is that well-formed XML is syntactically correct (it can be parsed), while valid XML is semantically correct (it can be matched to a known vocabulary and grammar).

An XML document cannot be valid until it is well-formed. All XML documents are held to the same standard for well-formedness (an RFC put out by the W3). One XML document can be valid against some schemas, and invalid against others. There are a number of schema languages, many of which are themselves XML-based.

harpo
+1  A: 

In addition to the aforementioned DTD's, there are 2 other ways of describing and validating XML documents are XMLSchema and RelaxNG, both of which may be easier to use and support more features than DTD.

Kyle Burton
A: 

If XML is confirming to DTD rules then it's a valid XML. If a XML document is conforming to XML rules (all tags started are closed,there is a root element etc)then it's a well formed XML.

Narasimhareddy