Is there any difference between 'valid xml' and 'well formed xml'?

前端 未结 14 2699
清酒与你
清酒与你 2020-11-21 04:43

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?

相关标签:
14条回答
  • 2020-11-21 05:49

    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.

    0 讨论(0)
  • 2020-11-21 05:49

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

    0 讨论(0)
提交回复
热议问题