I wasn't aware of a difference, but a coworker says there is, although he can't back it up. What's the difference if any?
16.9k 5 5 gold badges 57 57 silver badges 119 119 bronze badges asked Sep 25, 2008 at 16:53 10.9k 9 9 gold badges 29 29 silver badges 21 21 bronze badgesThere 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.
3,386 3 3 gold badges 35 35 silver badges 46 46 bronze badges answered Sep 25, 2008 at 16:53 32.7k 22 22 gold badges 98 98 silver badges 124 124 bronze badges Or an XML Schema, or RelaxNG, or Schematron, for that matter. Commented Sep 25, 2008 at 17:09 Probably worth pointing out that well-formedness is a prerequisite for validity. Commented Jun 7, 2010 at 11:20@Quentin: that's an important point, and one that recognized XML experts agree on (lists.w3.org/Archives/Public/www-xml-linking-comments/… "The spec explicitly says . "); but it's not entirely obvious from the XML spec. Do you have a citation for it? Are you basing it on w3.org/TR/REC-xml/#dt-valid ?
Commented May 2, 2013 at 13:24@LarsH By definition, if an XML document isn't well-formed it can't be checked against a DTD or schema.
– user764357 Commented Sep 24, 2014 at 0:47@LarsH, the spec reference you seek (confirming Quentin's correct assertion that well-formedness is a prerequisite for validity) is: Definition: A data object is an XML document if it is well-formed, as defined in this specification. In addition, the XML document is valid if it meets certain further constraints. See my answer below for further valid XML vs well-formed XML considerations. Thanks.
Commented Nov 11, 2014 at 17:54Well-formed means that a textual object meets the W3C requirements for being XML.
Valid means that well-formed XML meets additional requirements given by a specified schema.
[Definition: A data object is an XML document if it is well-formed, as defined in this specification. In addition, the XML document is valid if it meets certain further constraints.]
Not well-formed:
Invalid
Technically, colon characters are permitted in component names in XML. However, colons should only be used in names for namespace purposes:
Note:
The Namespaces in XML Recommendation [XML Names] assigns a meaning to names containing colon characters. Therefore, authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character.
Therefore, another term, namespace-well-formed, is defined in the Namespaces in XML 1.0 W3C Recommendation that implies all of the XML rules for well-formedness plus those governing namespaces and namespace prefixes.
Colloquially, the term well-formed is often used where namespace-well-formed would be more precise. However, this is a minor technical manner of less practical consequence than the distinction between well-formed vs valid XML described in this answer.
answered Sep 14, 2014 at 5:53 110k 30 30 gold badges 193 193 silver badges 260 260 bronze badgesThis is already a wonderful answer, but perhaps it would help to add a note about namespaces, i.e. about the property of being namespace-well-formed? As you know, namespaces are a common pitfall for beginners and many people would describe a document with namespace problems as "not well-formed".
Commented Feb 25, 2016 at 16:20 Thanks, @MathiasMüller. I've added an explanation of namespace-well-formed per your request. Commented Sep 28, 2016 at 17:47Valid 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.
answered Sep 25, 2008 at 16:54 Vinko Vrsalovic Vinko Vrsalovic 338k 55 55 gold badges 338 338 silver badges 374 374 bronze badgesI would disagree with the third paragraph. Neither term says anything about semantics (the meaning of something). DTDs have no way to indicate what a particular element or attribute means. That would be the goal of efforts like Web Ontology Language. Rather, well-formedness refers to a low level of syntax (maybe better referred to as lexical correctness), while validity refers to a higher level of syntax (call it "structural" if you like).
Commented May 1, 2013 at 20:49As 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 lexically correct (it can be parsed), while valid XML is grammatically 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.
27.9k 8 8 gold badges 97 97 silver badges 156 156 bronze badges answered Sep 25, 2008 at 17:04 42.9k 14 14 gold badges 100 100 silver badges 133 133 bronze badgesWell-Formed XML is XML that meets the syntactic requirements of the language. Not missing any closing tags, having all your singleton tags use instead of just , 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.
answered Sep 25, 2008 at 17:04 ZachPruckowski ZachPruckowski 61 1 1 bronze badgeXML 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.