XML 学习笔记 7:XSD

news2024/9/21 20:35:34

本文章内容参考自:
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、限制派生类型 - restriction 元素
      • 3.3、简单类型的扩展和限制 - simpleContent 元素
      • 3.4、复合类型的扩展和限制 - complexContent 元素
      • 3.5、集合派生类型
    • 4、XSD 简单类型 - simpleType 元素
      • 4.1、简单元素 - element 元素
      • 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 复合类型 - complexType 类型和 complexType 元素
      • 复合类型指示器
        • Occurrence 指示器 - occurs 属性组
          • minOccurs 属性
          • maxOccurs 属性
        • Order 指示器
          • all 元素
          • choice 元素
          • sequence 元素
        • Group 指示器
          • 元素组 - group 元素
          • 属性组 - attributeGroup 类型
      • 复合元素 - element 元素
    • 通配符类型 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:documentation>
            NOTATION cannot be used directly in a schema;
            rather a type must be derived from it 
            by specifying at least one enumeration facet 
            whose value is the name of a NOTATION declared in the schema.
        </xs:documentation>
    </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、限制派生类型 - restriction 元素

类型限制即 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 元素

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

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

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、简单元素 - element 元素

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

不过,“仅包含文本内容” 这个限定却很容易造成误解。文本内容有很多种类型,它可以是 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 类型和 complexType 元素

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 指示器

all 元素

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

choice 元素规定仅允许 <xs:choice> 声明的元素之一出现。

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

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

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

Group 指示器

元素组 - group 元素

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

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

例如声明一个元素组 customergroup,元素组中声明 sequence 元素,规定 agebirthday 元素必须依次出现,然后声明一个复合类型 customerinfo 并引用 customergroup 元素组:

<xs:group name="customergroup">
    <xs:sequence>
        <xs:element name="age" type="xs:integer"/>
        <xs:element name="birthday" type="xs:date"/>
    </xs:sequence>
</xs:group>

<xs:complexType name="customerinfo">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="country" type="xs:string"/>
        <xs:group ref="customergroup"/>
  </xs:sequence>
</xs:complexType>

<xs:element name="customer" type="customerinfo"/>
属性组 - 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>

复合元素 - element 元素

复合元素指包含其他元素或属性的 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,使来自指定命名空间的任何元素可以显示在包含 sequencechoice 元素中。

该元素使创作者可以通过未被 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 元素使创作者可以通过未被 schema 规定的属性来扩展 XML 文档。


注释 - annotation 元素

annotation 元素是一个顶层元素,规定 schema 的注释。

<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>
<annotation
id=ID
any attributes
>
    (appinfo|documentation)*
</annotation>

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

appInfo 元素规定 annotation 元素中应用程序要使用的信息。该元素必须位于 annotation 中。

注释:应用程序使用在 appinfo 元素内提供的信息处理指令。

<appInfo
source=anyURL
>
Any well-formed XML content
</appInfo>

source 可选。一个 URI 引用,规定应用程序信息的来源。

documentation 元素在 schema 中输入文本注释。该元素必须位于 annotation 元素内。

source 可选。规定应用程序信息的来源。
xml:lang 可选。规定内容中使用的语言。

<xs:annotation>
    <xs:appInfo>
        W3School Note
    </xs:appInfo>
    <xs:documentation xml:lang="en">
        This Schema defines a W3School note!
    </xs:documentation>
</xs:annotation>

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

从0到1自学网络安全(黑客)【附学习路线图+配套搭建资源】

前言 网络安全产业就像一个江湖&#xff0c;各色人等聚集。相对于欧美国家基础扎实&#xff08;懂加密、会防护、能挖洞、擅工程&#xff09;的众多名门正派&#xff0c;我国的人才更多的属于旁门左道&#xff08;很多白帽子可能会不服气&#xff09;&#xff0c;因此在未来的…

【STM32】小电流FOC驱控一体板(开源)

FOC驱控一体板 主控芯片stm32f103c8t6 驱动芯片drv8313 三相电流采样 根据B站一个UP主的改的&#xff08;【【自制】年轻人的第一块FOC驱动器】&#xff09;&#xff0c;大多数元器件是0805&#xff0c;实验室具备且便于自己动手焊接 。 晶振用的是无源晶振&#xff0c;体…

Bias Tee理论到实践

Bias Tees是将直流电压施加到必须传递RF/微波信号的任何组件的必不可少的组件&#xff0c;最常见的是需要DC电源的RF放大器。 对于窄带应用&#xff0c;bias tee设计和结构是简单的&#xff0c;只需要注意组件的自谐振频率。 然而&#xff0c;对于宽带应用&#xff0c;bias t…

基于瑞萨RA6M5的环境监测系统设计

基于瑞萨RA6M5的环境监测系统设计 1. 设计简介 本项目初步设计思路是打算以瑞萨单片机作为控制和数据处理的单元&#xff0c;使用温湿度&#xff0c;光照传感器去监测周围的环境参数&#xff0c;在屏幕上完成传感器数据和相关信息的显示。同时&#xff0c;使用WIFI无线模组与…

用于大型图像模型的 CNN 内核的最新内容

一、说明 由于OpenAI的ChatGPT的巨大成功引发了大语言模型的繁荣&#xff0c;许多人预见到大图像模型的下一个突破。在这个领域&#xff0c;可以提示视觉模型分析甚至生成图像和视频&#xff0c;其方式类似于我们目前提示 ChatGPT 的方式。 用于大型图像模型的最新深度学习方法…

Pytohn将matplotlib嵌入到tkinter中

文章目录 matplotlib窗口组成tkinter布局嵌入图像 matplotlib窗口组成 tkinter是Python标准库中自带的GUI工具&#xff0c;使用十分方便&#xff0c;如能将matplotlib嵌入到tkinter中&#xff0c;就可以做出相对专业的数据展示系统&#xff0c;很有竞争力。 在具体实现之前&a…

FTP使用教程

FTP使用教程 目录 一&#xff0e;FTP简介二&#xff0e;FTP搭建三&#xff0e;FTP使用 一&#xff0e;FTP简介 FTP中文为文件传输协议&#xff0c;简称为文传协议。它也是一个应用程序&#xff0c;不同的操作系统有不同的FTP应用程序&#xff0c;这些应用程序都遵守同一种协议以…

六寸相纸打印拼图 - opencv

准备自己打印一些照片&#xff0c;三寸相纸性价比低&#xff0c;只好买六寸来拼四张然后裁剪&#xff0c;不过并没有搜到提供了这个功能的工具&#xff0c;想想代码应该很简单&#xff0c;所以就造轮子了。可能其实有但是我没搜到。 轮子在这里&#xff1a; https://github.co…

ArraySetter

简介​ 用来展示属性类型为数组的 setter 展示​ 配置示例​ "setter": {"componentName": "ArraySetter","props": {"itemSetter": {"componentName": "ObjectSetter","props": {"c…

React 论文《ReAct: Synergizing Reasoning and Acting in Language Models》阅读笔记

文章目录 1. 简介论文摘要翻译动机和主要贡献 2. REACT : SYNERGIZING *RE*ASONING *ACT*ING3. KNOWLEDGE-INTENSIVE REASONING TASKS3.1 设置3.2 方法3.3 结果和观察 4. 决策任务5. 参考资料 1. 简介 论文摘要翻译 虽然大型语言模型&#xff08;LLM&#xff09;在自然语言理…

医疗实施-集成平台下门诊就诊流程详解

目录 集成平台下门诊就诊流程详解1.患者建档2. 门诊挂号3. 医生就诊4.处方开立5.费用收取、6、科室执行医嘱集成平台下门诊就诊流程详解 这篇文章是考虑医院使用了集成平台之后,门诊就诊流程详解。与我的文章《医疗实施-门诊就诊流程详解》的大致一样,供学有余力的人阅读。 …

图解java.util.concurrent并发包源码系列——深入理解ReentrantLock,看完可以吊打面试官

图解java.util.concurrent并发包源码系列——深入理解ReentrantLock&#xff0c;看完可以吊打面试官 ReentrantLock是什么&#xff0c;有什么作用ReentrantLock的使用ReentrantLock源码解析ReentrantLock#lock方法FairSync#tryAcquire方法NonfairSync#tryAcquire方法 Reentrant…

SpringBoot笔记:SpringBoot 集成 Dataway

文章目录 1、什么是 Dataway?2、主打场景3、技术架构4、整合SpringBoot4.1、maven 依赖4.2、初始化脚本4.3、整合 SpringBoot 5、Dataway 接口管理6、Mybatis 语法支持7、小结 1、什么是 Dataway? 官网地址&#xff1a;https://www.hasor.net/docs/guides/quickstart Da…

连通块是什么

刷题的时候遇到一个名词概念&#xff0c;连通块是什么&#xff1f; 在图论中&#xff0c;无向图中的连通块&#xff08;也叫作连通分量&#xff09;是指原图的一个子图&#xff08;即该子图只包含原图中的部分或全部顶点及边&#xff09;&#xff0c;该子图任意两个顶点都能通…

Swift 环境搭建

Swift是一门开源的编程语言&#xff0c;该语言用于开发OS X和iOS应用程序。 在正式开发应用程序前&#xff0c;我们需要搭建Swift开发环境&#xff0c;以便更好友好的使用各种开发工具和语言进行快速应用开发。由于Swift开发环境需要在OS X系统中运行&#xff0c;因此其环境的…

智能指针shared_ptr:自定义删除器

重点&#xff1a; 1.普通指针转化成智能指针。 2.智能指针创建的时候&#xff0c;第二个参数是自定义删除器&#xff0c;默认情况下&#xff0c;shared_ptr调用delete()函数。 class A { public:void Get() { cout << b << endl; }; private:int b{ 10 }; };clas…

Java 与其他编程语言:比较分析

Java 擅长可移植性和可靠性&#xff0c;Python 擅长通用性和简单性&#xff0c;JavaScript 擅长 Web 开发&#xff0c;C 擅长性能&#xff0c;Go 擅长效率。 在广阔的软件开发世界中&#xff0c;选择正确的编程语言对于任何项目的成功都至关重要。Java 是一种以其多功能性和可移…

交换机Vlan实验

介绍 Vlan表示虚拟局域网。 常见的网络安全技术 VlanACL Vlan的作用 Vlan隔离了广播域&#xff0c;增加了网络的安全性。 知识点 默认vlan vlan1 是默认vlan&#xff0c;主要机器开机了&#xff0c;默认所有的接口都属于Vlan1 交换机的接口模式 Access : 这个模式用来…

RK3588平台开发系列讲解(文件系统篇)什么是 VFS

文章目录 一、什么是 VFS二、VFS 数据结构2.1、超级块结构2.2、目录结构2.3、文件索引结点2.4、打开的文件2.5、四大对象结构的关系沉淀、分享、成长,让自己和他人都能有所收获!😄 📢 今天我们一起来瞧一瞧 Linux 是如何管理文件,也验证一下 Linux 那句口号:一切皆为文…

卡尔曼滤波 | Matlab实现无迹kalman滤波仿真

文章目录 效果一览文章概述研究内容程序设计参考资料效果一览 文章概述 卡尔曼滤波 | Matlab实现无迹kalman滤波仿真 研究内容 无迹kalman滤波(UKF)不是采用的将非线性函数线性化的做法。无迹kalman仍然采用的是线性kalman滤波的架构,对于一步预测方程,使用无迹变换(UT)来…