“有效 xml”和“有效 xml”之间有什么区别吗? 和“格式良好的 xml”?

发布于 2024-07-06 07:26:22 字数 53 浏览 7 评论 0原文

我没有意识到有什么区别,但一位同事说有区别,尽管他无法证实这一点。 如果有的话有什么区别?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

绻影浮沉 2024-07-13 07:26:22

是的,有区别。

遵循 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.

只为一人 2024-07-13 07:26:22

格式良好与有效 XML

格式良好意味着文本对象满足 W3C 要求 XML

有效表示格式正确的 XML 满足指定模式给出的附加要求。


官方定义

根据 W3C XML 建议书

[定义:如果数据对象是 XML 文档,则它是
格式良好,如本规范。 除此之外
XML 文档如果满足以下条件,则有效某些进一步的限制。]


观察:

  • 格式不正确的文档不是 XML。 (格式良好的 XML 很常用,但在技术上是多余的。)
  • 有效意味着格式良好。
  • 格式良好并不意味着有效。
  • 尽管 W3C XML 建议书定义了 有效性 是针对 DTD 的,常规使用允许应用该术语以符合通过 XSD, RELAX NG、Schematron 或其他方法。

导致文档...

格式不正确:

  • 元素缺少结束标记(并且不是自闭合)的示例。
  • 元素在没有正确嵌套的情况下重叠:
  • 属性值缺少与
    开盘报价。
  • 在内容中使用 <&,而不是 <&
  • 存在多个根元素。
  • 存在多个 XML 声明,或者 XML 声明出现在文档顶部以外的位置。

无效

  • 缺少 XML 架构所需的元素或属性。
  • 元素或属性已使用但未由 XML 模式定义。
  • 元素的内容与 XML 架构指定的内容不匹配。
  • 属性值与 XML 架构指定的类型不匹配。

格式良好的命名空间

从技术上讲,XML 中的组件名称中允许使用冒号字符。 但是,冒号应该 只能在用于命名空间目的的名称中使用:

注意:

XML 推荐中的命名空间 [XML 名称] 分配一个
含义包含冒号字符的名称。 因此,作者
除命名空间用途外,不应在 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:

[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.]


Observations:

  • A document that is not well-formed is not XML. (Well-formed XML is commonly used but technically redundant.)
  • Being valid implies being well-formed.
  • Being well-formed does not imply being valid.
  • Although the W3C Recommendation for XML defines validity to 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.

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.

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.

Namespace-Well-Formed

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.

你的他你的她 2024-07-13 07:26:22

有效的 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.

疏忽 2024-07-13 07:26:22

正如其他人所说,格式良好的 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.

与他有关 2024-07-13 07:26:22

格式良好的 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.

向日葵 2024-07-13 07:26:22

如果满足标准规定的所有 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.

偏爱你一生 2024-07-13 07:26:22

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.

鸠魁 2024-07-13 07:26:22

我要补充一点,有效的 XML 也意味着它格式良好,但格式良好的 XML 不一定有效。

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

ゞ记忆︶ㄣ 2024-07-13 07:26:22

如果 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.

您的好友蓝忘机已上羡 2024-07-13 07:26:22

摘自 可扩展标记语言 (XML) 1.0(第五版) - W3C 建议 11 月 26 日2008 年

[定义:如果数据对象格式良好,则该数据对象就是 XML 文档,如下所示
本规范中定义。 另外,XML文档是有效的
如果它满足某些进一步的限制。]


对于那些喜欢伪代码而不是文本段落的人......:)

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

基于理论:“格式良好”与有效

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

氛圍 2024-07-13 07:26:22

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.

生生漫 2024-07-13 07:26:22

嗯,根据定义,格式不正确的 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).

同展鸳鸯锦 2024-07-13 07:26:22

请参阅W3 Schools 上的 XML DTD

具有正确语法的 XML 文档称为“格式良好”。

根据 DTD 验证的 XML 文档既“格式良好”,又
“有效”。

See XML DTD on W3 Schools:

An XML document with correct syntax is called "Well Formed".

An XML document validated against a DTD is both "Well Formed" and
"Valid".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文