Schema详解1

本文深入解析XML Schema定义,包括simpleType、complexType等元素的作用及使用方法,并通过具体实例展示如何利用这些元素定义XML数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

simpleType 详解

simpleType元素作用是定义一个简单类型决定了元素的属性值的约束和相关的信息。
restriction定义一个约束条件
test5.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   attributeFormDefault="unqualified">

  <xs:simpleType name="myType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value = "0"/>
      <xs:maxInclusive value ="100"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="hello" byte="myType">
  </xs:element>
</xs:schema>

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

list从一个特定数据类型的集合中选择定义一个简单的类型元素。test6.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   attributeFormDefault="unqualified">
 
    <xs:simpleType name="myType">
      <xs:list itemType="xs:integer"/>
    </xs:simpleType>
  <xs:element name="hello" type="myType"/>
</xs:schema>

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

union从一个特定的简单数据类型的集合中选择定义一个简单类型元素。
 test7.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="allFrameSize">
    <xs:simpleType>
      <xs:union memberTypes="roadbikeSize mountainbikeSize"/>
    </xs:simpleType>
  </xs:attribute>

<xs:simpleType name="roadbikeSize">

  <xs:restriction base="xs:positiveInteger">
     <xs:enumeration value="46"/>
     <xs:enumeration value="55"/>
     <xs:enumeration value="60">
  </xs:restriction>
</xs:simpleType>
  <xs:simpleType name ="mountainbikeSize">
        <xs:restriction base="xs:string">
           <xs:enumeration value="small"/>
           <xs:enumeration value="medium"/>
           <xs:enumeration value="large"/>
        </xs:restruction>
   </xs:simpleType>
   <xs:element name = "hello">
      <xs:complexType>
         <xs:sequence>
              <xs:element name="welcome"                    type="xs:string"/>
         </xs:sequence>
        <xs:attribute ref="allFrameSize" use="required"/>
      </xs:complexType>
   </xs:element>  
</xs:schema>

test7.xml
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test7.xsd" allFrameSize="small">
   <welcome>hello world</welcome>

</hello>

complexType元素作用是定义一个符合类型,决定元素的属性值得约束和相关信息。

 注意:simpleType和complexType的区别:
simpleType类型中的元素中不包含子元素和属性。
当需要声明一个元素的子元素和/或属性时,用complexType;
当需要基于内置的基本数据类型定义一个新数据类型时,用simpleType。

simpleContent应用于complexType,对它的内容(元素本身的内容)进行约束和扩展。
 注意simpleContent元素下不包含子元素,extension表示这个元素的类型。
simpleContent用于ComplexType元素上,用于限定该ComplexType的类型元素,表示该ComplexType没有子元素,同时该ComplexType需要有属性,否则就成为了SimpleType了。
test8.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="shoeSize">
      <xs:complexType>
         <xs:simpleContent>
            <xs:extension base="xs:decinal">
            <xs:attribute name="sizing" use="required">
              <xs:simpleType>
                  <xs:restriction base="xs:string">
                       <xs:enumeration                           value="us"></xs:enumeration>
                       <xs:enumeration
                     value="europe"></xs:enumeration>
                       <xs:enumeration                     value="uk"/xs:enumeration>
                   <xs:restriction>
              </xs:simpleType>
            </xs:attribute>   
         </xs:simpleContent>
      </xs:complexType>
  </xs:element>
</xs:schema>

test8.xml
<?xml version="1.0" encoding="UTF-8"?>
<shoeSize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test8.xsd" sizing="enrope">
  1.2
</shoeSize>

choice元素允许唯一的一个元素从一个组中被选择出来。
test9.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   attributeFormDefault="unqualified">
 
   <xs:complexType name="myType">
         <xs:choice minOccurs="1" maxOccurs="1">
              <xs:element name="hello" type="xs:string"/>
              <xs:element name="world" type="xs:string"/>
         </xs:choice>
   </xs:complexType>
   <xs:element name="helloworld" type="myType"/>
 </xs:shema>


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

sequence作用是给一组数据一个特定的序列。
test10.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault ="qualified"   attributeFormDefault="unqualified">
<xs:complexType name="myType">
         <xs:sequence minOccurs="1" maxOccurs="1">
              <xs:element name="hello" type="xs:string"/>
              <xs:element name="world" type="xs:string"/>
         </xs:sequence>
   </xs:complexType>
   <xs:element name="helloworld" type="myType"/>
 </xs:shema>

test10.xml
<?xml version="1.0" encoding="UTF-8"?>
<helloworld xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test10.xsd">
<hello>mingming</hello>
<world>hehe</world>
</helloworld>

Shema经典实例 test11.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="purchaseOrder" type="PurchaseOrderType"/>
<xs:element name="comment" type="xs:string"/>
<xs:complexType name="PurchaseOrderType">
  <xs:sequence>
    <xs:element name="shipTo" type="USAddress"/>
    <xs:element name="billTo" type="USAddress"/>
    <xs:element name="comment" type="0"/>
    <xs:element name="items" type="Items"/>
  </xs:sequence>
<xs:attribute name="orderDate" type="xs:date"/>
</xs:complexType>
<xs:complexType name="USAddress">
  <xs:sequence>
   <xs:element name="name" type="xs:string"/>
   <xs:element name="street" type="xs:string"/>
   <xs:element name="city" type="xs:string"/>
   <xs:element name="city" type="xs:string"/>
   <xs:element name="city" type="xs:string"/>
 </xs:sequence>
<xs:attribute name="country"
type="xs:NMTOKEN"
fixed="US"/>
</xs:complexType>
<xs:complexType name="Items">
  <xs:sequence>
   <xs:element name="item" minOccurs="0"
    maxOccurs="unbounded">
 <xs:complexType>
 <xs:sequence>
  <xs:element name="productName" type="xs:string"/>
  <xs:element name="quantity"/>
  <xs:simpleType>
   <xs:restriction base="xs:positiveInteger">
   <xs:maxExclusive value="100"/>
  </xs:restriction>
 </xs:simpleType>
</xs:element>
   <xs:element name="USPrice" type="xs:string"/>
   <xs:element ref="comment" minOccurs="0">
   <xs:element name="shipDate" type="xs:date" minOccurs="0"/>
   <xs:sequence>
   <xs:attribute name="partNum" type="SKU" use="required"/>
  </xs:complexType>
 </xs:element>
 </xs:sequence>
 </xs:complexType>
 <!--Stock Keeping Unit,a code for identifying products-->
  <xs:simpleType name="SKU">
  <xs:restriction base="xs:integer">
  <xs:minInclusive="2"/>
  <xs:maxInclusive="10"/>
 </xs:restriction>
 </xs:simpleType>
</xs:schema>
 
test11.xml
<?xml version="1.0" encoding="UTF-8"?>
<purchaseOrderxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test11.xsd">

<shipTo>
   <name/>
   <street/>
   <city/>
   <state/>
   <zip>2.5</zip>
</shipTo>
<billTo>
  <name/>
  <street/>
  <city/>
  <state/>
  <zip>1.2</zip>
  </billTo>
 </items>
</n1;purchaseOrder>

通过DOCTYPE可以明确指定文档的根元素,因为DOCTYPE后面跟的元素就是文档的根元素。
通过Schema没法明确目标XML文档的根元素,XmlSpy是通过推断那个元素包含了其他元素来选择包含其他元素最多的那个元素作为文档的根,但是我们可以明确指定文档的根元素而不必按照XmlSpy的生成来做。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值