“有效的 xml”和“格式良好的 xml”之间有什么区别吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/134494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any difference between 'valid xml' and 'well formed xml'?
提问by user18931
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?
我不知道有什么区别,但一位同事说有区别,尽管他无法支持。如果有的话有什么区别?
回答by Kilhoffer
回答by Vinko Vrsalovic
Valid XML is XML that succeeds validation against a DTD.
有效 XML 是通过 DTD 验证的 XML。
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.
格式良好的 XML 是所有标签都按正确顺序关闭的 XML,如果它有声明,它在文件中的第一件事就是具有正确的属性。
In other words, validity refers to semantics, well-formedness refers to syntax.
换句话说,有效性是指语义,格式良好是指语法。
So you can have invalid well formed XML.
所以你可能有无效的格式良好的 XML。
回答by kjhughes
Well-formed vs Valid XML
格式良好的与有效的 XML
Well-formedmeans that a textual object meets the W3C requirementsfor being XML.
Validmeans that well-formed XML meets additional requirements given by a specified schema.
有效意味着格式良好的 XML 满足指定模式给出的附加要求。
Official Definitions
官方定义
Per the W3C Recommendation for XML:
根据W3C 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 validif it meets certain further constraints.]
[定义:数据对象是一个 XML 文档,如果它是 格式良好的,如本规范中所定义。此外,如果 XML 文档满足某些进一步的约束,则它是有效的。]
Observations:
观察:
- A document that is not well-formed is not XML. (Well-formed XMLis commonly used but technically redundant.)
- Being valid implies being well-formed.
- Being well-formed does not imply being valid.
- Although the W3C Recommendation for XMLdefines validityto be against a DTD, conventional use allows the term to be applied for conformance to XML schemas specified via XSD, RELAX NG, Schematron, or other methods.
- 格式不正确的文档不是 XML。(格式良好的 XML是常用的,但在技术上是多余的。)
- 有效意味着格式良好。
- 格式良好并不意味着有效。
- 尽管W3C XML 建议书定义了针对 DTD 的有效性,但常规用法允许将该术语应用于符合通过XSD、RELAX NG、Schematron或其他方法指定的 XML 模式。
Examples of what causes a document to be...
导致文档...的示例
Not well-formed:
格式不正确:
- An element lacks a closing tag (and is not self-closing).
- Elements overlap without proper nesting:
<a><b></a></b> - An attribute value is missing a closing quote that matches the opening quote.
<or&are used in content rather than<or&.- Multiple root elements exist.
- Multiple XML declarations exist, or an XML declaration appears other than at the top of the document.
- 元素缺少结束标记(并且不是自闭合的)。
- 元素在没有适当嵌套的情况下重叠:
<a><b></a></b> - 属性值缺少与开头引号匹配的结尾引号。
<or&用于内容而不是<或&。- 存在多个根元素。
- 存在多个 XML 声明,或者 XML 声明出现在文档顶部以外的位置。
Invalid:
无效:
- An element or attribute is missing but required by the XML schema.
- An element or attribute is used but undefined by the XML schema.
- The content of an element does not match the content specified by the XML schema.
- The value of an attribute does not match the type specified by the XML schema.
- 元素或属性缺失但 XML 架构需要。
- XML 模式使用但未定义的元素或属性。
- 元素的内容与 XML 架构指定的内容不匹配。
- 属性值与 XML 架构指定的类型不匹配。
Namespace-Well-Formed
命名空间格式良好
Technically, colon characters are permitted in component names in XML. However, colons shouldonly be used in names for namespace purposes:
从技术上讲,在 XML 的组件名称中允许使用冒号字符。然而,冒号应该只能在命名空间用途名称中使用:
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.
笔记:
XML 建议中的命名空间 [ XML 名称] 为包含冒号字符的名称分配了含义。因此,作者不应在 XML 名称中使用冒号,除非是出于命名空间的目的,但 XML 处理器必须接受冒号作为名称字符。
Therefore, another term, namespace-well-formed, is defined in the Namespaces in XML 1.0 W3C Recommendationthat implies all of the XML rules for well-formedness plusthose governing namespaces and namespace prefixes.
因此,另一个术语,结构良好的命名空间,在定义命名空间中的XML 1.0 W3C推荐标准暗含所有的良构的XML规则加上那些有关命名空间和命名空间前缀。
Colloquially, the term well-formedis often used where namespace-well-formedwould be more precise. However, this is a minor technical manner of less practical consequence than the distinction between well-formed vs valid XMLdescribed in this answer.
通俗地讲,格式良好的术语通常用于命名空间格式良好的地方更精确。但是,这是一种次要的技术方式,与此答案中描述的格式良好的 XML 与有效 XML之间的区别相比,其实际后果更小。
回答by harpo
As others have said, well-formed XML conforms to the XML spec, and valid XML conforms to a given schema.
正如其他人所说,格式良好的 XML 符合 XML 规范,有效的 XML 符合给定的模式。
Another way to put it is that well-formed XML is lexicallycorrect (it can be parsed), while valid XML is grammaticallycorrect (it can be matched to a known vocabulary and grammar).
另一种说法是,格式良好的 XML 在词法上是正确的(它可以被解析),而有效的 XML 在语法上是正确的(它可以与已知的词汇和语法相匹配)。
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.
XML 文档只有在格式正确时才有效。所有 XML 文档都遵循相同的格式良好标准(W3 发布的 RFC)。一个 XML 文档可能对某些模式有效,而对其他模式无效。有许多模式语言,其中许多语言本身都是基于 XML 的。
回答by ZachPruckowski
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.
格式良好的 XML 是符合语言语法要求的 XML。不要遗漏任何结束标签,使用所有的单例标签<whatever />而不是仅仅使用<whatever>,并且以正确的顺序使用结束标签。
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.
有效的 XML 是使用 DTD 并符合其所有要求的 XML。因此,如果您不正确地使用属性,就会违反 DTD 并且无效。
All valid XML is well-formed, but not all well-formed XML is valid.
所有有效的 XML 都是格式良好的,但并非所有格式良好的 XML 都是有效的。
回答by Simon Forrest
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 文档的要求,那么 XML 就是格式良好的——例如具有单个根节点、正确嵌套节点、所有节点都具有结束标记(或在之前使用斜杠的空节点简写)右尖括号),引用的属性等。格式良好仅意味着它遵守 XML 规则,因此可以正确解析。
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.
如果 XML 将根据 DTD 或模式进行验证,则它是有效的。这显然因情况而异 - 对一种模式有效的 XML 对另一种模式无效,即使它仍然是格式良好的。
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.
如果 XML 格式不正确,则无法正确解析它 - 解析器只会抛出异常或报告错误。这是通用的,与您的 XML 包含什么无关。只有在解析后才能检查其有效性。此域或上下文相关,需要 DTD 或模式进行验证。对于简单的 XML 文档,您可能没有 DTD 或模式,在这种情况下您无法知道 XML 是否有效——概念或有效性在这种情况下根本不适用。当然,这并不代表你不能使用它,只是你无法判断它是否有效。
回答by harsha
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.
W3C 在 XML 规范中定义了创建 XML 文档时需要遵循的某些规则。此类规则的示例包括恰好有一个根元素、每个开始标记都有结束标记、对属性值使用单引号/双引号等。如果 XML 文档遵循所有这些规则,则称其为格式良好的文档,并且可以使用 XML 解析器来解析和处理此类文档。
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.
文档类型定义 (DTD) 或 XML 模式可用于定义特定类 XML 文档的结构和内容。这包括父子关系细节、属性列表、数据类型信息、值限制等。 除了格式良好的规则之外,如果一个 XML 文档还遵循关联的 DTD/Schema 中指定的规则,则称是有效的 XML 文档。
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.
所有有效的 XML 文档都是格式良好的,但反过来并不总是正确的。格式良好的 XML 文档不一定是有效的。
回答by Kent Pawar
Taken from Extensible Markup Language (XML) 1.0 (Fifth Edition) - W3C Recommendation 26 November 2008:
摘自可扩展标记语言 (XML) 1.0(第五版)- W3C 建议 2008 年 11 月 26 日:
[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.]
[定义:数据对象是一个 XML 文档,如果它是格式良好的,如本规范中所定义。此外,如果 XML 文档满足某些进一步的约束,则它是有效的。]
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
基于理论:“格式良好”与有效
回答by Kyle Burton
回答by Narasimhareddy
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.
如果 XML 确认符合 DTD 规则,那么它就是一个有效的 XML。如果一个 XML 文档符合 XML 规则(所有开始的标签都是关闭的,有一个根元素等),那么它就是一个格式良好的 XML。

