XML Schema(模式/架构)

XML模式描述了XML文档的结构。XML模式语言也称为XML模式定义(XSD)。

XML模式的目的是定义XML文档的合法构造块:

  • 可以出现在文档中的元素和属性
  • 子元素的数量(及其顺序)
  • 元素和属性的数据类型
  • 元素和属性的默认值和固定值

XML Schema是DTD的基于XML的替代方案

<xs:element name="note">

<xs:complexType>
  <xs:sequence>
    <xs:element name="to" type="xs:string"/>
    <xs:element name="from" type="xs:string"/>
    <xs:element name="heading" type="xs:string"/>
    <xs:element name="body" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

</xs:element>

上面的模式可的解释:

  • <xs:element name="note"> 定义了名称为"note"的元素
  • <xs:complexType> "note"元素是复杂类型
  • <xs:sequence> 复杂类型是一系列元素
  • <xs:element name="to" type="xs:string"> 元素 "to" 是字符串类型
  • <xs:element name="from" type="xs:string"> 元素"from" 是字符串类型
  • <xs:element name="heading" type="xs:string"> 元素 "heading"是字符串类型
  • <xs:element name="body" type="xs:string"> 元素"body" 是字符串类型

XML模式比DTD更强大

  • XML模式是用XML编写的
    1 您不必学习新语言
    2 您可以使用XML编辑器来编辑Schema文件
    3 可以使用XML解析器来解析Schema文件
    4 您可以使用XML DOM操作架构
    5 您可以使用XSLT转换模式
  • XML模式是可扩展的,因为它们是用XML编写的。使用可扩展的模式定义,可以:
    1 在其他模式中重用您的模式
    2 创建从标准类型派生的自己的数据类型
    3 在同一文档中引用多个模式
  • XML模式支持数据类型
    1 描述文档内容更容易
    2 定义数据限制更容易
    3 验证数据的正确性更容易
    4 在不同数据类型之间转换数据更容易
  • XML模式支持名称空间

XML模式安全数据通信

当从发送者向接收者发送数据时,至关重要的是,两个部分对内容必须具有相同的“期望”。 使用XML模式,发送方可以以接收方可以理解的方式描述数据。
在某些国家/地区,日期如:“ 03-11-2004”将被解释为11月3日,在其他国家/地区则被解释为3月11日。 但是,XML元素的数据类型如下: <date type =“ date”> 2004-03-11 </ date> 确保XML数据类型“日期”要求格式为“ YYYY-MM-DD”,因此可以确保相互理解内容。

XSD和DTD使用对比

XML文档可以引用DTD或XML Schema。

一个简单的XML文档:

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

以下示例是一个名为“ note.dtd”的DTD文件,该文件定义了上面的XML文档(“ note.xml”)的元素:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

以下示例是一个名为“ note.xsd”的XML模式文件,该文件定义了上述XML文档(“ note.xml”)的元素:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

该XML文档引用了DTD:

<?xml version="1.0"?>

<!DOCTYPE note SYSTEM
"https://www.w3schools.com/xml/note.dtd">

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

该XML文档引用了XML模式:

<?xml version="1.0"?>

<note
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/xml note.xsd">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>
原文地址:https://www.cnblogs.com/laop520/p/13741552.html