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

dataType

2.1、基本数据类型 19 种

2.1.1、基本字符串类型 1 种

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

2.1.2、基本数值类型 3 种

数据类型派生类型限制描述
xs:decimalanySimpleType 类型whiteSpace="collapse"、fixed="true"十进制数。最大位数是 18 位。例如 -999.52
xs:floatanySimpleType 类型whiteSpace="collapse"、fixed="true"32 位单精度浮点数
xs:doubleanySimpleType 类型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:dateanySimpleType 类型whiteSpace="collapse"、fixed="true"YYYY-MM-DD
xs:timeanySimpleType 类型whiteSpace="collapse"、fixed="true"hh:mm:ss
xs:dateTimeanySimpleType 类型whiteSpace="collapse"、fixed="true"YYYY-MM-DDThh:mm:ss,T 表示时间部分的起始
xs:gYearanySimpleType 类型whiteSpace="collapse"、fixed="true"定义日期时间的一个部分 - 年 (YYYY)
xs:gYearMonthanySimpleType 类型whiteSpace="collapse"、fixed="true"定义日期时间的一个部分 - 年和月 (YYYY-MM)
xs:gMonthanySimpleType 类型whiteSpace="collapse"、fixed="true"定义日期时间的一个部分 - 月 (MM)
xs:gMonthDayanySimpleType 类型whiteSpace="collapse"、fixed="true"定义日期时间的一个部分 - 月和天 (MM-DD)
xs:gDayanySimpleType 类型whiteSpace="collapse"、fixed="true"定义日期时间的一个部分 - 天 (DD)
xs:durationanySimpleType 类型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 简单类型
<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:booleananySimpleType 类型whiteSpace="collapse"、fixed="true"true 或 false
2.1.4.1、布尔 - boolean 简单类型

2.1.5、编码二进制类型 2 种

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

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

数据类型派生类型限制描述
xs:anyURIanySimpleType 类型whiteSpace="collapse"、fixed="true"转义URI
xs:QNameanySimpleType 类型whiteSpace="collapse"、fixed="true"命名空间名称和本地名称组成的限定名
xs:NOTATIONanySimpleType 类型whiteSpace="collapse"、fixed="true"非 XML 格式的数据
2.1.6.1、统一资源标识符 - URI 简单类型
2.1.6.2、限定名 - QName 简单类型
<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 简单类型
<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 限制派生后的类型。

例如:

<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:normalizedStringstring 类型whiteSpace="replace"规范化字符串。XML 处理器会移除制表符、换行以及回车
xs:tokennormalizedString 类型whiteSpace="collapse" 标记。XML 处理器会移除制表符、换行、回车以及开头和结尾的空格,多个连续空格的内部序列会被缩减为一个空格
xs:languagetoken 类型pattern="[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*" 由 RFC3066 规定的合法的语言标识
xs:NMTOKENtoken 类型pattern="\c+",\c 表示NameChar名称标记
xs:NMTOKENSNMTOKEN 集合list itemType="xs:NMTOKEN"、minLength=1 名称标记集合
xs:Nametoken 类型pattern="\i\c*",\i 表示 NameStartChar,\c 表示 NameChar名称
xs:NCNameName 类型pattern="[\i-[:]][\c-[:]]*",\i 表示 NameStartChar,\c 表示NameChar“non-colonized” Name,即用户定义名称
xs:IDNCName 类型唯一标识
xs:IDREFNCName 类型唯一标识引用
xs:IDREFSIDREF 集合list itemType="xs:IDREF"、minLength=1唯一标识引用集合
xs:ENTITIENCName 类型实体
xs:ENTITIESENTITY 集合list itemType="xs:ENTITY"、minLength=1实体集合
2.2.1.1、名称标记 - NMTOKEN 简单类型
<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:

[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 简单类型
<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:integerfractionDigits=0、pattern="[\-+]?[0-9]+" 限制,从 decimal 派生的整数,用于规定无小数部分的数值
xs:nonPositiveIntegermaxInclusive=0 限制,从 integer 派生的非正整数(0 和负整数)
xs:negativeInteger负整数
xs:longminInclusive、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、派生类型

schema

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

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

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

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

<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 复合类型

<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约束,并不会为类型添加新的元素或属性。

<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”:

<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 元素的扩展或限制,简单类型扩展后会变成复合类型

<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 属性:

<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 元素的扩展或限制。

<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. 以及对它们的 约束
<simpleType id=ID name=NCName anyAttribute>
    (annotation?,(restriction|list|union))
</simpleType>

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

<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)。

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

例如声明两个简单元素 nameage

<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 等属性。

<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 元素定义一个属性。

<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 中的两个字母:

<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 元素组的定义如下:

<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定义空白字符(换行、回车、空格以及制表符)的处理方式。

facet

4.3.1、有序限定 4 种

4.3.1.1、左开区间 - minExclusive 元素
<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 的整数值。

<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 元素
<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 元素
<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,小数点也算位数?

<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 元素
<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 元素
<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,用来表示美国的邮政编码,并通过正则表达式限制了字符串的取值范围。

<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 元素
<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 简单类型的定义:

<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 元素。

<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 属性
<xs:attribute name="minOccurs" 
type="xs:nonNegativeInteger" 
use="optional" 
default="1"/>

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

maxOccurs 属性
<xs:attribute name="maxOccurs" 
type="xs:allNNI" 
use="optional" 
default="1"/>

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

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

<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 复合类型

order指示器

choice 元素和 sequence 元素

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

<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 元素的定义如下:

<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 元素规定子元素能够以任意顺序出现,每个子元素可以出现零次或一次。

<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 元素的定义如下:

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

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

<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 元素用于对元素声明进行组合,这样这些声明就能够以组合的形式合并到复合类型中。

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

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

<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 属性组。

<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 中引用:

<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 元素用于对属性声明进行组合,这样这些声明就能够以组合的形式合并到复合类型中。

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

元素类型 - element 类型

<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 元素。

<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 复合类型

<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 文档。

<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 元素

<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 的注释。

<annotation
id=ID
any attributes
>
    (appinfo|documentation)*
</annotation>

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

<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 元素内容使用的语言。

<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>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/67166.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【Spring Boot】构建RESTful服务 — 构建RESTful应用接口

构建RESTful应用接口 RESTful架构是目前最流行的互联网软件架构规范&#xff0c;是Web API&#xff08;应用编程接口&#xff09;的大趋势和主流规范&#xff0c;了解了RESTful的众多优点之后&#xff0c;接下来一步一步地学习如何使用Spring Boot构建RESTful Web API。 1.Sp…

途乐证券-光伏、储能板块拉升 德业股份、固德威等大幅走高

光伏、储能等新能源板块10日盘中震荡上扬&#xff0c;截至发稿&#xff0c;德业股份涨近8%&#xff0c;锦浪科技、固德威、阿特斯等涨逾6%&#xff0c;禾迈股份、昱能科技涨近4%。 消息面上&#xff0c;据中关村储能产业技术联盟计算&#xff0c;2021年至2023年上半年&#xff…

rust关于项目结构包,Crate和mod和目录的组织

rust 最近开始学习rust语言。感觉这门语言相对java确实是难上很多。开几个文章把遇到的问题记录一下 rust关于包&#xff0c;Crate 关于包&#xff0c;Crate这块先看看官方书籍怎么说的 crate 是 Rust 在编译时最小的代码单位。如果你用 rustc 而不是 cargo 来编译一个文件…

Android 内存泄漏

名词解释 内存泄漏:即memory leak。是指内存空间使用完毕后无法被释放的现象&#xff0c;虽然Java有垃圾回收机制&#xff08;GC&#xff09;&#xff0c;但是对于还保持着引用&#xff0c; 该内存不能再被分配使用&#xff0c;逻辑上却已经不会再用到的对象&#xff0c;垃圾回…

安装CUDA与CUDNN与Pytorch(最新超级详细图文版本2023年8月最新)

一、安装CUDA 1.1、下载安装包 cuda可以认为就是Nvidia为了显卡炼丹搞的一个软件&#xff0c;其下载地址为&#xff1a;CUDA Toolkit 12.2 Update 1 Downloads | NVIDIA Developer 当你点进这个链接的时候&#xff0c;你需要依次选择 1是选择系统&#xff0c;这里选windows…

Netty面试题1

计算机网络模型 OSI采用了分层的结构化技术&#xff0c;共分七层&#xff0c; 物理层、数据链路层、网络层、传输层、会话层、表示层、应用层 。 Open System Interconnect 简称OSI&#xff0c;是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参…

医疗保健中的 NLP:实体链接

一、说明 HEalthcare和生命科学行业产生大量数据&#xff0c;这些数据是由合规性和监管要求&#xff0c;记录保存&#xff0c;研究论文等驱动的。但随着数据量的增加&#xff0c;搜索用于研究目的的必要文件和文章以及数据结构成为一个更加复杂和耗时的过程。例如&#xff0c;如…

微信小程序中的分包使用介绍

一、分包的好处 可以优化小程序首次启动的下载时间 在多团队共同开发时可以更好的解耦协作 主包&#xff1a;放置默认启动页面/TabBar 页面&#xff0c;公共资源/JS 脚本 分包&#xff1a;根据开发者的配置进行划分 限制&#xff1a;所有分包大小不超过 20M&#xff0c;单…

无人驾驶实战-第十二课(强化学习自动驾驶系统)(完)

在七月算法上报了《无人驾驶实战》课程&#xff0c;老师讲的真好。好记性不如烂笔头&#xff0c;记录一下学习内容。 课程入口&#xff0c;感兴趣的也可以跟着学一下。 ————————————————————————————————————————— 强化学习&#xff…

php webshell 免杀入门

webshell 查杀软件&#xff1a; d盾、安全狗、护卫神、Sangfor WebShellKill 在线查杀 百度WEBDIR https://scanner.baidu.com 河马 https://www.shellpub.com cloudwalker牧云 https://webshellchop.chaitin.cn 查杀技术 静态检测、动态检测、日志检查 静态检查&#xff1a…

通用FIR滤波器的verilog实现(内有Lowpass、Hilbert参数生成示例)

众所周知&#xff0c;Matlab 中的 Filter Designer 可以直接生成 FIR 滤波器的 verilog 代码&#xff0c;可以方便地生成指定阶数、指定滤波器参数的高通、低通、带通滤波器&#xff0c;生成的 verilog 代码也可以指定输入输出信号的类型和位宽。然而其生成的代码实在算不上美观…

智能型静电消除器的优势有哪些?

智能型静电消除器是一种使用先进技术和智能控制系统来消除静电问题的设备。静电是由于电荷不平衡而引起的现象&#xff0c;常见于工业生产、医疗设备、办公环境等场合。静电的存在可能导致电子设备故障、火灾、等问题。 智能型静电消除器与传统静电消除器相比&#xff0c;具有以…

Python做一个绘图系统3:从文本文件导入数据并绘图

文章目录 导入数据文件对话框修改绘图逻辑源代码 Python绘图系统系列&#xff1a;将matplotlib嵌入到tkinter 简单的绘图系统 导入数据 单纯从作图的角度来说&#xff0c;更多情况是已经有了一组数据&#xff0c;然后需要将其绘制。这组数据可能是txt格式的&#xff0c;也可能…

uni-app:实现点击按钮,进行数据累加展示(解决数据过多,导致出错)

效果 代码 核心代码 一、标签显示 <!-- 加载更多 --> <view class"load_more" v-if"info.length > pageNum * pageSize" tap"loadMore">加载更多 </view> v-if"info.length > pageNum * pageSize"&#xf…

远景智能PMO负责人严晓婷受邀为第十二届中国PMO大会演讲嘉宾

上海远景科创智能科技有限公司PMO负责人严晓婷女士受邀为由PMO评论主办的2023第十二届中国PMO大会演讲嘉宾&#xff0c;演讲议题&#xff1a;能源物联网产品标准项目和非标准项目的并行管理。大会将于8月12-13日在北京举办&#xff0c;敬请关注&#xff01; 议题简要&#xff1…

教雅川学缠论07-中枢实战众泰汽车000980

本文实战众泰汽车 下面是2023年11月14-2023年8月8众泰汽车日K图 先画日K 接下来处理包含&#xff0c;就变成下面这个样子 下面在套上缠论的理论&#xff0c;未来股价的走势应该是红色椭圆形虚线里面的样子 好了&#xff0c;文章就到这里&#xff0c;如果众泰最终不是这个走势…

网页版Java(Spring/Spring Boot/Spring MVC)五子棋项目(四)对战模块

网页版Java&#xff08;Spring/Spring Boot/Spring MVC&#xff09;五子棋项目&#xff08;四&#xff09;对战模块 一、约定前后端交互接口1. 建立连接接口2. 针对落子的请求和响应 二、实现前端页面三、实现后端1. 当用户进入房间&#xff0c;更新用户状态 OnlineUserManager…

W5500-EVB-PICO作为TCP Client 进行数据回环测试(五)

前言 上一章我们用W5500-EVB-PICO开发板通过DNS解析www.baidu.com&#xff08;百度域名&#xff09;成功得到其IP地址&#xff0c;那么本章我们将用我们的开发板作为客户端去连接服务器&#xff0c;并做数据回环测试&#xff1a;收到服务器发送的数据&#xff0c;并回传给服务器…

FFmpeg 编码详细流程

介绍 FFmpeg的 libavcodec 模块完成音视频多媒体的编解码模块。FFmpeg 本身不具有音视频编码的功能和底层能力&#xff0c;只是对各类第三方的编码器API 进行封装调用。老版本的 FFmpeg 将avcodec_encode_video2()作为视频的解码函数 API&#xff0c;将avcodec_encode_audio2(…
最新文章