XSD实例

XSD实例

在前面的XSD笔记中,基本上是以数据类型为主线来写的,而在我的实际开发过程中,是先设计好了XML的结构(元素、属性),并写好了一份示例,然后再反过来写XSD文件(在工具生成的基础上修改),也就是说,是以XML结构为主线的。而我在学习XSD的时候,则是以能否看懂spring-beans-3.2.xsd这个文件来检测自己,我的想法很简单,这个文件已经够复杂——对我来说——如果能够看懂这个文件,那基本上已经够我用的了,倘若实际开发的时候遇到超出这个范围的,那到时候再找相关资料学习也不晚。

一、为XML结构编写相应XSD片段

1、定义一个没有属性也没有内容的元素——没有属性的空元素:

<xs:element name="emptyElement">
  <xs:complexType>
    <!--既然XML文件中所有能出现的元素和属性都必须在XSD中出现,那么什么都没定义的意思就是什么都不能出现了-->
  </xs:complexType>
</xs:element>

2、定义一个没有内容的元素,但是这个元素有两个属性,一个必须的id属性,一个可选的name属性:

<xs:element name="emptyElementWithAttribute">
  <xs:complexType>
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attribute name="name" type="xs:NMTOKEN" default="未定义"/>
  </xs:complexType>
</xs:element>

3、定义有内容但是没有属性的元素——简单元素

复制代码
<!--1.使用内建类型,内容为字符串-->
<xs:element name="simpleElement1" type="xs:string"/>
<!--2.使用约束派生,内容为长度在6至16之间的字符串-->
<xs:element name="simpleElement2">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:annotation>
        <xs:documentation><![CDATA[
         长度大于或等于6,小于或等于16的字符串
        ]]></xs:documentation>
      </xs:annotation>
      <xs:minLength value="6"/>
      <xs:maxLength value="16"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
<!--3.使用列表派生,内容为已经存在的一个或多个ID类型数据的列表-->
<xs:element name="simpleElement3">
  <xs:simpleType>
    <xs:list itemType="xs:IDREF"/>
  </xs:simpleType>
</xs:element>
<!--4.使用联合派生,内容可以是整型,也可以是NMTOKEN类型-->
<xs:element name="simpleElement4">
  <xs:simpleType>
    <xs:union memberTypes="xs:int xs:NMTOKEN"/>
  </xs:simpleType>
</xs:element>
复制代码

4、定义只包含子元素的元素,不能包含内容,但可以包含属性

复制代码
<!-- 空类型 -->
<xs:complexType name="emptyType">
</xs:complexType>
<!-- 表类别类型(简单枚举类型) -->
<xs:simpleType name="tableTypeType">
  <xs:restriction base="xs:string">
      <xs:enumeration value="系统表"/>
      <xs:enumeration value="参数表"/>
      <xs:enumeration value="数据表"/>
  </xs:restriction>
</xs:simpleType>
<!-- 名称类型(2-30位的字符串) -->
<xs:simpleType name="nameType">
  <xs:restriction base="xs:NCName">
      <xs:minLength value="2"/>
      <xs:maxLength value="30"/>
  </xs:restriction>
</xs:simpleType>
<!-- 注释类型(2-1024位的字符串) -->
<xs:simpleType name="commentType">
  <xs:restriction base="xs:string">
      <xs:minLength value="2"/>
      <xs:maxLength value="1024"/>
  </xs:restriction>
</xs:simpleType>
<!-- 字段元素(含属性不含内容元素)-->
<xs:element name="field">
  <xs:complexType>
      <xs:complexContent>
          <xs:extension base="emptyType">
              <xs:attribute name="fieldName" type="nameType" use="required"/>
              <xs:attribute name="dataType" type="xs:NCName" use="required"/>
              <xs:attribute name="length" type="xs:int" use="optional"/>
              <xs:attribute name="comment" type="commentType" use="optional"/>
          </xs:extension>
      </xs:complexContent>
  </xs:complexType>
</xs:element>
<!-- 表元素(含属性和子元素的元素) -->
<xs:element name="table">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="unbounded" ref="field" />
        </xs:sequence>
        <xs:attribute name="tableName" type="nameType" use="required"/>
        <xs:attribute name="tableType" type="tableTypeType" use="required"/>
        <xs:attribute name="comment" type="commentType" use="optional"/>
    </xs:complexType>
</xs:element>
复制代码

5、定义既包含子元素又包含内容的混合元素,同时也可以拥有属性

比如将上面例子中<table>元素修改为可以包括内容,可以在<xs:complexType>加一个mixed="true"属性:

复制代码
<!-- 表元素(含属性、内容和子元素的元素) -->
<xs:element name="table">
    <xs:complexType mixed="true">
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="unbounded" ref="field" />
        </xs:sequence>
        <xs:attribute name="tableName" type="nameType" use="required"/>
        <xs:attribute name="tableType" type="tableTypeType" use="required"/>
        <xs:attribute name="comment" type="commentType" use="optional"/>
    </xs:complexType>
</xs:element>
复制代码

二、spring-beans-3.2.xsd

  我将这个文件放在这里,每次看的时候都能够检测一下(其中以<annotation>元素描述的注释内容去掉了,并简单修改了一小部分写法):

复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.springframework.org/schema/beans">
    
    <!--导入命名空间,并保持原命名空间-->
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
    
    <!--定义抽象的id标识类型,供后文引用,而不能直接在XML中使用-->
    <xsd:complexType name="identifiedType" abstract="true">
        <xsd:attribute name="id" type="xsd:string"/>
    </xsd:complexType>

        <!--定义根元素beans,包含子元素的复杂类型元素-->
    <xsd:element name="beans">
        <xsd:complexType>
            <xsd:sequence>
                    <!--首先是描述信息,可以出现,也可以不出现-->
                <xsd:element ref="description" minOccurs="0"/>
                <!--然后是主体部分,这里使用相当于DTD中的 (e1|e2|e3)* 结构实现自由组合的无序元素 -->
                <xsd:choice minOccurs="0" maxOccurs="unbounded">
                    <xsd:element ref="import"/>
                    <xsd:element ref="alias"/>
                    <xsd:element ref="bean"/>
                    <!--除了使用当前XSD文件的元素,还以使用导入的其它XSD文件中定义的元素,不过必须先获取相应的XSD文件,并做严格的校验-->
                    <xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:choice>
                <!--从这里可以看到,原来beans这个元素还可以嵌套,大体作用相当于import吧,只是import导入的就不是本地的配置,而嵌套的应该还属于本地的配置(?)-->
                <xsd:element ref="beans" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute name="profile" use="optional" type="xsd:string"/>
            <!--对于多个地方都使用的true|false|default,将其定义为一个自定义数据类型,然后再在需要的地方引用-->
            <xsd:attribute name="default-lazy-init" default="default" type="defaultable-boolean"/>
            <xsd:attribute name="default-merge" default="default" type="defaultable-boolean"/>
            <xsd:attribute name="default-autowire" default="default">
                <xsd:simpleType>
                    <!--通过限制定义自动包装的枚举类型-->
                    <xsd:restriction base="xsd:NMTOKEN">
                        <xsd:enumeration value="default"/>
                        <xsd:enumeration value="no"/>
                        <xsd:enumeration value="byName"/>
                        <xsd:enumeration value="byType"/>
                        <xsd:enumeration value="constructor"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="default-autowire-candidates" type="xsd:string"/>
            <xsd:attribute name="default-init-method" type="xsd:string"/>
            <xsd:attribute name="default-destroy-method" type="xsd:string"/>
            <!--还可以有其它命名空间的属性,并且不会做强制的检查-->
            <xsd:anyAttribute namespace="##other" processContents="lax"/>
        </xsd:complexType>
    </xsd:element>

    <!--定义描述信息元素,混合内容元素-->
    <xsd:element name="description">
        <xsd:complexType mixed="true">
            <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="import">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="resource" type="xsd:string" use="required"/>
                </xsd:restriction>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="alias">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="name" type="xsd:string" use="required"/>
                    <xsd:attribute name="alias" type="xsd:string" use="required"/>
                </xsd:restriction>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:group name="beanElements">
        <xsd:sequence>
            <xsd:element ref="description" minOccurs="0"/>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element ref="meta"/>
                <xsd:element ref="constructor-arg"/>
                <xsd:element ref="property"/>
                <xsd:element ref="qualifier"/>
                <xsd:element ref="lookup-method"/>
                <xsd:element ref="replaced-method"/>
                <xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:choice>
        </xsd:sequence>
    </xsd:group>

    <xsd:attributeGroup name="beanAttributes">
        <xsd:attribute name="name" type="xsd:string"/>
        <xsd:attribute name="class" type="xsd:string"/>
        <xsd:attribute name="parent" type="xsd:string"/>
        <xsd:attribute name="scope" type="xsd:string"/>
        <xsd:attribute name="abstract" type="xsd:boolean"/>
        <xsd:attribute name="lazy-init" default="default" type="defaultable-boolean"/>
        <xsd:attribute name="autowire" default="default">
            <xsd:simpleType>
                <xsd:restriction base="xsd:NMTOKEN">
                    <xsd:enumeration value="default"/>
                    <xsd:enumeration value="no"/>
                    <xsd:enumeration value="byName"/>
                    <xsd:enumeration value="byType"/>
                    <xsd:enumeration value="constructor"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:attribute>
        <xsd:attribute name="depends-on" type="xsd:string"/>
        <xsd:attribute name="autowire-candidate" default="default" type="defaultable-boolean"/>
        <xsd:attribute name="primary" type="xsd:boolean"/>
        <xsd:attribute name="init-method" type="xsd:string"/>
        <xsd:attribute name="destroy-method" type="xsd:string"/>
        <xsd:attribute name="factory-method" type="xsd:string"/>
        <xsd:attribute name="factory-bean" type="xsd:string"/>
        <xsd:anyAttribute namespace="##other" processContents="lax"/>
    </xsd:attributeGroup>

    <xsd:element name="meta" type="metaType"/>

    <xsd:complexType name="metaType">
        <xsd:attribute name="key" type="xsd:string" use="required"/>
        <xsd:attribute name="value" type="xsd:string" use="required"/>
    </xsd:complexType>

    <xsd:element name="bean">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="identifiedType">
                    <xsd:group ref="beanElements"/>
                    <xsd:attributeGroup ref="beanAttributes"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="constructor-arg">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="description" minOccurs="0"/>
                <xsd:choice minOccurs="0" maxOccurs="1">
                    <xsd:element ref="bean"/>
                    <xsd:element ref="ref"/>
                    <xsd:element ref="idref"/>
                    <xsd:element ref="value"/>
                    <xsd:element ref="null"/>
                    <xsd:element ref="array"/>
                    <xsd:element ref="list"/>
                    <xsd:element ref="set"/>
                    <xsd:element ref="map"/>
                    <xsd:element ref="props"/>
                    <xsd:any namespace="##other" processContents="strict"/>
                </xsd:choice>
            </xsd:sequence>
            <xsd:attribute name="index" type="xsd:string"/>
            <xsd:attribute name="type" type="xsd:string"/>
            <xsd:attribute name="name" type="xsd:string"/>
            <xsd:attribute name="ref" type="xsd:string"/>
            <xsd:attribute name="value" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="property" type="propertyType"/>

    <xsd:element name="qualifier">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="attribute" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
            <xsd:attribute name="type" type="xsd:string" default="org.springframework.beans.factory.annotation.Qualifier"/>
            <xsd:attribute name="value" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="attribute" type="metaType"/>

    <xsd:element name="lookup-method">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="name" type="xsd:string"/>
                    <xsd:attribute name="bean" type="xsd:string"/>
                </xsd:restriction>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="replaced-method">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:choice minOccurs="0" maxOccurs="unbounded">
                    <xsd:element ref="arg-type"/>
                </xsd:choice>
            </xsd:sequence>
            <xsd:attribute name="name" type="xsd:string"/>
            <xsd:attribute name="replacer" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="arg-type">
        <xsd:complexType mixed="true">
            <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
            <xsd:attribute name="match" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="ref">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="bean" type="xsd:string"/>
                    <xsd:attribute name="local" type="xsd:string"/>
                    <xsd:attribute name="parent" type="xsd:string"/>
                </xsd:restriction>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="idref">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:restriction base="xsd:anyType">
                    <xsd:attribute name="bean" type="xsd:string"/>
                    <xsd:attribute name="local" type="xsd:string"/>
                </xsd:restriction>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="value">
        <xsd:complexType mixed="true">
            <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
            <xsd:attribute name="type" type="xsd:string"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="null">
        <xsd:complexType mixed="true">
            <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
        </xsd:complexType>
    </xsd:element>

    <!-- Collection Elements -->
    <xsd:group name="collectionElements">
        <xsd:sequence>
            <xsd:element ref="description" minOccurs="0"/>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
                <xsd:element ref="bean"/>
                <xsd:element ref="ref"/>
                <xsd:element ref="idref"/>
                <xsd:element ref="value"/>
                <xsd:element ref="null"/>
                <xsd:element ref="array"/>
                <xsd:element ref="list"/>
                <xsd:element ref="set"/>
                <xsd:element ref="map"/>
                <xsd:element ref="props"/>
                <xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:choice>
        </xsd:sequence>
    </xsd:group>

    <xsd:element name="array">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="listOrSetType">
                    <xsd:attribute name="merge" default="default" type="defaultable-boolean"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="list">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="listOrSetType">
                    <xsd:attribute name="merge" default="default" type="defaultable-boolean"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="set">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="listOrSetType">
                    <xsd:attribute name="merge" default="default" type="defaultable-boolean"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="map">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="mapType">
                    <xsd:attribute name="merge" default="default" type="defaultable-boolean"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="entry" type="entryType"/>

    <xsd:element name="props">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="propsType">
                    <xsd:attribute name="merge" default="default" type="defaultable-boolean"/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="key">
        <xsd:complexType>
            <xsd:group ref="collectionElements"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="prop">
        <xsd:complexType mixed="true">
            <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
            <xsd:attribute name="key" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="propertyType">
        <xsd:sequence>
            <xsd:element ref="description" minOccurs="0"/>
            <xsd:choice minOccurs="0" maxOccurs="1">
                <xsd:element ref="meta"/>
                <xsd:element ref="bean"/>
                <xsd:element ref="ref"/>
                <xsd:element ref="idref"/>
                <xsd:element ref="value"/>
                <xsd:element ref="null"/>
                <xsd:element ref="array"/>
                <xsd:element ref="list"/>
                <xsd:element ref="set"/>
                <xsd:element ref="map"/>
                <xsd:element ref="props"/>
                <xsd:any namespace="##other" processContents="strict"/>
            </xsd:choice>
        </xsd:sequence>
        <xsd:attribute name="name" type="xsd:string" use="required"/>
        <xsd:attribute name="ref" type="xsd:string"/>
        <xsd:attribute name="value" type="xsd:string"/>
    </xsd:complexType>

    <!-- base type for collections that have (possibly) typed nested values -->
    <xsd:complexType name="collectionType">
        <xsd:attribute name="value-type" type="xsd:string"/>
    </xsd:complexType>

    <!-- 'list' and 'set' collection type -->
    <xsd:complexType name="listOrSetType">
        <xsd:complexContent>
            <xsd:extension base="collectionType">
                <xsd:group ref="collectionElements"/>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

    <!-- 'map' element type -->
    <xsd:complexType name="mapType">
        <xsd:complexContent>
            <xsd:extension base="collectionType">
                <xsd:sequence>
                    <xsd:element ref="description" minOccurs="0"/>
                    <xsd:choice minOccurs="0" maxOccurs="unbounded">
                        <xsd:element ref="entry"/>
                    </xsd:choice>
                </xsd:sequence>
                <xsd:attribute name="key-type" type="xsd:string"/>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

    <!-- 'entry' element type -->
    <xsd:complexType name="entryType">
        <xsd:sequence>
            <xsd:element ref="key" minOccurs="0"/>
            <xsd:group ref="collectionElements"/>
        </xsd:sequence>
        <xsd:attribute name="key" type="xsd:string"/>
        <xsd:attribute name="key-ref" type="xsd:string"/>
        <xsd:attribute name="value" type="xsd:string"/>
        <xsd:attribute name="value-ref" type="xsd:string"/>
        <xsd:attribute name="value-type" type="xsd:string"/>
    </xsd:complexType>

    <!-- 'props' collection type -->
    <xsd:complexType name="propsType">
        <xsd:complexContent>
            <xsd:extension base="collectionType">
                <xsd:sequence>
                    <xsd:choice minOccurs="0" maxOccurs="unbounded">
                        <xsd:element ref="prop"/>
                    </xsd:choice>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

    <!-- 将true|false|default定义为枚举布尔类型-->
    <xsd:simpleType name="defaultable-boolean">
        <xsd:restriction base="xsd:NMTOKEN">
            <xsd:enumeration value="default"/>
            <xsd:enumeration value="true"/>
            <xsd:enumeration value="false"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>
复制代码
 
 
分类: XML
标签: XML
原文地址:https://www.cnblogs.com/Leo_wl/p/3311259.html