XML 学习笔记 7:XSD

本文章内容参考自:
W3school XSD 教程
Extensible Markup Language (XML) 1.0 (Second Edition)
XML Schema 2001
XML Schema Part 2: Datatypes Second Edition

文章目录

    • [1、XSD 是什么](#1、XSD 是什么)
    • [2、XSD 内置数据类型 - built-in datatypes](#2、XSD 内置数据类型 - built-in datatypes)
      • [2.1、基本数据类型 19 种](#2.1、基本数据类型 19 种)
        • [2.1.1、基本字符串类型 1 种](#2.1.1、基本字符串类型 1 种)
          • [2.1.1.1、字符串 - string 简单类型](#2.1.1.1、字符串 - string 简单类型)
        • [2.1.2、基本数值类型 3 种](#2.1.2、基本数值类型 3 种)
          • [2.1.2.1、十进制数 - decimal 简单类型](#2.1.2.1、十进制数 - decimal 简单类型)
          • [2.1.2.2、单精度浮点数 - float 简单类型](#2.1.2.2、单精度浮点数 - float 简单类型)
          • [2.1.2.3、双精度浮点数 - double 简单类型](#2.1.2.3、双精度浮点数 - double 简单类型)
        • [2.1.3、日期时间类型 9 种](#2.1.3、日期时间类型 9 种)
          • [2.1.3.1、日期 - date 简单类型](#2.1.3.1、日期 - date 简单类型)
          • [2.1.3.2、时间 - time 简单类型](#2.1.3.2、时间 - time 简单类型)
          • [2.1.3.3、日期时间 - dateTime 简单类型](#2.1.3.3、日期时间 - dateTime 简单类型)
          • [2.1.3.4、年 - gYear 简单类型](#2.1.3.4、年 - gYear 简单类型)
          • [2.1.3.5、年和月 - gYearMonth 简单类型](#2.1.3.5、年和月 - gYearMonth 简单类型)
          • [2.1.3.6、月 - gMonth 简单类型](#2.1.3.6、月 - gMonth 简单类型)
          • [2.1.3.7、月和天 - gMonthDay 简单类型](#2.1.3.7、月和天 - gMonthDay 简单类型)
          • [2.1.3.8、天 - gDay 简单类型](#2.1.3.8、天 - gDay 简单类型)
          • [2.1.3.9、周期 - duration 简单类型](#2.1.3.9、周期 - duration 简单类型)
        • [2.1.4、布尔类型 1 种](#2.1.4、布尔类型 1 种)
          • [2.1.4.1、布尔 - boolean 简单类型](#2.1.4.1、布尔 - boolean 简单类型)
        • [2.1.5、编码二进制类型 2 种](#2.1.5、编码二进制类型 2 种)
          • [2.1.5.1、Base64 编码 - base64Binary 简单类型](#2.1.5.1、Base64 编码 - base64Binary 简单类型)
          • [2.1.5.2、十六进制编码 - hexBinary 简单类型](#2.1.5.2、十六进制编码 - hexBinary 简单类型)
        • [2.1.6、其他基本数据类型 3 种](#2.1.6、其他基本数据类型 3 种)
          • [2.1.6.1、统一资源标识符 - URI 简单类型](#2.1.6.1、统一资源标识符 - URI 简单类型)
          • [2.1.6.2、限定名 - QName 简单类型](#2.1.6.2、限定名 - QName 简单类型)
          • [2.1.6.3、实体标记 - NOTATION 简单类型](#2.1.6.3、实体标记 - NOTATION 简单类型)
      • [2.2、派生数据类型 25 种](#2.2、派生数据类型 25 种)
        • [2.2.1、派生字符串类型 12 种](#2.2.1、派生字符串类型 12 种)
          • [2.2.1.1、名称标记 - NMTOKEN 简单类型](#2.2.1.1、名称标记 - NMTOKEN 简单类型)
          • [2.2.1.2、用户定义名称 - NCName 简单类型](#2.2.1.2、用户定义名称 - NCName 简单类型)
          • [2.2.1.3、实体 - ENTITIE 简单类型](#2.2.1.3、实体 - ENTITIE 简单类型)
        • [2.2.2、派生数值类型 13 种](#2.2.2、派生数值类型 13 种)
    • 3、派生类型
    • [4、XSD 简单类型](#4、XSD 简单类型)
      • 4.1、简单元素
      • [4.2、属性 - attribute](#4.2、属性 - attribute)
      • [4.3、限定 12 种 - facet 类型](#4.3、限定 12 种 - facet 类型)
        • [4.3.1、有序限定 4 种](#4.3.1、有序限定 4 种)
          • [4.3.1.1、左开区间 - minExclusive 元素](#4.3.1.1、左开区间 - minExclusive 元素)
          • [4.3.1.2、左闭区间 - minInclusive 元素](#4.3.1.2、左闭区间 - minInclusive 元素)
          • [4.3.1.3、右开区间 - maxExclusive 元素](#4.3.1.3、右开区间 - maxExclusive 元素)
          • [4.3.1.4、右闭区间 - maxInclusive 元素](#4.3.1.4、右闭区间 - maxInclusive 元素)
        • [4.3.2、数值限定 5 种](#4.3.2、数值限定 5 种)
          • [4.3.2.1、总位数 - totalDigits 元素](#4.3.2.1、总位数 - totalDigits 元素)
          • [4.3.2.2、小数位数 - fractionDigits 元素](#4.3.2.2、小数位数 - fractionDigits 元素)
          • [4.3.2.3、长度 - length 元素](#4.3.2.3、长度 - length 元素)
          • [4.3.2.4、最小长度 - minLength 元素](#4.3.2.4、最小长度 - minLength 元素)
          • [4.3.2.5、最大长度 - maxLength 元素](#4.3.2.5、最大长度 - maxLength 元素)
        • [4.3.3、非固定值限定 2 种](#4.3.3、非固定值限定 2 种)
          • [4.3.3.1、枚举值限定 - enumeration 元素](#4.3.3.1、枚举值限定 - enumeration 元素)
          • [4.3.3.2、字符串匹配模式 - pattern 元素](#4.3.3.2、字符串匹配模式 - pattern 元素)
        • [4.3.4、空白字符限定 1 种](#4.3.4、空白字符限定 1 种)
          • [4.3.4.1、空白字符处理 - whiteSpace 元素](#4.3.4.1、空白字符处理 - whiteSpace 元素)
    • [5、XSD 复合类型](#5、XSD 复合类型)
      • 复合类型指示器
        • [Occurrence 指示器 - occurs 属性组](#Occurrence 指示器 - occurs 属性组)
          • [minOccurs 属性](#minOccurs 属性)
          • [maxOccurs 属性](#maxOccurs 属性)
        • [Order 指示器 - explicitGroup 复合类型](#Order 指示器 - explicitGroup 复合类型)
          • [choice 元素和 sequence 元素](#choice 元素和 sequence 元素)
          • [all 元素](#all 元素)
        • [Group 指示器](#Group 指示器)
          • [元素组 - realGroup 复合类型](#元素组 - realGroup 复合类型)
          • [属性组 - attributeGroup 复合类型](#属性组 - attributeGroup 复合类型)
      • 复合元素
      • [通配符类型 - wildcard 复合类型](#通配符类型 - wildcard 复合类型)
      • [任何元素 - any 元素](#任何元素 - any 元素)
      • [任何属性 - anyAttribute 元素](#任何属性 - anyAttribute 元素)
      • [注释 - annotation 元素](#注释 - annotation 元素)

1、XSD 是什么

XSD(XML Schema Definition)用来描述 XML 文档的结构,是 DTD 的替代者,功能比 DTD 更强大。

  1. XML Schema 基于 XML 编写

    • 可扩展
    • 可以通过 XML 解析器来解析 Schema 文件
    • 可以通过 XML DOM 来处理 Schema
    • 可以通过 XSLT 来转换 Schema
  2. XML Schema 支持数据类型

    • 可更容易地验证数据的正确性
    • 可以更容易地定义数据格式
    • 可以更容易地定义数据约束(data facets)
    • 可以更容易地在不同的数据类型间转换数据

通过 XML Schema,数据的发送方可以用一种接收方能够理解的方式来描述数据。

例如 "03-11-2004",在某些国家被解释为 11 月 3 日,而在另一些国家被解释为 3 月 11 日。

但是一个带有数据类型的 XML 元素 <date type="date">2004-03-11</date>,可以确保对数据内容的一致理解,这是因为 XML 数据类型 xs:date 要求的格式是 YYYY-MM-DD

XML Schema 在 2001 年 5 月 2 日成为 W3C 标准


2、XSD 内置数据类型 - built-in datatypes

XSD 规定 schema 的数据类型包括 44 种内置的数据类型(built-in datatypes)和用户派生的数据类型(user-derived datatypes)。

44 种内置类型包括 19 种从 anySimpleType 派生的基本类型 (primitive datatypes)和 25 种从基本类型派生的派生基本类型(derived primitive types)。

19 种基本类型包括:

  • 1 种字符串类型 string
  • 3 种数值类型 decimal、float、double
  • 9 种日期时间类型 date、time、datetime、gYear、gYearMonth、gMonth、gMonthDay、gDay 以及周期类型 duration
  • 1 种布尔类型 boolean
  • 2 种编码二进制类型 base64Binary、hexBinary
  • 3 种其他类型 anyURI、QName、NOTATION

25 种派生类型又包括 12 种字符串派生类型以及 13 种数值派生类型。如下图示:

2.1、基本数据类型 19 种

2.1.1、基本字符串类型 1 种

数据类型 派生类型 限制 描述
xs:string anySimpleType 类型 whiteSpace="preserve" 字符串。XML 处理器不会做任何处理,保留空格、换行、回车以及制表符
2.1.1.1、字符串 - string 简单类型

2.1.2、基本数值类型 3 种

数据类型 派生类型 限制 描述
xs:decimal anySimpleType 类型 whiteSpace="collapse"、fixed="true" 十进制数。最大位数是 18 位。例如 -999.52
xs:float anySimpleType 类型 whiteSpace="collapse"、fixed="true" 32 位单精度浮点数
xs:double anySimpleType 类型 whiteSpace="collapse"、fixed="true" 64 位双精度浮点数
2.1.2.1、十进制数 - decimal 简单类型
2.1.2.2、单精度浮点数 - float 简单类型
2.1.2.3、双精度浮点数 - double 简单类型

2.1.3、日期时间类型 9 种

数据类型 派生类型 限制 描述
xs:date anySimpleType 类型 whiteSpace="collapse"、fixed="true" YYYY-MM-DD
xs:time anySimpleType 类型 whiteSpace="collapse"、fixed="true" hh:mm:ss
xs:dateTime anySimpleType 类型 whiteSpace="collapse"、fixed="true" YYYY-MM-DDThh:mm:ss,T 表示时间部分的起始
xs:gYear anySimpleType 类型 whiteSpace="collapse"、fixed="true" 定义日期时间的一个部分 - 年 (YYYY)
xs:gYearMonth anySimpleType 类型 whiteSpace="collapse"、fixed="true" 定义日期时间的一个部分 - 年和月 (YYYY-MM)
xs:gMonth anySimpleType 类型 whiteSpace="collapse"、fixed="true" 定义日期时间的一个部分 - 月 (MM)
xs:gMonthDay anySimpleType 类型 whiteSpace="collapse"、fixed="true" 定义日期时间的一个部分 - 月和天 (MM-DD)
xs:gDay anySimpleType 类型 whiteSpace="collapse"、fixed="true" 定义日期时间的一个部分 - 天 (DD)
xs:duration anySimpleType 类型 whiteSpace="collapse"、fixed="true" PnYnMnDTnHnMnSn 表示年月日时分秒的数目,P 表示周期,T 表示时间部分的起始。例如 P5Y2M10DT15H 表示 5 年零 2 个月零 10天 零 15 小时
2.1.3.1、日期 - date 简单类型
2.1.3.2、时间 - time 简单类型
2.1.3.3、日期时间 - dateTime 简单类型
2.1.3.4、年 - gYear 简单类型
xml 复制代码
<xs:simpleType name="gYear" id="gYear">
    <xs:annotation>
        <xs:appinfo>
            <hfp:hasFacet name="pattern"/>
            <hfp:hasFacet name="enumeration"/>
            <hfp:hasFacet name="whiteSpace"/>
            <hfp:hasFacet name="maxInclusive"/>
            <hfp:hasFacet name="maxExclusive"/>
            <hfp:hasFacet name="minInclusive"/>
            <hfp:hasFacet name="minExclusive"/>
            <hfp:hasProperty name="ordered" value="partial"/>
            <hfp:hasProperty name="bounded" value="false"/>
            <hfp:hasProperty name="cardinality" value="countably infinite"/>
            <hfp:hasProperty name="numeric" value="false"/>
        </xs:appinfo>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#gYear"/>
    </xs:annotation>
    
    <xs:restriction base="xs:anySimpleType">
        <xs:whiteSpace value="collapse" fixed="true" id="gYear.whiteSpace"/>
    </xs:restriction>
</xs:simpleType>

[Definition]
gYear represents a gregorian calendar year. The ·value space· of gYear is the set of Gregorian calendar years as defined in § 5.2.1 of [ISO 8601]. Specifically, it is a set of one-year long, non-periodic instances e.g. lexical 1999 to represent the whole year 1999, independent of how many months and days this year has.

gYear 表示公历年。gYear 的值空间是 ISO 8601 中定义的一组公历年份。具体来说,它是一组一年长的非周期实例。例如词汇 1999,代表 1999 年全年,与该年的月份和天数无关。

2.1.3.5、年和月 - gYearMonth 简单类型
2.1.3.6、月 - gMonth 简单类型
2.1.3.7、月和天 - gMonthDay 简单类型
2.1.3.8、天 - gDay 简单类型
2.1.3.9、周期 - duration 简单类型

2.1.4、布尔类型 1 种

数据类型 派生类型 限制 描述
xs:boolean anySimpleType 类型 whiteSpace="collapse"、fixed="true" true 或 false
2.1.4.1、布尔 - boolean 简单类型

2.1.5、编码二进制类型 2 种

数据类型 派生类型 限制 描述
xs:base64Binary anySimpleType 类型 whiteSpace="collapse"、fixed="true" Base64 编码的二进制数据
xs:hexBinary anySimpleType 类型 whiteSpace="collapse"、fixed="true" 十六进制编码的二进制数据
2.1.5.1、Base64 编码 - base64Binary 简单类型
2.1.5.2、十六进制编码 - hexBinary 简单类型

2.1.6、其他基本数据类型 3 种

数据类型 派生类型 限制 描述
xs:anyURI anySimpleType 类型 whiteSpace="collapse"、fixed="true" 转义URI
xs:QName anySimpleType 类型 whiteSpace="collapse"、fixed="true" 命名空间名称和本地名称组成的限定名
xs:NOTATION anySimpleType 类型 whiteSpace="collapse"、fixed="true" 非 XML 格式的数据
2.1.6.1、统一资源标识符 - URI 简单类型
2.1.6.2、限定名 - QName 简单类型
xml 复制代码
<xs:simpleType name="QName" id="QName">
    <xs:annotation>
        <xs:appinfo>
            <hfp:hasFacet name="length"/>
            <hfp:hasFacet name="minLength"/>
            <hfp:hasFacet name="maxLength"/>
            <hfp:hasFacet name="pattern"/>
            <hfp:hasFacet name="enumeration"/>
            <hfp:hasFacet name="whiteSpace"/>
            <hfp:hasProperty name="ordered" value="false"/>
            <hfp:hasProperty name="bounded" value="false"/>
            <hfp:hasProperty name="cardinality" value="countably infinite"/>
            <hfp:hasProperty name="numeric" value="false"/>
        </xs:appinfo>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#QName"/>
    </xs:annotation>

    <xs:restriction base="xs:anySimpleType">
        <xs:whiteSpace value="collapse" fixed="true" id="QName.whiteSpace"/>
    </xs:restriction>
</xs:simpleType>

[Definition]
QName represents XML qualified names. The value space of QName is the set of tuples {namespace name, local part}, where namespace name is an anyURI and local part is an NCName. The lexical space of QName is the set of strings that match the QName production of [Namespaces in XML].

限定名称由命名空间名称和本地部分组成,命名空间名称是任意 URI,而本地部分则是由用户定义的名称(NCName,non-colonized Name)。

2.1.6.3、实体标记 - NOTATION 简单类型
xml 复制代码
<xs:simpleType name="NOTATION" id="NOTATION">
    <xs:annotation>
        <xs:appinfo>
            <hfp:hasFacet name="length"/>
            <hfp:hasFacet name="minLength"/>
            <hfp:hasFacet name="maxLength"/>
            <hfp:hasFacet name="pattern"/>
            <hfp:hasFacet name="enumeration"/>
            <hfp:hasFacet name="whiteSpace"/>
            <hfp:hasProperty name="ordered" value="false"/>
            <hfp:hasProperty name="bounded" value="false"/>
            <hfp:hasProperty name="cardinality" value="countably infinite"/>
            <hfp:hasProperty name="numeric" value="false"/>
        </xs:appinfo>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#NOTATION"/>
    </xs:annotation>
    
    <xs:restriction base="xs:anySimpleType">
        <xs:whiteSpace value="collapse" fixed="true" id="NOTATION.whiteSpace"/>
    </xs:restriction>
</xs:simpleType>

[Definition : Notations identify by name the format of unparsed entities, the format of elements which bear a notation attribute, or the application to which a processing instruction is addressed.]

NOTATION 通过名称标识:

  1. 未解析实体的格式
  2. 能够帮助 XML 处理器处理实体的外部应用程序

Schema Component Constraint: enumeration facet value required for NOTATION
It is an error for NOTATION to be used directly in a schema. Only datatypes that are derived from NOTATION by specifying a value for enumeration can be used in a schema.

在 schema 中直接使用 NOTATION 类型是不合法的,只能使用通过 enumeration 限制派生后的类型。

例如:

xml 复制代码
<xs:notation name="gif" public="image/gif" system="view.exe"/>
<xs:notation name="jpeg" public="image/jpeg" system="view.exe"/>

<xs:element name="image">
    <xs:complexType>
        <xs:simpleContent>
            <xs:attribute name="type">
                <xs:simpleType>
                    <xs:restriction base="xs:NOTATION">
                        <xs:enumeration value="gif"/>
                        <xs:enumeration value="jpeg"/>
                    <xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

<image type="gif"></image>

2.2、派生数据类型 25 种

2.2.1、派生字符串类型 12 种

数据类型 派生类型 限制 描述
xs:normalizedString string 类型 whiteSpace="replace" 规范化字符串。XML 处理器会移除制表符、换行以及回车
xs:token normalizedString 类型 whiteSpace="collapse" 标记。XML 处理器会移除制表符、换行、回车以及开头和结尾的空格,多个连续空格的内部序列会被缩减为一个空格
xs:language token 类型 pattern="[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*" RFC3066 规定的合法的语言标识
xs:NMTOKEN token 类型 pattern="\c+",\c 表示NameChar 名称标记
xs:NMTOKENS NMTOKEN 集合 list itemType="xs:NMTOKEN"、minLength=1 名称标记集合
xs:Name token 类型 pattern="\i\c*",\i 表示 NameStartChar,\c 表示 NameChar 名称
xs:NCName Name 类型 pattern="[\i-[:]][\c-[:]]*",\i 表示 NameStartChar,\c 表示NameChar "non-colonized" Name,即用户定义名称
xs:ID NCName 类型 唯一标识
xs:IDREF NCName 类型 唯一标识引用
xs:IDREFS IDREF 集合 list itemType="xs:IDREF"、minLength=1 唯一标识引用集合
xs:ENTITIE NCName 类型 实体
xs:ENTITIES ENTITY 集合 list itemType="xs:ENTITY"、minLength=1 实体集合
2.2.1.1、名称标记 - NMTOKEN 简单类型
xml 复制代码
<xs:simpleType name="NMTOKEN" id="NMTOKEN">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#NMTOKEN"/>
    </xs:annotation>
    
    <xs:restriction base="xs:token">
        <xs:pattern value="\c+" id="NMTOKEN.pattern">
            <xs:annotation>
                <xs:documentation source="http://www.w3.org/TR/REC-xml#NT-Nmtoken">
                    pattern matches production 7 from the XML spec
                </xs:documentation>
            </xs:annotation>
        </xs:pattern>
    </xs:restriction>
</xs:simpleType>

pattern:

xml 复制代码
[4]    NameStartChar  ::=  ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
[4a]   NameChar  ::=  NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5]    Name  ::=  NameStartChar (NameChar)*
[6]    Names  ::=  Name (#x20 Name)*
[7]    Nmtoken  ::=  (NameChar)+
[8]    Nmtokens  ::=  Nmtoken (#x20 Nmtoken)*
2.2.1.2、用户定义名称 - NCName 简单类型
xml 复制代码
<xs:simpleType name="NCName" id="NCName">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#NCName"/>
    </xs:annotation>

    <xs:restriction base="xs:Name">
        <xs:pattern value="[\i-[:]][\c-[:]]*" id="NCName.pattern">
            <xs:annotation>
                <xs:documentation source="http://www.w3.org/TR/REC-xml-names/#NT-NCName">
                    pattern matches production 4 from the Namespaces in XML spec
                </xs:documentation>
            </xs:annotation>
        </xs:pattern>
    </xs:restriction>
</xs:simpleType>
2.2.1.3、实体 - ENTITIE 简单类型

2.2.2、派生数值类型 13 种

数据类型 描述
xs:integer fractionDigits=0、pattern="[\-+]?[0-9]+" 限制,从 decimal 派生的整数,用于规定无小数部分的数值
xs:nonPositiveInteger maxInclusive=0 限制,从 integer 派生的非正整数(0 和负整数)
xs:negativeInteger 负整数
xs:long minInclusive、maxInclusive 限制,从 integer 派生有正负的 64 位整数
xs:int 有正负的 32 位整数
xs:short 有正负的 16 位整数
xs:byte 有正负的 8 位整数
xs:nonNegativeInteger 非负整数(0 和正整数)
xs:positiveInteger 正整数
xs:unsignedLong 无正负的 64 位整数
xs:unsignedInt 无正负的 32 位整数
xs:unsignedShort 无正负的 16 位整数
xs:unsignedByte 无正负的 8 位整数

3、派生类型

3.1、扩展派生类型 - extension 元素

类型扩展即 extension 元素,定义对简单类型 simpleType 或复合类型 simpleContentcomplexContent扩展,也就是为类型添加新的元素或属性。

xml 复制代码
<extension
id=ID
base=QName
any attributes
>
    (annotation?,
        ((group|all|choice|sequence)?,
            ((attribute|attributeGroup)*,anyAttribute?)))
</extension>

使用扩展定义一个复合类型 Chinese_customer,它从复合类型 customer 派生,拥有新的子元素 country

xml 复制代码
<xs:complexType name="customer">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="Chinese_customer">
    <xs:complexContent>
        <xs:extension base="customer">
            <xs:sequence>
                <xs:element name="country" type="xs:string"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

3.2、限制派生类型 - restrictionType 复合类型

xml 复制代码
<xs:complexType name="restrictionType">
    <xs:complexContent>
        <xs:extension base="xs:annotated">
            <xs:sequence>
                <xs:choice minOccurs="0">
                    <xs:group ref="xs:typeDefParticle"/>
                    <xs:group ref="xs:simpleRestrictionModel"/>
                </xs:choice>
                <xs:group ref="xs:attrDecls"/>
            </xs:sequence>
            <xs:attribute name="base" type="xs:QName" use="required"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

类型限制即 restriction 元素,定义对简单类型 simpleType 或复合类型 simpleContentcomplexContent约束,并不会为类型添加新的元素或属性。

xml 复制代码
<restriction
id=ID
base=QName
any attributes
>
    <!-- Content for simpleType -->
    (annotation?,
        (simpleType?,
            (minExclusive|minInclusive|maxExclusive|maxInclusive|
             totalDigits|fractionDigits|
             length|minLength|maxLength|
             enumeration|whiteSpace|pattern)*))

    <!-- Content for simpleContent -->
    (annotation?,
        (simpleType?,
            (minExclusive |minInclusive|maxExclusive|maxInclusive|
             totalDigits|fractionDigits|
             length|minLength|maxLength|
             enumeration|whiteSpace|pattern)*)?, 
                ((attribute|attributeGroup)*,anyAttribute?))

    <!-- Content for complexContent -->
    (annotation?,
        (group|all|choice|sequence)?,
            ((attribute|attributeGroup)*,anyAttribute?))
</restriction>

例如使用约束定义一个复合类型 Chinese_customer,它从复合类型 customer 派生,子元素 country 的固定值是 "China":

xml 复制代码
<xs:complexType name="customer">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="Chinese_customer">
    <xs:complexContent>
        <xs:restriction base="customer">
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="country" type="xs:string" fixed="China"/>
            </xs:sequence>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

3.3、简单类型的扩展和限制

simpleContent 元素是对简单类型 simpleType 元素的扩展或限制,简单类型扩展后会变成复合类型

xml 复制代码
<xs:element name="simpleContent" id="simpleContent">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-simpleContent"/>
    </xs:annotation>
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="xs:annotated">
                <xs:choice>
                    <xs:element name="restriction" type="xs:simpleRestrictionType"/>
                    <xs:element name="extension" type="xs:simpleExtensionType"/>
                </xs:choice>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

例如对简单类型 integer 进行扩展后获得一个复合类型 shoesize,其内容被定义为整数数据类型,并包含一个 country 属性:

xml 复制代码
<xs:element name="shoesize">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:integer">
                <xs:attribute name="country" type="xs:string" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

3.4、复合类型的扩展和限制

complexContent 元素定义对复合类型 complexType 元素的扩展或限制。

xml 复制代码
<xs:element name="complexContent" id="complexContent">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-complexContent"/>
    </xs:annotation>
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="xs:annotated">
                <xs:choice>
                    <xs:element name="restriction" type="xs:complexRestrictionType"/>
                    <xs:element name="extension" type="xs:extensionType"/>
                </xs:choice>
                <xs:attribute name="mixed" type="xs:boolean"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

3.5、集合派生类型


4、XSD 简单类型

simpleType 元素定义一个简单类型,规定与

  1. 仅包含文本内容的元素
  2. 属性 的值有关的信息
  3. 以及对它们的 约束
xml 复制代码
<simpleType id=ID name=NCName anyAttribute>
    (annotation?,(restriction|list|union))
</simpleType>

例如声明 age 元素是一个带有约束(restriction)的简单类型(age 元素的值不能小于 0 或大于 100):

xml 复制代码
<xs:element name="age">
    <xs:simpleType>
        <xs:restriction base="xs:integer">
            <xs:minInclusive value="0"/>
            <xs:maxInclusive value="100"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

4.1、简单元素

简单元素指那些仅包含文本内容的元素,它不包含任何其他的元素或属性。

不过,"仅包含文本内容" 这个限定却很容易造成误解。文本内容有很多种类型,它可以是 XML Schema 定义类型中的一种(布尔、字符串、数值等),它也可以是自定义的定制类型。

可以向数据类型添加限定(facets),以此来限制它的内容,或者可以要求数据匹配某种特定的模式(facets 中的 pattern)。

xml 复制代码
<xs:element name="元素名称" type="数据类型" default="默认值" fixed="固定值"/>

例如声明两个简单元素 nameage

xml 复制代码
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>

<name>Smith</name>
<age>28</age>

4.2、属性 - attribute

attribute 类型是从 annotated 类型扩展派生,拥有 simpleType 子元素和 type、use、default、fixed、form 等属性。

xml 复制代码
<xs:complexType name="attribute">
    <xs:complexContent>
        <xs:extension base="xs:annotated">
            <xs:sequence>
                <xs:element name="simpleType" minOccurs="0" type="xs:localSimpleType"/>
            </xs:sequence>
            <xs:attributeGroup ref="xs:defRef"/>
            <xs:attribute name="type" type="xs:QName"/>
            <xs:attribute name="use" use="optional" default="optional">
                <xs:simpleType>
                    <xs:restriction base="xs:NMTOKEN">
                        <xs:enumeration value="prohibited"/>
                        <xs:enumeration value="optional"/>
                        <xs:enumeration value="required"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
            <xs:attribute name="default" type="xs:string"/>
            <xs:attribute name="fixed" type="xs:string"/>
            <xs:attribute name="form" type="xs:formChoice"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

attribute 元素定义一个属性。

xml 复制代码
<attribute
default=string
fixed=string
form=qualified|unqualified
id=ID
name=NCName
ref=QName
type=QName
use=optional|prohibited|required
any attributes
>
    (annotation?,(simpleType?))
</attribute>

  • use 属性
    可选。规定如何使用该属性。默认值是 optional 可选的。可以设置为下列值:
    1. optional - 属性是可选的
    2. prohibited - 属性不能使用
    3. required - 属性是必需的

  • form 属性
    可选。规定属性的格式。默认值是 包含该属性的元素 的 attributeFormDefault 属性的值。可以设置为下列值:
    1. qualified - 指示必须通过命名空间前缀和该属性的本地名称(即无冒号名称,NCName)来限定此属性
    2. unqualified - 指示此属性无须通过命名空间前缀限定,且无须匹配此属性的本地名称 (无须匹配本地名称不太理解是什么意思?)

  • defaultfixed 属性不能同时出现
  • refnameformtype 等属性不能同时出现
  • ref 属性和 simpleType 子元素不能同时出现
  • type 属性和 simpleType 子元素不能同时出现

例如声明一个 country 属性,限定属性值是大写字母 A 到 Z 中的两个字母:

xml 复制代码
<xs:attribute name="country">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Z][A-Z]"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

4.3、限定 12 种 - facet 类型

本部分内容翻译自W3C Recommendation

Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.

限定(restriction)用于为 XML 元素或者属性定义可接受的值。对 XML 元素的限定被称为 facet (/ˈfæsɪt/,方面)。

在 XMLSchema.xsd 文件中对 facets 元素组的定义如下:

xml 复制代码
<xs:group name="facets">
    <xs:choice>
        <xs:element ref="xs:minExclusive"/>
        <xs:element ref="xs:minInclusive"/>
        <xs:element ref="xs:maxExclusive"/>
        <xs:element ref="xs:maxInclusive"/>
        
        <xs:element ref="xs:totalDigits"/>
        <xs:element ref="xs:fractionDigits"/>
        <xs:element ref="xs:length"/>
        <xs:element ref="xs:minLength"/>
        <xs:element ref="xs:maxLength"/>
        
        <xs:element ref="xs:enumeration"/>
        <xs:element ref="xs:pattern"/>

        <xs:element ref="xs:whiteSpace"/>
    </xs:choice>
</xs:group>
限定 描述
minExclusive 定义数值或者日期的下限。所允许的值必需大于此值。等同于左开区间。
minInclusive 定义数值或者日期的下限。所允许的值必需大于或等于此值。等同于左闭区间。
maxExclusive 定义数值或者日期的上限。所允许的值必须小于此值。等同于右开区间。
maxInclusive 定义数值或者日期的上限。所允许的值必须小于或等于此值。等同于右闭区间。
totalDigits 定义所允许的十进制数的精确位数。必须大于0。
fractionDigits 定义所允许的十进制数的最大的小数位数。必须大于或等于0。
length 定义所允许的字符或者列表项的精确数目。必须大于或等于0。
minLength 定义所允许的字符或者列表项的最小数目。必须大于或等于0。
maxLength 定义所允许的字符或者列表项的最大数目。必须大于或等于0。
enumeration 定义可接受值的一个枚举列表。
pattern 定义可接受的字符的正则表达式。
whiteSpace 定义空白字符(换行、回车、空格以及制表符)的处理方式。

4.3.1、有序限定 4 种

4.3.1.1、左开区间 - minExclusive 元素
xml 复制代码
<minExclusive
fixed = boolean : false
id = ID
value = anySimpleType
{any attributes with non-schema namespace . . .}>
    Content: (annotation?)
</minExclusive>

[Definition]
minExclusive is the exclusive lower bound of the value space for a datatype with the ordered property. The value of minExclusive must be in the value space of the base type or be equal to {value} in {base type definition}.

minExclusive 是具有有序 属性的数据类型的值空间左开区间minExclusive 的值必须在基本类型的值空间中。

[Example]
The following is the definition of a user-derived datatype which limits values to integers greater than or equal to 100, using minExclusive.

如下示例定义了一个从 integer 类型派生的简单类型 more-than-ninety-nine,通过 minExclusive 限定了大于或等于 100 的整数值。

xml 复制代码
<simpleType name="more-than-ninety-nine">
    <restriction base="integer">
        <minExclusive value="99"/>
    </restriction>
</simpleType>

  • 同时定义 minInclusiveminExclusive 是不合法的
  • 同时定义 maxInclusivemaxExclusive 是不合法的
  • minExclusive >= maxInclusiveminExclusive > maxExclusive 是不合法的
  • minInclusive >= maxExclusiveminInclusive > maxInclusive 是不合法的
4.3.1.2、左闭区间 - minInclusive 元素
4.3.1.3、右开区间 - maxExclusive 元素
4.3.1.4、右闭区间 - maxInclusive 元素

4.3.2、数值限定 5 种

4.3.2.1、总位数 - totalDigits 元素
xml 复制代码
<totalDigits
fixed = boolean : false
id = ID
value = positiveInteger
{any attributes with non-schema namespace . . .}>
    Content: (annotation?)
</totalDigits>

[Definition:] totalDigits controls the maximum number of values in the ·value space· of datatypes ·derived· from decimal, by restricting it to numbers that are expressible as i × 10^-n where i and n are integers such that |i| < 10^totalDigits and 0 <= n <= totalDigits. The value of totalDigits ·must· be a positiveInteger.

totalDigits 通过表示形式 i×10^n 的数字来限制从十进制派生的数据类型的值空间中的最大值,i 和 n 是整数,因此 |i|<10^totalDigets0<=n<=totalDigitstotalDigits 的值必须是正整数。

4.3.2.2、小数位数 - fractionDigits 元素
xml 复制代码
<fractionDigits
fixed = boolean : false
id = ID
value = nonNegativeInteger
{any attributes with non-schema namespace . . .}>
    Content: (annotation?)
</fractionDigits>

[Definition]
fractionDigits controls the size of the minimum difference between values in the value space of datatypes derived from decimal, by restricting the value space to numbers that are expressible as i × 10^-n where i and n are integers and 0 <= n <= fractionDigits. The value of fractionDigits must be a nonNegativeInteger.

[Example]
The following is the definition of a user-derived datatype which could be used to represent the magnitude of a person's body temperature on the Celsius scale. This definition would appear in a schema authored by an "end-user" and shows how to define a datatype by specifying facet values which constrain the range of the base type.

如下示例定义一个从 decimal 派生的简单类型 celsiusBodyTemp,表示人的摄氏度体温,totalDigits 的值是 4,小数点也算位数?

xml 复制代码
<simpleType name='celsiusBodyTemp'>
    <restriction base='decimal'>
        <totalDigits value='4'/>
        <fractionDigits value='1'/>
        <minInclusive value='36.4'/>
        <maxInclusive value='40.5'/>
    </restriction>
</simpleType>
4.3.2.3、长度 - length 元素
4.3.2.4、最小长度 - minLength 元素
xml 复制代码
<minLength
fixed = boolean : false
id = ID
value = nonNegativeInteger
{any attributes with non-schema namespace . . .}>
    (annotation?)
</minLength>
4.3.2.5、最大长度 - maxLength 元素

4.3.3、非固定值限定 2 种

4.3.3.1、枚举值限定 - enumeration 元素
4.3.3.2、字符串匹配模式 - pattern 元素
xml 复制代码
<pattern
id = ID
value = string
{any attributes with non-schema namespace . . .}>
    Content: (annotation?)
</pattern>

[Definition]
pattern is a constraint on the value space of a datatype which is achieved by constraining the lexical space to literals which match a specific pattern. The value of pattern must be a regular expression.

pattern 元素是对数据类型的值空间 的约束,通过将词汇空间 约束为与特定模式匹配的字面量来实现。pattern 的值必须是正则表达式

[Example]
The following is the definition of a user-derived datatype which is a better representation of postal codes in the United States, by limiting strings to those which are matched by a specific regular expression.

如下示例定义了一个从 string 类型派生的简单类型 better-us-zipcode,用来表示美国的邮政编码,并通过正则表达式限制了字符串的取值范围。

xml 复制代码
<simpleType name='better-us-zipcode'>
    <restriction base='string'>
        <pattern value='[0-9]{5}(-[0-9]{4})?'/>
    </restriction>
</simpleType>

4.3.4、空白字符限定 1 种

4.3.4.1、空白字符处理 - whiteSpace 元素
xml 复制代码
<xs:element name="whiteSpace" id="whiteSpace">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#element-whiteSpace"/>
    </xs:annotation>

    <xs:complexType>
        <xs:complexContent>
            <xs:restriction base="xs:facet">
                <xs:sequence>
                    <xs:element ref="xs:annotation" minOccurs="0"/>
                </xs:sequence>

                <xs:attribute name="value" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:NMTOKEN">
                            <xs:enumeration value="preserve"/>
                            <xs:enumeration value="replace"/>
                            <xs:enumeration value="collapse"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>

                <xs:anyAttribute namespace="##other" processContents="lax"/>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

[Definition]
whiteSpace constrains the value space of types derived from string such that the various behaviors specified in Attribute Value Normalization in [XML 1.0 (Second Edition)] are realized. The value of whiteSpace must be one of {preserve, replace, collapse}.

whiteSpace 限制 string 派生类型的值空间,用来规范化属性值,whiteSpace 的 value 属性是必须的,且必须是 preserve, replace, collapse 其中之一

  • preserve:XML 处理器不会对空白字符做任何处理
    No normalization is done, the value is not changed (this is the behavior required by [XML 1.0 (Second Edition)] for element content).

  • replace:XML 处理器会使用空格替换所有空白字符(制表符、换行以及回车
    All occurrences of #x9 (tab), #xA (line feed) and #xD (carriage return) are replaced with #x20 (space).

  • collapse:XML 处理器会使用空格替换所有空白字符(制表符、换行以及回车),多个连续空格会被缩减为一个空格,开头和结尾的空格会被移除
    After the processing implied by replace, contiguous sequences of #x20's are collapsed to a single #x20, and leading and trailing #x20's are removed.

例如 XSD 中 token 简单类型的定义:

xml 复制代码
<xs:simpleType name="token" id="token">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-2/#token"/>
    </xs:annotation>
    <xs:restriction base="xs:normalizedString">
        <xs:whiteSpace value="collapse" id="token.whiteSpace"/>
    </xs:restriction>
</xs:simpleType>


5、XSD 复合类型

complexType 类型定义复合类型。复合类型的元素是 包含其他元素属性 的 XML 元素。

xml 复制代码
<xs:complexType name="complexType" abstract="true">
    <xs:complexContent>
        <xs:extension base="xs:annotated">
            <xs:group ref="xs:complexTypeModel"/>
            <xs:attribute name="name" type="xs:NCName"/>
            <xs:attribute name="mixed" type="xs:boolean" use="optional" default="false"/>
            <xs:attribute name="abstract" type="xs:boolean" use="optional" default="false"/>
            <xs:attribute name="final" type="xs:derivationSet"/>
            <xs:attribute name="block" type="xs:derivationSet"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:element name="complexType" type="xs:topLevelComplexType" id="complexType">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-complexType"/>
    </xs:annotation>
</xs:element>
  • mixed

    可选。规定是否允许字符数据出现在该复杂类型的子元素之间。默认值为 false。

    如果子元素是 simpleContent 元素,则不允许 mixed 属性。

    如果子元素是 complexContent 元素,则 mixed 属性可以被 complexContent 元素的 mixed 属性重写。

  • abstract

    可选。规定在实例文档中是否可以直接使用该复合类型,类似 Java 中的 abstract 关键字。如果值为 true,则元素不能直接使用该复合类型,而是必须使用从该复合类型派生的复合类型。默认值为 false。

  • final

    可选。防止从该 complexType 元素指定的类型派生出新的类型,类似 Java 中的 final 关键字。该值可以包含:

    1. #all - 防止所有派生(扩展和限制)
    2. extension - 防止通过扩展派生
    3. restriction - 防止通过限制派生
  • block


复合类型指示器

Occurrence 指示器 - occurs 属性组

minOccurs 属性
xml 复制代码
<xs:attribute name="minOccurs" 
type="xs:nonNegativeInteger" 
use="optional" 
default="1"/>

minOccurs 指示器规定某个元素能够出现的最小次数。

maxOccurs 属性
xml 复制代码
<xs:attribute name="maxOccurs" 
type="xs:allNNI" 
use="optional" 
default="1"/>

maxOccurs 指示器规定某个元素能够出现的最大次数,如果某个元素的出现次数不受限制,使用 maxOccurs="unbounded"

例如规定 class 元素可以有多个 student 子元素而只能有 1-2 个 teacher 子元素:

xml 复制代码
<xs:element name="class">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="student" type="xs:string"
                        maxOccurs="unbounded"/>
            <xs:element name="teacher" type="xs:string"
                        minOccurs="1" maxOccurs="2"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Order 指示器 - explicitGroup 复合类型

choice 元素和 sequence 元素

choice 元素规定仅允许 <xs:choice> 声明的元素之一出现。
sequence 元素规定 <xs:sequence> 声明的元素以指定的顺序出现,每个子元素可以出现 0 次到任意次数。

xml 复制代码
<xs:element name="choice" type="xs:explicitGroup" id="choice">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-choice"/>
    </xs:annotation>
</xs:element>

<xs:element name="sequence" type="xs:explicitGroup" id="sequence">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-sequence"/>
    </xs:annotation>
</xs:element>

choice 和 sequence 元素是 explicitGroup 类型。

  • explicitGroup 类型不支持 name 和 ref 属性
  • explicitGroup 类型引用了 nestedParticle 元素组,nestedParticle 元素组提供 element、group、choice、sequence、any 等元素其中之一
  • explicitGroup 类型继承 group 类型
  • group 类型引用了 defRef 和 occurs 属性组,defRef 属性组提供 name 和 ref 属性,occurs 属性组提供可选的 minOccurs 和 maxOccurs 属性
  • group 类型继承 annotated 类型
  • annotated 类型提供了 annotation 子元素和 id 属性

综上所述 choice 和 sequence 元素的定义如下:

xml 复制代码
<choice
id=ID
minOccurs=nonNegativeInteger
maxOccurs=nonNegativeInteger|unbounded
any attributes
>
    (annotation?,(element|group|choice|sequence|any)*)
</choice>

<sequence
id=ID
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
any attributes
>
    (annotation?,(element|group|choice|sequence|any)*)
</sequence>
all 元素

all 元素规定子元素能够以任意顺序出现,每个子元素可以出现零次或一次。

xml 复制代码
<xs:complexType name="all">
    <xs:complexContent>
        <xs:restriction base="xs:explicitGroup">
            <xs:group ref="xs:allModel"/>
            <xs:attribute name="minOccurs" use="optional" default="1"/>
            <xs:attribute name="maxOccurs" use="optional" default="1"/>
            <xs:anyAttribute namespace="##other" processContents="lax"/>
        </xs:restriction>
    </xs:complexContent>
</xs:complexType>

<xs:element name="all" id="all" type="xs:all">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-all"/>
    </xs:annotation>
</xs:element>

all 元素是 all 类型。

  • all 类型继承 explicitGroup 类型
  • all 类型引用了 allModel 元素组
  • all 类型提供可选的 minOccurs 和 maxOccurs 属性

综上所述 all 元素的定义如下:

xml 复制代码
<all
id=ID
minOccurs=0|1
maxOccurs=1
any attributes
>
    (annotation?,element*)
</all>

例如规定 person 元素中 firstnamelastname 子元素能够以任意顺序出现零次或一次:

xml 复制代码
<xs:element name="person">
    <xs:complexType>
        <xs:all minOccurs="0">
            <xs:element name="firstname" type="xs:string"/>
            <xs:element name="lastname" type="xs:string"/>
        </xs:all>
    </xs:complexType>
</xs:element>

Group 指示器

元素组 - realGroup 复合类型

group 元素用于对元素声明进行组合,这样这些声明就能够以组合的形式合并到复合类型中。

xml 复制代码
<group
id=ID
name=NCName
ref=QName
minOccurs=nonNegativeInteger
maxOccurs=nonNegativeInteger|unbounded
any attributes
>
    (annotation?,(all|choice|sequence)?)
</group>

例如声明一个元素组 attrDecls,然后声明一个复合类型 attributeGroup 并引用 attrDecls 元素组:

xml 复制代码
<xs:group name="attrDecls">
    <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="attribute" type="xs:attribute"/>
            <xs:element name="attributeGroup" type="xs:attributeGroupRef"/>
        </xs:choice>
        <xs:element ref="xs:anyAttribute" minOccurs="0"/>
    </xs:sequence>
</xs:group>

<xs:complexType name="attributeGroup" abstract="true">
    <xs:complexContent>
        <xs:extension base="xs:annotated">
            <xs:group ref="xs:attrDecls"/>
            <xs:attributeGroup ref="xs:defRef"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
属性组 - attributeGroup 复合类型

attributeGroup 类型是抽象(abstract="true")类型,从 annotated 类型扩展派生,新增了 attrDecls 元素组和 defRef 属性组。

xml 复制代码
<xs:complexType name="attributeGroup" abstract="true">
    <xs:complexContent>
        <xs:extension base="xs:annotated">
            <xs:group ref="xs:attrDecls"/>
            <xs:attributeGroup ref="xs:defRef"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

例如声明一个属性组 defRef,包含 name 和 ref 属性,然后在复合类型 customer 中引用:

xml 复制代码
<xs:attributeGroup name="defRef">
    <xs:annotation>
        <xs:documentation>
            for element, group and attributeGroup, which both define and reference
        </xs:documentation>
    </xs:annotation>
    <xs:attribute name="name" type="xs:NCName"/>
    <xs:attribute name="ref" type="xs:QName"/>
</xs:attributeGroup>

attributeGroup 元素用于对属性声明进行组合,这样这些声明就能够以组合的形式合并到复合类型中。

xml 复制代码
<attributeGroup
id=ID
name=NCName
ref=QName
any attributes
>
    (annotation?),((attribute|attributeGroup)*,anyAttribute?))
</attributeGroup>

元素类型 - element 类型

xml 复制代码
<xs:complexType name="element" abstract="true">
    <xs:annotation>
        <xs:documentation>
            The element element can be used 
            either at the top level to define an element-type binding globally, 
            or within a content model to 
            either reference a globally-defined element 
            or type or declare an element-type binding locally. 
            The ref form is not allowed at the top level.
        </xs:documentation>
    </xs:annotation>
    <xs:complexContent>
        <xs:extension base="xs:annotated">
            <xs:sequence>
                <xs:choice minOccurs="0">
                    <xs:element name="simpleType" type="xs:localSimpleType"/>
                    <xs:element name="complexType" type="xs:localComplexType"/>
                </xs:choice>
                <xs:group ref="xs:identityConstraint" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attributeGroup ref="xs:defRef"/>
            <xs:attribute name="type" type="xs:QName"/>
            <xs:attribute name="substitutionGroup" type="xs:QName"/>
            <xs:attributeGroup ref="xs:occurs"/>
            <xs:attribute name="default" type="xs:string"/>
            <xs:attribute name="fixed" type="xs:string"/>
            <xs:attribute name="nillable" type="xs:boolean" use="optional" default="false"/>
            <xs:attribute name="abstract" type="xs:boolean" use="optional" default="false"/>
            <xs:attribute name="final" type="xs:derivationSet"/>
            <xs:attribute name="block" type="xs:blockSet"/>
            <xs:attribute name="form" type="xs:formChoice"/>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

复合元素

复合元素指包含其他元素或属性的 XML 元素。

xml 复制代码
<xs:element name="employee" type="fullpersoninfo"/>

<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="fullpersoninfo">
  <xs:complexContent>
    <xs:extension base="personinfo">
      <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

通配符类型 - wildcard 复合类型

xml 复制代码
<xs:complexType name="wildcard">
    <xs:complexContent>
        <xs:extension base="xs:annotated">
            <xs:attribute name="namespace" type="xs:namespaceList" use="optional" default="##any"/>
            <xs:attribute name="processContents" use="optional" default="strict">
                <xs:simpleType>
                    <xs:restriction base="xs:NMTOKEN">
                        <xs:enumeration value="skip"/>
                        <xs:enumeration value="lax"/>
                        <xs:enumeration value="strict"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
  • namespace

    可选。规定包含可以使用的元素的命名空间。如果没有指定命名空间,则默认值是 ##any。如果指定命名空间,则取值必须是以下之一:

    1. ##any - 来自任何命名空间的元素都可以出现
    2. ##other - 来自该元素的父元素的目标命名空间之外的任何命名空间的元素都可以出现
    3. ##local - 未由命名空间限定的元素可以出现
    4. ##targetNamespace - 来自包含该元素的父元素的目标命名空间的元素可以出现
    5. {URI references of namespaces, ##targetNamespace, ##local} 的列表 - 来自通过空格分隔的命名空间列表的元素可以出现。 该列表可以包含以下内容: 命名空间 ##targetNamespace##local 的 URI 引用。
  • processContents

    可选。一个指示符,指示应用程序或 XML 处理器应如何根据由该 any 元素指定的元素处理 XML 文档的验证。 如果没有指定 processContents 属性,则默认为 strict。如果指定了 processContents,则取值必须是以下之一:

    1. strict - XML 处理器必须获得所需命名空间的架构,并且必须验证来自这些命名空间的所有元素
    2. lax - 与 strict 相同;但是,即使不能获取该架构,也不会发生任何错误
    3. skip - XML 处理器不尝试验证来自指定命名空间的所有元素

任何元素 - any 元素

any 元素的类型是从 wildcard 类型扩展的派生类型。

wildcard 类型的 namespace 属性使来自指定命名空间的任意元素可以包含在元素中。

any 元素使 XML 文档的创作者可以通过未被 schema 规定的元素来扩展 XML 文档。

xml 复制代码
<xs:element name="any" id="any">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-any"/>
    </xs:annotation>
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="xs:wildcard">
                <xs:attributeGroup ref="xs:occurs"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

<any
id=ID
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
namespace=namespace
processContents=lax|skip|strict
any attributes
>
    (annotation?)
</any>

任何属性 - anyAttribute 元素

xml 复制代码
<xs:element name="anyAttribute" type="xs:wildcard" id="anyAttribute">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-anyAttribute"/>
    </xs:annotation>
</xs:element>

anyAttribute 元素使 XML 文档的创作者可以通过未被 schema 规定的属性来扩展 XML 文档。


注释 - annotation 元素

annotation 元素是一个顶层元素,表示 schema 的注释。

xml 复制代码
<annotation
id=ID
any attributes
>
    (appinfo|documentation)*
</annotation>

annotation 元素包含可选的 appinfo 元素(由应用程序使用的信息)和 documentation 元素(由用户读取或使用的注释或文本)。

xml 复制代码
<appInfo
source=anyURL
>
    Any well-formed XML content
</appInfo>

<documentation
source=URI reference
xml:lang=language
>
    Any well-formed XML content
</documentation>
  • appInfodocumentation 元素的 source 属性是 anyURI 基本类型,指定 appInfodocument 元素内容的来源。

  • documentation 元素的 xml:lang 属性是 language 类型,指定 document 元素内容使用的语言。

xml 复制代码
<xs:element name="annotation" id="annotation">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-annotation"/>
    </xs:annotation>
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="xs:openAttrs">
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="xs:appinfo"/>
                    <xs:element ref="xs:documentation"/>
                </xs:choice>
                <xs:attribute name="id" type="xs:ID"/>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

<xs:element name="appinfo" id="appinfo">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-appinfo"/>
    </xs:annotation>
    <xs:complexType mixed="true">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:any processContents="lax"/>
        </xs:sequence>
        <xs:attribute name="source" type="xs:anyURI"/>
        <xs:anyAttribute namespace="##other" processContents="lax"/>
    </xs:complexType>
</xs:element>

<xs:element name="documentation" id="documentation">
    <xs:annotation>
        <xs:documentation source="http://www.w3.org/TR/xmlschema-1/#element-documentation"/>
    </xs:annotation>
    <xs:complexType mixed="true">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:any processContents="lax"/>
        </xs:sequence>
        <xs:attribute name="source" type="xs:anyURI"/>
        <xs:attribute ref="xml:lang"/>
        <xs:anyAttribute namespace="##other" processContents="lax"/>
    </xs:complexType>
</xs:element>
相关推荐
readmancynn3 天前
XML_Tomcat_HTTP
xml·http·tomcat
我是全栈架构师3 天前
Dom4j使用xpath查询xml文
xml
cyt涛3 天前
MyBatis之手动映射
xml·数据库·mybatis·查询·resultmap·手动映射
齐 飞3 天前
使用jackson将xml和对象、List相互转换
xml·java·spring boot·后端·list
代码代码快快显灵3 天前
XML标记语言
xml·java·数据库
Dingdangr3 天前
AndroidManifest.xml文件的重要信息
xml
OEC小胖胖4 天前
MyBatis 如何将 Mapper 接口与其 XML 映射文件关联:深入原理与实现
xml·java·后端·mybatis·web
_Shirley4 天前
android.view.InflateException: Binary XML file line #7: Error inflating class
android·xml·java·ide·kotlin·android studio
陈逸轩*^_^*4 天前
xml重点笔记(尚学堂 3h)
xml·java·笔记·java-ee·intellij-idea
codelife3214 天前
记录word转xml文件踩坑
xml·word