“有效 xml”和“有效 xml”之间有什么区别吗? 和“格式良好的 xml”?
我没有意识到有什么区别,但一位同事说有区别,尽管他无法证实这一点。 如果有的话有什么区别?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
是的,有区别。
遵循 XML 标准 的 XML 被认为是格式良好的,而遵循 DTD 被视为有效。
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.
格式良好与有效 XML
格式良好意味着文本对象满足 W3C 要求 XML。
有效表示格式正确的 XML 满足指定模式给出的附加要求。
官方定义
根据 W3C XML 建议书的
观察:
导致文档...
格式不正确:
开盘报价。
<
或&
,而不是<
或&
。无效
格式良好的命名空间
从技术上讲,XML 中的组件名称中允许使用冒号字符。 但是,冒号应该 只能在用于命名空间目的的名称中使用:
因此,另一个术语,namespace-well-formed ,在 XML 1.0 W3C 建议中的命名空间 这意味着所有格式良好的 XML 规则以及管理命名空间和命名空间前缀的规则。
通俗地说,术语格式良好经常用在命名空间格式良好更精确的地方。 然而,与本答案中描述的格式良好与有效的 XML 之间的区别相比,这是一种次要的技术方式,其实际效果较小。
Well-formed vs Valid XML
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.
Official Definitions
Per the W3C Recommendation for XML:
Observations:
Examples of what causes a document to be...
Not well-formed:
<a><b></a></b>
opening quote.
<
or&
are used in content rather than<
or&
.Invalid
Namespace-Well-Formed
Technically, colon characters are permitted in component names in XML. However, colons should only be used in names for namespace purposes:
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.
有效的 XML 是根据 DTD 成功验证的 XML。
格式良好的 XML 是指所有标记都以正确的顺序闭合的 XML,并且如果它有声明,则它会在文件中首先具有正确的属性。
换句话说,有效性指的是语义,格式良好指的是语法。
因此,您可能会拥有无效的格式正确的 XML。
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.
正如其他人所说,格式良好的 XML 符合 XML 规范,有效的 XML 符合给定的模式。
另一种说法是,格式良好的 XML 在词汇上是正确的(可以被解析),而有效的 XML 在语法上是正确的(可以与已知词汇表匹配)和语法)。
XML 文档在格式正确之前才有效。 所有 XML 文档都遵循相同的格式良好标准(W3 提出的 RFC)。 一个 XML 文档可能对某些模式有效,而对其他模式无效。 有多种模式语言,其中许多本身是基于 XML 的。
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.
格式良好的 XML 是满足语言语法要求的 XML。 不丢失任何结束标记,让所有单例标记使用
而不仅仅是
,并且让结束标记按正确的顺序排列。有效的 XML 是使用 DTD 并符合其所有要求的 XML。 因此,如果属性使用不当,就会违反 DTD 并且无效。
所有有效的 XML 都是格式良好的,但并非所有格式良好的 XML 都是有效的。
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.
如果满足标准规定的所有 XML 文档的要求,XML 就是格式良好的 - 因此,诸如具有单个根节点、正确嵌套的节点、所有节点具有结束标记(或在前面使用斜杠的空节点简写)右尖括号)、引用的属性等。格式良好仅意味着它遵循 XML 规则,因此可以正确解析。
如果 XML 将根据 DTD 或模式进行验证,则该 XML 是有效的。 这显然因情况而异 - 对一种模式有效的 XML 对另一种模式无效,即使它仍然是格式良好的。
如果 XML 格式不正确,则无法正确解析 - 解析器将简单地抛出异常或报告错误。 这是通用的,您的 XML 包含什么内容并不重要。 只有解析后才能检查其有效性。 此域或上下文相关,并且需要 DTD 或模式进行验证。 对于简单的 XML 文档,您可能没有 DTD 或架构,在这种情况下您无法知道 XML 是否有效 - 概念或有效性根本不适用于这种情况。 当然,这并不意味着你不能使用它,只是意味着你无法判断它是否有效。
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.
W3C 在 XML 规范中定义了创建 XML 文档时需要遵循的某些规则。 此类规则的示例包括仅具有一个根元素、每个开始标记具有结束标记、对属性值使用单/双引号等等。 如果 XML 文档遵循所有这些规则,则称其为格式良好的文档,并且可以使用 XML 解析器来解析和处理此类文档。
文档类型定义 (DTD) 或 XML 模式可用于定义特定类 XML 文档的结构和内容。 这包括父子关系详细信息、属性列表、数据类型信息、值限制等。除了格式良好的规则之外,如果 XML 文档还遵循关联的 DTD/Schema 中指定的规则,则可以说是一个有效的 XML 文档。
所有有效的 XML 文档都是格式良好的,但反之则不然。 格式正确的 XML 文档不一定是有效的。
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.
我要补充一点,有效的 XML 也意味着它格式良好,但格式良好的 XML 不一定有效。
I'll add that valid XML also implies that it's well-formed, but well-formed XML is not necessarily valid.
如果 XML 符合 DTD 规则,那么它就是有效的 XML。
如果 XML 文档符合 XML 规则(所有标签
开始是关闭的,有一个根元素等)然后它是一个
格式良好的 XML。
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) 1.0(第五版) - W3C 建议 11 月 26 日2008 年:
对于那些喜欢伪代码而不是文本段落的人......:)
基于理论:“格式良好”与有效
Taken from Extensible Markup Language (XML) 1.0 (Fifth Edition) - W3C Recommendation 26 November 2008 :
For those who prefer psuedo-code to paragraphs upon paragraphs of text... :)
Based on the theory: "Well Formed" vs. Valid
DTD 是文档类型定义的缩写。 这是 XML 文件系列内容的描述。 这是 XML 1.0 规范的一部分,允许人们描述和验证给定的文档实例是否符合详细说明其结构和内容的一组规则。
验证是根据 DTD(更一般地说是根据一组构造规则)检查文档的过程。
验证过程和构建 DTD 是 XML 生命周期中最困难的两个部分。 简而言之,DTD 定义了文档中所有可能的元素、文档树的正式形状(通过定义元素允许的内容;文本、允许的子项列表的正则表达式或混合内容)即文本和子项)。 DTD 还定义了所有元素的有效属性以及这些属性的类型。
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.
嗯,根据定义,格式不正确的 XML 就不是 XML。 人们通常将有效的 XML 称为遵循某种模式(XSD 或 DTD)的 XML。
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 certain schema (XSD or DTD).
请参阅W3 Schools 上的 XML DTD:
See XML DTD on W3 Schools: