XML 和 XSD 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2333998/
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
What is the difference between XML and XSD?
提问by Gandalf StormCrow
What is the difference between Extensible Markup Language (XML) and XML Schema (XSD)?
可扩展标记语言 (XML) 和 XML 架构 (XSD) 之间有什么区别?
回答by anthares
Actually the XSD is XML itself. Its purpose is to validate the structure of another XML document. The XSD is not mandatory for any XML, but it assures that the XML could be used for some particular purposes. The XML is only containing data in suitable format and structure.
实际上,XSD 就是 XML 本身。其目的是验证另一个 XML 文档的结构。XSD 对于任何 XML 都不是强制性的,但它确保 XML 可以用于某些特定目的。XML 仅包含合适格式和结构的数据。
回答by InfantPro'Aravind'
Take an example
举个例子
<root>
<parent>
<child_one>Y</child_one>
<child_two>12</child_two>
</parent>
</root>
and design an xsd for that:
并为此设计一个 xsd:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="parent">
<xs:complexType>
<xs:sequence>
<xs:element name="child_one" type="xs:string" />
<xs:element name="child_two" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
What isn't possible with XSD:would like to write it first as the list is very small
1) You can't validate a node/attribute using the value of another node/attribute.
2) This is a restriction :An element defined in XSD file must be defined with only one datatype. [in the above example, for <child_two>appearing in another <parent>node, datatype cannot be defined other than int.
3) You can't ignore the validation of elements and attributes, ie, if an element/attribute appears in XML, it must be well-defined in the corresponding XSD. Though usage of <xsd:any>allows it, but it has got its own rules. Abiding which leads to the validation error. I had tried for a similar approach, and certainly wasn't successful, here is the Q&A
XSD 无法实现的功能:因为列表非常小,所以想先编写它
1) 您不能使用另一个节点/属性的值来验证一个节点/属性。
2)这是一个限制:XSD 文件中定义的元素必须只定义一种数据类型。[在上面的例子中,对于<child_two>出现在另一个<parent>节点中,不能定义除int以外的数据类型。
3) 不能忽略元素和属性的验证,即如果一个元素/属性出现在XML 中,它必须在相应的XSD 中定义好。虽然使用 <xsd:any>允许它,但它有自己的规则。遵守导致验证错误。我曾尝试过类似的方法,但肯定没有成功,这是问答
what are possible with XSD:
1) You can test the proper hierarchy of the XML nodes. [xsd defines which child should come under which parent, etc, abiding which will be counted as error, in above example, child_two cannot be the immediate child of root, but it is the child of "parent" tag which is in-turn a child of "root" node, there is a hierarchy..]
2) You can define Data type of the values of the nodes. [in above example child_two cannot have any-other data than number]
3) You can also define custom data_types, [example, for node <month>, the possible data can be one of the 12 months.. so you need to define all the 12 months in a new data type writing all the 12 month names as enumeration values .. validation shows error if the input XML contains any-other value than these 12 values .. ]
4) You can put the restriction on the occurrence of the elements, using minOccurs and maxOccurs, the default values are 1 and 1.
XSD 有什么可能:
1) 您可以测试 XML 节点的正确层次结构。[xsd 定义了哪个孩子应该属于哪个父母等等,坚持哪个会被算作错误,在上面的例子中,child_two 不能是 root 的直接孩子,但它是“parent”标签的孩子,反过来“根”节点的子节点,有层次结构..]
2)您可以定义节点值的数据类型。[在上面的例子中 child_two 不能有除数字以外的任何其他数据]
3) 您还可以定义自定义数据类型,[例如,对于节点<month>,可能的数据可以是 12 个月之一..所以您需要定义所有 12 个月在新的数据类型中将所有 12 个月的名称写为枚举值..
4)可以对元素的出现次数进行限制,使用minOccurs和maxOccurs,默认值为1和1。
.. and many more ...
.. 还有很多 ...
回答by Skull
XSD:
XSD (XML Schema Definition) specifies how to formally describe the elements in an Extensible Markup Language (XML) document.
Xml:
XML was designed to describe data.It is independent from software as well as hardware.
It enhances the following things.
-Data sharing.
-Platform independent.
-Increasing the availability of Data.
XSD:
XSD(XML Schema Definition)指定如何正式描述可扩展标记语言 (XML) 文档中的元素。
XML:
XML 被设计用来描述数据。它独立于软件和硬件。
它增强了以下方面。
- 数据共享。
- 平台独立。
- 增加数据的可用性。
Differences:
区别:
XSD is based and written on XML.
XSD defines elements and structures that can appear in the document, while XML does not.
XSD ensures that the data is properly interpreted, while XML does not.
An XSD document is validated as XML, but the opposite may not always be true.
XSD is better at catching errors than XML.
XSD 是基于 XML 编写的。
XSD 定义了可以出现在文档中的元素和结构,而 XML 则没有。
XSD 确保正确解释数据,而 XML 则不然。
XSD 文档被验证为 XML,但反过来可能并不总是正确的。
XSD 比 XML 更擅长捕捉错误。
An XSD defines elementsthat can be used in the documents, relating to the actual data with which it is to be encoded.
for eg:
A date that is expressed as 1/12/2010 can either mean January 12 or December 1st. Declaring a date data type in an XSD document, ensures that it follows the format dictated by XSD.
XSD定义了可以在文档中使用的元素,这些元素与要对其进行编码的实际数据相关。
例如:
表示为 1/12/2010 的日期可以表示 1 月 12 日或 12 月 1 日。在 XSD 文档中声明日期数据类型可确保它遵循 XSD 规定的格式。
回答by kjhughes
XML versus XSD
XML 与 XSD
XMLdefines the syntaxof elements and attributesfor structuring data in a well-formeddocument.
XML定义了用于在格式良好的文档中构建数据的元素和属性的语法。
XSD (aka XML Schema), like DTD before, powers the eXtensibility in XMLby enabling the user to define the vocabulary and grammarof the elements and attributesin a validXMLdocument.
XSD (又名XML模式),如之前DTD,权力电子X在tensibilityXML通过使用户定义的词汇和语法的的元素和属性一个在有效的XML文档。
回答by shubham1js
SIMPLE XML EXAMPLE:
简单的 XML 示例:
<school>
<firstname>John</firstname>
<lastname>Smith</lastname>
</school>
XSD OF ABOVE XML(Explained):
以上 XML 的 XSD(解释):
<xs:element name="school">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Here:
这里:
xs:element: Defines an element.
xs:element:定义一个元素。
xs:sequence: Denotes child elements only appear in the order mentioned.
xs:sequence:表示子元素仅按提及的顺序出现。
xs:complexType: Denotes it contains other elements.
xs:complexType:表示它包含其他元素。
xs:simpleType: Denotes they do not contain other elements.
xs:simpleType:表示它们不包含其他元素。
type:string, decimal, integer, boolean, date, time,
类型:字符串、十进制、整数、布尔值、日期、时间、
- In simple words, xsd is another way to represent and validate XML data with the specific type.
With the help of extra attributes, we can perform multiple operations.
Performing any task on xsd is simpler than xml.
- 简而言之,xsd 是另一种表示和验证具有特定类型的 XML 数据的方法。
借助额外的属性,我们可以执行多项操作。
在 xsd 上执行任何任务都比 xml 简单。
回答by SelMez
Basically an XSD file defines how the XML file is going to look like. It's a Schema filewhich defines the structure of the XML file. So it specifies what the possible fields are and what size they are going to be.
基本上,XSD 文件定义了 XML 文件的外观。它是一个Schema 文件,它定义了 XML 文件的结构。所以它指定了可能的字段是什么以及它们将是什么大小。
An XML file is an instanceof XSD as it uses the rules defined in the XSD.
XML 文件是XSD 的一个实例,因为它使用 XSD 中定义的规则。
回答by Hatebit
XML has a much wider application than f.ex. HTML. It doesn't have an intrinsic, or default "application". So, while you might not really care that web pages are also governed by what's allowed, from the author's side, you'll probably want to precisely define what an XML document may and may not contain.
XML 比 f.ex 具有更广泛的应用。HTML。它没有固有的或默认的“应用程序”。因此,虽然您可能并不真正关心网页也受允许的内容的约束,但从作者的角度来看,您可能希望精确定义 XML 文档可以包含和不可以包含的内容。
It's like designing a database.
这就像设计一个数据库。
The thing about XML technologies is that they are textual in nature. With XSD, it means you have a data structure definition framework that can be "plugged in" to text processing tools like PHP. So not only can you manipulate the data itself, but also very easily change and document the structure, and even auto-generate front-ends.
XML 技术的问题在于它们本质上是文本的。使用 XSD,这意味着您拥有一个数据结构定义框架,可以“插入”到像 PHP 这样的文本处理工具中。因此,您不仅可以操作数据本身,还可以非常轻松地更改和记录结构,甚至自动生成前端。
Viewed like this, XSD is the "glue" or "middleware" between data (XML) and data-processing tools.
这样看来,XSD 是数据 (XML) 和数据处理工具之间的“粘合剂”或“中间件”。

