XSD 学习

1、新建文件 Rhythmk.xsd

   

<?xml version="1.0" encoding="utf-8"?>

<xs:schema id="Rhythmk"
    targetNamespace="http://tempuri.org/Rhythmk.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/Rhythmk.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vs="http://schemas.microsoft.com/Visual-Studio-Intellisense" 
>
  <xs:element name="sqlMap">
    <xs:complexType>
      <!-- xs:all 控制子元素单一出现 -->
      <xs:all>
        <xs:element ref="rhythmk" minOccurs="0"/>
        <xs:element ref="books" minOccurs="0" maxOccurs="1"  />
      </xs:all>
      <xs:attribute name="namespace" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>

  <!--
  1、elementFormDefault="qualified":
      指出任何 XML 实例文档所使用的且在此 schema 中声明过的元素必须被命名空间限定
      
  -->
  <xs:element name="rhythmk">
    <xs:complexType>
      <xs:sequence>
        <!-- 指定类型为整行-->
        <xs:element name="id" type="xs:integer">
        </xs:element>
        <xs:element name="realname" type="xs:string"></xs:element>
        <xs:element name="body" type="xs:string"></xs:element>
        <xs:element name="birthday" type="xs:date"></xs:element>
        <!-- 为性别设置默认值 "男"-->
        <xs:element name="sex" type="xs:string" default="男"></xs:element>
        <!--设置默认值,且无法修改-->
        <xs:element name="url" type="xs:string"  fixed="http://rhythmk.cnblogs.com"></xs:element>
        <!-- 定义属性  -->
        <xs:element name="address"  >
          <xs:complexType >
            <!--必填属性-->
            <xs:attribute name="area" use="required" >
              <xs:simpleType>
                <xs:restriction  base="xs:NMTOKEN"  >
                  <xs:enumeration value="长沙"></xs:enumeration>
                  <xs:enumeration value="衡阳"></xs:enumeration>
                  <xs:enumeration value="岳阳"></xs:enumeration>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
  
  <!--包住子元素让其可以多次出现-->
  <xs:element name="books">
    <xs:complexType>
      <!--   允许子节点多次出现  -->
      <xs:choice maxOccurs="unbounded">
        <xs:element ref="book"></xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
  
  
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <!-- 指定类型为整行-->
        <xs:element name="id" type="xs:integer">
        </xs:element>
        <xs:element name="bookname" type="xs:string"></xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

2、根据定义 书写XML    

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace=""  xmlns="http://tempuri.org/Rhythmk.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <rhythmk>
    <id> 1</id>
    <realname>wk </realname>
    <body> body</body>
    <birthday>1990-01-25</birthday>
    <sex></sex>
    <url>http://rhythmk.cnblogs.com</url>
    <address area="长沙"></address>
  </rhythmk>
  <books>
    <book>
      <id>1</id>
      <bookname>book1</bookname>
    </book>
    <book>
      <id>2</id>
      <bookname>book1</bookname>
    </book>
    <book>
      <id>3</id>
      <bookname>book1</bookname>
    </book>
  </books>

</sqlMap>

下载地址:http://pan.baidu.com/share/link?shareid=3470703694&uk=3826187342

原文地址:https://www.cnblogs.com/rhythmK/p/3338822.html