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?
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 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.
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.
Taken from Extensible Markup Language (XML) 1.0 (Fifth Edition) - W3C Recommendation 26 November 2008 :
[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.]
For those who prefer psuedo-code to paragraphs upon paragraphs of text... :)
IF is_well_formed(<XML_doc>) THEN
# It is well-formed, and can be parsed
IF is_valid(<XML_doc>) THEN
# Well-formed and ALSO valid. Hurray!
# **A valid XML doc, is a well-formed doc!**
ELSE
# Only well-formed, NOT valid
END IF
ELSE
# Not well-formed, or valid!
END IF
FUNCTION is_well_formed
IF <does_not_contain_syntax,_spelling,_punctuation,_grammar_errors,_etc._errors> THEN
RETURN TRUE
ELSE
RETURN FALSE
END IF
END FUNCTION
FUNCTION is_valid
IF <markup_of_the_XML_document_matches_"some"_defined_standard> THEN
# Standards used to validate XML could be a DTDs or XML Schemas, referenced within the XML document
RETURN TRUE
ELSE
RETURN FALSE
END IF
END FUNCTION
Based on the theory: "Well Formed" vs. Valid
Well-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.
Per the W3C Recommendation for XML:
[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.]
<a><b></a></b>
<
or &
are used in content rather than <
or &
.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.
W3C, in the XML specification, has defined certain rules that needs to be followed while creating XML documents. The examples of such rules include having exactly one root element, having end-tag for each start-tag, using single/double quotes for attribute values, and so on. If an XML document follows all these rules, it is said to be well-formed document and XML parsers can be used to parse and process such documents.
Document Type Definitions (DTDs) or XML Schemas can be used to define the structure and content of a specific class of XML documents. This includes the parent-child relationship details, attribute lists, data type information, value restrictions, etc. In addition to the well-formedness rules, if an XML document also follows the rules specified in the associated DTD/Schema, it is said to be a valid XML document.
All valid XML documents are well-formed, but the reverse is not always true. Well-formed XML documents do not necessarily have to be valid.
DTD is the acronym for Document Type Definition. This is a description of the content for a family of XML files. This is part of the XML 1.0 specification, and allows one to describe and verify that a given document instance conforms to the set of rules detailing its structure and content.
Validation is the process of checking a document against a DTD (more generally against a set of construction rules).
The validation process and building DTDs are the two most difficult parts of the XML life cycle. Briefly a DTD defines all the possible elements to be found within your document, what is the formal shape of your document tree (by defining the allowed content of an element; either text, a regular expression for the allowed list of children, or mixed content i.e. both text and children). The DTD also defines the valid attributes for all elements and the types of those attributes.