Schema(模式)


DTD相当于数据,Schema相当于集合。
XML Schema 是一套预先规定的XMl元素和属性创造的。
DTD不遵循XML语法,DTD数据类型有限,DTD不可扩展,DTD不支持空间

命名。
Schema 基于XMl语法,可以处理XMl文档的处理,扩展了数据类型,可

以自定义数据类型,Schema支持继承,
Schema的文档结构。
DTD 和Schema的作用相同,都是用于XML验证。Schema更强大。
此外Schema是一个XMl,DTD不是一个XMl。
myschema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"

attributeFormDefault="unqualified">
  <xs:element name="书本" type="书本类型"/>
  <xs:complexType name ="书本类型">
    <xs:sequence>
      <xs:element name="名称" type="xs:string"/>
      <xs:element name="作者" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

myschema.xml
<?xml version="1.0" encoding="UTF-8"?>
<书本 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Users\acer\Desktop\zm\xml

\myschema.xsd">
<名称>我爱编程</名称>
<作者>明铭</作者>
</书本>

所有的Schema文档使用schema作为其根元素。
用于构造schema的元素和数据类型来自

http://www.w3.org/2001/XMLSchema命名空间。
schema元素作用包含已经定义的schema。
element元素作用声明一个元素。
<xs:element name="cat" type="xs:string">
<xs:complexType>
 <xs:sequence minOccur="0" maxOccurs="0">
   <xs:element ref="cat"/>
 </xs:sequence>
</xs:complexType>


test1.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   

attributeFormDefault="unqualified">
  <xs:element name="cat" type="xs:string"/>
  <xs:element name="dog" type="xs:string"/>

  <xs:complexType name = "myType">
    <xs:sequence minOccurs="1" maxOccurs="1">
      
      <xs:element ref="cat" type="xs:string"/>
      <xs:element ref="dog" type="xs:string"/>

    </xs:sequence>
  </xs:complexType>

  <xs:element name="pets" type="myType"/>
</xs:schema>



test1.xml
<?xml version="1.0" encoding="UTF-8"?>
<pets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test1.xsd">

<cat>hello</cat>
<dog>world</dog>
</pets>


unbounded不限制出现几次。

group元素作用是把一组元素声明组合在一起,以便他们能够一起被复

合类型使用。
test2.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   

attributeFormDefault="unqualified">
      <xs:group name="myGroup">
       <xs:sequence>
         <xs:element name="name" type="xs:string"/>
         <xs:element name="birthday" type="xs:date"/>
         <xs:element name="age" type="xs:integer"/>
       <xs:/sequence>
      </xs:group>

      <xs:element name="person">
         <xs:complexType>
             <xs:group ref="myGroup"/>
             
         </xs:complexType>
</xs:schema>

test2.xml
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test2.xsd">

<name>tom</name>
<bithday></bithday>
<age>25</age>

</person>

attribute元素作用是声明一个属性。
test3.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   

attributeFormDefault="unqualified">

  <xs:attribute name="interest" type="xs:integer"/>
  <xs:element name ="person">
      <xs:complexType>
         <xs:sequence>
           <xs:element name="hello" type="xs:string"/>
           <xs:element name="world" type="xs:string"/>
         </xs:sequence>
         <xs:attribute ref ="interest" use="required"/>
     </xs:complexType>
   </xs:element>
</xs:schema>

test3.xml
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test3.xsd" interest=""2>
<hello></hello>
<world></world>

</person>


attributeGroup元素 作用是把一组属性声明组合在一起,以便可以被

复合类型应用。
test4.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   

attributeFormDefault="unqualified">
  <xs:attributeGroup name = "myAttributeGroup">
     <xs:attribute name="hello" type="xs:string"

use="required"/>
     <xs:attribute name="world" type="xs:string"
use="optional"/>
   <xs:attributeGroup>

   <xs:element name="myElement">
     <xs:complexType>
       <xs:attributeGroup ref = "myAttributeGroup"/>
     </xs:complexType>
   <xs:element>
<xs:schema>

test4.xml
<?xml version="1.0" encoding="UTF-8"?>
<myElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-

instance"
xsi:noNamespaceSchemaLocation="test4.xsd" hello="abc"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值