XML和XSD之间的区别是什么?

时间:2022-07-10 22:26:43

What is the difference between Extensible Markup Language (XML) and XML Schema (XSD)?

可扩展标记语言(XML)和XML Schema (XSD)之间的区别是什么?

6 个解决方案

#1


122  

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文档的结构。对于任何xml, xsd都不是强制的,但是它保证了xml可以用于某些特定的目的。xml只包含适合格式和结构的数据。

#2


97  


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来说,不可能的是:首先要将其写入列表,因为列表非常小,您不能使用另一个节点/属性的值来验证一个节点/属性。2)这是一个限制:在XSD文件中定义的元素必须只定义一个数据类型。[在上面的例子中, 在另一个 节点中出现,数据类型不能被定义为int. 3)您不能忽略元素和属性的验证(如果一个元素/属性出现在XML中,它必须在相应的XSD中定义良好)。虽然使用 都允许,但它有自己的规则。持久,导致验证错误。我曾经尝试过类似的方法,当然也没有成功,这是问答环节。 任何>


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节点的适当层次结构。在上面的例子中,child_two不能是根的直接子,但是它是“父”标记的子元素,它是“root”节点的子节点,有一个层次结构。]2)您可以定义节点值的数据类型。[在上面的例子中,child_two不能有任何其他的数据]3)您也可以定义自定义的data_types,[例如,对于node ,可能的数据可能是12个月中的一个。因此,您需要在一个新的数据类型中定义所有的12个月,将所有12个月的名称作为枚举值写入。如果输入XML包含任何其他值,则验证显示错误。]4)您可以对元素的出现进行限制,使用minOccurs和maxOccurs,默认值为1和1。

.. and many more ...

. .和更多的……

#3


9  

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模式定义)指定了如何正式地描述可扩展标记语言(XML)文档中的元素。Xml: Xml是用来描述数据的。它独立于软件和硬件。它增强了以下的东西。拼共享。平*立的。-增加数据的可用性。

Differences:

差异:

  1. XSD is based and written on XML.

    XSD是基于XML编写的。

  2. XSD defines elements and structures that can appear in the document, while XML does not.

    XSD定义了文档中可能出现的元素和结构,而XML则没有。

  3. XSD ensures that the data is properly interpreted, while XML does not.

    XSD确保正确地解释数据,而XML则不能。

  4. An XSD document is validated as XML, but the opposite may not always be true.

    XSD文档被验证为XML,但是相反的可能并不总是正确的。

  5. XSD is better at catching errors than XML.

    XSD比XML更能捕获错误。

An XSD defines elements that 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所指示的格式。

#4


5  

XML versus XSD

XML defines the syntax of elements and attributes for structuring data in a well-formed document.

XML定义了在格式良好的文档中结构化数据的元素和属性的语法。

XSD (aka XML Schema), like DTD before, powers the eXtensibility in XML by enabling the user to define the vocabulary and grammar of the elements and attributes in a valid XML document.

XSD(又名XML Schema)和以前的DTD一样,通过允许用户在有效的XML文档中定义元素和属性的词汇和语法,从而增强XML的可扩展性。

#5


0  

Basically an XSD file defines how the XML file is going to look like. It's a Schema file which 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文件的外观。它是一个模式文件,它定义了XML文件的结构。它指定了可能的字段以及它们的大小。

An XML file is an instance of XSD as it uses the rules defined in the XSD.

XML文件是XSD的一个实例,因为它使用XSD中定义的规则。

#6


0  

SIMPLE XML EXAMPLE:

<school>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</school>

XSD OF ABOVE XML:

<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:

在这里:

complex type: denotes it contains other elements. simple type: denotes they do not contain other elements.

复杂类型:表示它包含其他元素。简单类型:表示它们不包含其他元素。

type: string, decimal, integer, boolean, date, time,

类型: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简单。

#1


122  

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文档的结构。对于任何xml, xsd都不是强制的,但是它保证了xml可以用于某些特定的目的。xml只包含适合格式和结构的数据。

#2


97  


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来说,不可能的是:首先要将其写入列表,因为列表非常小,您不能使用另一个节点/属性的值来验证一个节点/属性。2)这是一个限制:在XSD文件中定义的元素必须只定义一个数据类型。[在上面的例子中, 在另一个 节点中出现,数据类型不能被定义为int. 3)您不能忽略元素和属性的验证(如果一个元素/属性出现在XML中,它必须在相应的XSD中定义良好)。虽然使用 都允许,但它有自己的规则。持久,导致验证错误。我曾经尝试过类似的方法,当然也没有成功,这是问答环节。 任何>


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节点的适当层次结构。在上面的例子中,child_two不能是根的直接子,但是它是“父”标记的子元素,它是“root”节点的子节点,有一个层次结构。]2)您可以定义节点值的数据类型。[在上面的例子中,child_two不能有任何其他的数据]3)您也可以定义自定义的data_types,[例如,对于node ,可能的数据可能是12个月中的一个。因此,您需要在一个新的数据类型中定义所有的12个月,将所有12个月的名称作为枚举值写入。如果输入XML包含任何其他值,则验证显示错误。]4)您可以对元素的出现进行限制,使用minOccurs和maxOccurs,默认值为1和1。

.. and many more ...

. .和更多的……

#3


9  

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模式定义)指定了如何正式地描述可扩展标记语言(XML)文档中的元素。Xml: Xml是用来描述数据的。它独立于软件和硬件。它增强了以下的东西。拼共享。平*立的。-增加数据的可用性。

Differences:

差异:

  1. XSD is based and written on XML.

    XSD是基于XML编写的。

  2. XSD defines elements and structures that can appear in the document, while XML does not.

    XSD定义了文档中可能出现的元素和结构,而XML则没有。

  3. XSD ensures that the data is properly interpreted, while XML does not.

    XSD确保正确地解释数据,而XML则不能。

  4. An XSD document is validated as XML, but the opposite may not always be true.

    XSD文档被验证为XML,但是相反的可能并不总是正确的。

  5. XSD is better at catching errors than XML.

    XSD比XML更能捕获错误。

An XSD defines elements that 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所指示的格式。

#4


5  

XML versus XSD

XML defines the syntax of elements and attributes for structuring data in a well-formed document.

XML定义了在格式良好的文档中结构化数据的元素和属性的语法。

XSD (aka XML Schema), like DTD before, powers the eXtensibility in XML by enabling the user to define the vocabulary and grammar of the elements and attributes in a valid XML document.

XSD(又名XML Schema)和以前的DTD一样,通过允许用户在有效的XML文档中定义元素和属性的词汇和语法,从而增强XML的可扩展性。

#5


0  

Basically an XSD file defines how the XML file is going to look like. It's a Schema file which 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文件的外观。它是一个模式文件,它定义了XML文件的结构。它指定了可能的字段以及它们的大小。

An XML file is an instance of XSD as it uses the rules defined in the XSD.

XML文件是XSD的一个实例,因为它使用XSD中定义的规则。

#6


0  

SIMPLE XML EXAMPLE:

<school>
  <firstname>John</firstname>
  <lastname>Smith</lastname>
</school>

XSD OF ABOVE XML:

<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:

在这里:

complex type: denotes it contains other elements. simple type: denotes they do not contain other elements.

复杂类型:表示它包含其他元素。简单类型:表示它们不包含其他元素。

type: string, decimal, integer, boolean, date, time,

类型: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简单。