XSD笔记

XML Schema 是基于 XML 的 DTD 替代者。
XML Schema 可描述 XML 文档的结构。
XML Schema 语言也可作为 XSD(XML Schema Definition)来引用。

  1. 基本结构
    SomeWebService.Data.xsd:

     <?xml version="1.0" encoding="utf-8"?>
     <xs:schema id="SomeWebService_Data"
                targetNamespace="http://soa.aaa.com/user/SomeWebService/data/v1"
                xmlns="http://soa.aaa.com/user/SomeWebService/data/v1"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                elementFormDefault="qualified"
                xmlns:common="http://soa.aaa.com/common/types/v1">
       <xs:import namespace="http://soa.aaa.com/common/types/v1" schemaLocation="SOACommonTypes_V1.0.0.xsd"/>
     
     </xs:schema>
    

    schema是根节点
    targetNamespace是schema定义的元素的命名空间
    xmlns是默认命名空间
    简易元素:只包含文本的元素,包括:boolean,string,int等

    常用的类型:

    xs:string
    xs:decimal
    xs:integer
    xs:boolean
    xs:date
    xs:time

  2. import使用
    注意这里的SomeWebService.xsd引入了SomeWebService.Data.xsd,但是它们的命名空间是不能相同的。
    SomeWebService.xsd:

     <?xml version="1.0" encoding="UTF-8"?>
     <xs:schema id="SomeWebService"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://soa.aaa.com/user/SomeWebService/v1"
                xmlns="http://soa.aaa.com/user/SomeWebService/v1"
                xmlns:common="http://soa.aaa.com/common/types/v1"
                xmlns:data="http://soa.aaa.com/user/SomeWebService/data/v1"
                elementFormDefault="qualified"
                attributeFormDefault="unqualified">
       <xs:import namespace="http://soa.aaa.com/common/types/v1" schemaLocation="SOACommonTypes_V1.0.0.xsd"/>
       <xs:import namespace="http://soa.aaa.com/user/SomeWebService/data/v1" schemaLocation="SomeWebService.Data.xsd"/>
     
       <xs:complexType name="SomeRequest">
         <xs:sequence>
           <xs:element name="Uid" type="xs:string" />
           <xs:element name="Password" type="xs:string" />
           <xs:element name="From" type="xs:string" />
           <xs:element name="SubmitTime" type="xs:dateTime" />
         </xs:sequence>
       </xs:complexType>
     
       <xs:complexType name="User">
         <xs:sequence>
           <xs:element name="Age" type="xs:int" />
           <xs:element name="Name" type="xs:string" />
         </xs:sequence>
       </xs:complexType>
       
     </xs:schema>
    
  3. 定义一个基于C#的枚举

     <xs:simpleType name="Mode">
       <xs:restriction base="xs:string">
         <xs:enumeration value="Xml">
           <xs:annotation>
             <xs:appinfo>
               <EnumerationValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/">0</EnumerationValue>
             </xs:appinfo>
           </xs:annotation>
         </xs:enumeration>
         <xs:enumeration value="Json">
           <xs:annotation>
             <xs:appinfo>
               <EnumerationValue xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1</EnumerationValue>
             </xs:appinfo>
           </xs:annotation>
         </xs:enumeration>
       </xs:restriction>
     </xs:simpleType>
    
  4. 添加documentation

     <xs:complexType name="User">
       <xs:sequence>
         <xs:annotation>
           <xs:documentation>
             用户
           </xs:documentation>
         </xs:annotation>
         <xs:element name="Name" type="xs:string">
           <xs:annotation>
             <xs:documentation>
               姓名
             </xs:documentation>
           </xs:annotation>
         </xs:element>
       </xs:sequence>
     </xs:complexType>
    
  5. 属性

     <xs:attribute name="lang" type="xs:string"/>
     <xs:attribute name="lang" type="xs:string" default="EN"/>默认值
     <xs:attribute name="lang" type="xs:string" fixed="EN"/>固定值  
     在缺省的情况下,属性是可选的。如需规定属性为必选,请使用 "use" 属性:  
     <xs:attribute name="lang" type="xs:string" use="required"/>
    
  6. 限定

     <xs:element name="age">
     	<xs:simpleType>
     	  <xs:restriction base="xs:integer">
     	    <xs:minInclusive value="0"/>
     	    <xs:maxInclusive value="120"/>
     	  </xs:restriction>
     	</xs:simpleType>
     </xs:element> 
    

w3school限定

  1. To Be Continued ...

参考:
http://www.w3school.com.cn/schema/index.asp

原文地址:https://www.cnblogs.com/liqipeng/p/4820317.html