Home > Article > Web Front-end > XML study notes 4??XSD simple data type_html/css_WEB-ITnose
XSD provides data types and supports custom data types, but all of this is based on XSD built-in data types and a set of rules for extending built-in data types. In this note , let’s take a look at the data types in XSD.
1. As can be seen from the XSD data type diagram above, it is mainly divided into two categories:
(1) Simple type: It can be used for attributes or elements. In addition to built-in types, you can also use 5177ff1dedbe47c533be80089d72c92fCustomize simple types, and there are three ways to customize them: restriction14990b85fa3ba5c0278c91ac094b6057, list4309a73696dbaeac0ddd115cebb6f9b7, and unionb47ed4bd32efd0b38ac0da4c2f0ea1dd.
(2) Complex type: can only be used for elements, and all need to be customized using b04708ad431d182f917fc0425233bdde. According to the content, it can be further divided into complex types with simple content and complex types with complex content. Use 82c5419d49e1979f95fb7d975b38bb67 and 2cc22049b0adbb889777606273b51a92 respectively to define its content. In addition, complex types can also use restrictions 14990b85fa3ba5c0278c91ac094b6057 and extensions d48e197d6ef23c023982d3f523fa26d2 to derive new types.
Complex types can only be used for elements, which is probably why we distinguish between simple types and complex types.
2. Built-in types
Built-in types have a basic important position and are the basis for deriving other types. First, cut out an inheritance diagram of built-in types from the official documentation. :
Several descriptions in the legend: built-in primitive types (built-in basic types), built-in derived types (built-in derived types), complex types (complex types) , derived by restriction, derived by list, derived by extension or restriction.
The first impression of this picture is that there are two pillars (string and its derivatives, decimal and its derivatives) with two floors of shops on them. The pillars are the spine and the shops are decorations?? It’s a bit far. , let’s take a closer look at these built-in types!
(1) String type
Note that although QNAME is string type data, it is not derived from string due to problems with the derivation mechanism.
(2) Numeric type
Float and double types can also accept special values: -INF (negative infinity), INF (positive infinity), NaN (not a number), 0 (positive zero) and -0 (negative zero). Where positive zero is greater than negative zero, NaN is greater than all numbers (including INF), and INF is greater than all floating-point numbers.
类型 | 说明 | |
float | 32位的单精度浮点数,可使用科学计数法,整数部分为0时可省略,但不能省略小数点,不能用f/F后缀 | |
double | 64位的双精度浮点数,可使用科学计数法,整数部分为0时可省略,但不能省略小数点 | |
decimal | 精确小数,不能使用科学计数法,不能接受-INF、INF、NaN等特殊值 | |
integer | 代表任意大的整数 | |
nonPositiveInteger | 非正整数 | |
negativeInteger | 负整数 | |
long | 64位的有符号整数 | |
int | 32位的有符号整数 | |
short | 16位的有符号整数 | |
byte | 8位的有符号整数 | |
nonNegativeInteger | 非负整数 | |
positvieInteger | 正整数 | |
unsignedLong | 64位的无符号整数 | |
unsignedInt | 32位的无符号整数 | |
unsignedShort | 16位的无符号整数 | |
unsignedByte | 8位的无符号整数 |
(3) Boolean type
The Boolean type can accept four values: true, false, 1 (representing true), and 0 (representing false).
(4) Date and time types
类型 | 格式 | 说明 |
date | YYYY-MM-DD | 日期 |
time | hh:mm:ss.sss | 时间,sss表示毫秒数 |
dateTime | YYYY-MM-DDThh:mm:ss.sss | 日期时间,中间的T是必须的,是日期和时间的分隔符 |
gYear | YYYY | 年 |
gYearMonth | YYYY-MM | 年月 |
gMonth | --MM | 月,前面的两个中划线是必须的 |
gMonthDay | --MM-DD | 月日,前面的两个中划线是必须的 |
gDay | ---DDD | 日,前面的三个中划线是必须的 |
duration | PnYnMnDTnHnMnS | 定义时间间隔,P是固定的,表示周期,S前的n可以有小数部分,其它必须是整数 |
Note: Z can be added after the first 8 types listed above to represent UTC time; Y, M, D, h, m, s represent year, month, day, hour, minute and second respectively, and can be replaced with a valid integer, where , if the year is not enough, add 0 to the left, add a negative sign in front to indicate BC, if the month, day, hour, minute, and second are not enough, add 0 to the left, and the millisecond sss can be an integer with 1-3 digits.
(5) Binary data type
There are the following two binary data types in XSD:
A: hexBinary, binary data saved in hexadecimal, so only It can be composed of characters such as 0~9, a~f, A~F, etc. The character length must be an even number.
B: base64Binary, any binary data saved in Base64 encoding, so it can only be composed of characters such as 0~9, a~f, A~F and plus sign. The character length must be a multiple of 4.
(6) anyURI type: legal URI.
(7) NOTATION type: Same as DTD, representing legal symbols.
3. Customize simple data types
(1) The syntax of using the 4f03d1856cfe1dbd6aa7bb2502caba1f element to customize simple data types is as follows:
<simpleType id=ID name=NCName final=... any-attributes> (annotation?,(restriction|list|union)) <!-- 1.id属性是可选的,用于唯一标识<simpleType>元素本身 2.name属性表示自定义数据类型的名称,值是NCName类型,并且必须在所有<simpleType>和<complexType>之间唯一,<simpleType>是<schema>的子元素时必须指定,这个时候新定义的数据类型为全局的数据类型 --></simpleType>
(2) Global data types can be defined under the schema element, and local data types can also be defined under other elements.
(3) The final attribute is used to limit the derivation of new types. The default value is the value of the finalDefault attribute of the root element b7b165c079dc80a737996dfae2b9c338. The possible values are:
A, #all : Restrict this type to derive new types in any form
B. Free combination of restriction, list, union: Restrict the use of specified methods to derive new types
C, "": Do not do Any restrictions
4. Derive custom data types through restrictions
From a syntactic point of view, use the 4f03d1856cfe1dbd6aa7bb2502caba1f element to customize a simple type. There are three specific ways: restriction 14990b85fa3ba5c0278c91ac094b6057, List4309a73696dbaeac0ddd115cebb6f9b7, unionb47ed4bd32efd0b38ac0da4c2f0ea1dd. This section will first look at the first type: restriction14990b85fa3ba5c0278c91ac094b6057.
(1) The syntax format is as follows:
<simpleType ...> <restriction id=ID base=QNAME any-attributes> <!--1.处于<simpleType>元素下的语法--> (annotation?,(simpleType?,(12种数据类型约束)*)) </restriction></simpleType><simpleContent ...> <restriction id=ID base=QNAME any-attributes> <!--2.处于<simpleContent>元素下的语法--> (annotation?,(simpleType?,(12种数据类型约束)*)?,((attribute|attributeGroup)*,anyAttribute?)) </restriction></simpleContent><complextContent ...> <restriction id=ID base=QNAME any-attributes> <!--3.处于<complextContent>元素下的语法--> (annotation?,(group|all|choice|sequence)?,((attribute|attributeGroup)*,anyAttribute?)) </restriction></complextContent>
The id attribute uniquely identifies the 14990b85fa3ba5c0278c91ac094b6057 element itself, optional; the base attribute represents the schema (or other schema indicated by the specified namespace). You can also directly use the 4f03d1856cfe1dbd6aa7bb2502caba1f sub-element to define the restricted base type without specifying the base attribute.
(2) Constraints: In XSD, restrictions are achieved by adding constraints on the base type. So, what are the constraints? To which base types can these constraints apply?
Classification | Available data types | Constraints | Description | Enumeration constraints | enumeration | List of acceptable values | |||||||||||||||||||||||||||||||||||||
Precision Constraints | decimal | fractionDigits | Maximum number of decimal places allowed | ||||||||||||||||||||||||||||||||||||||||
totalDigits | Maximum number of digits allowed (excluding decimal point) | ||||||||||||||||||||||||||||||||||||||||||
Length constraints |
| length | Character length or number of list items | ||||||||||||||||||||||||||||||||||||||||
maxLength | The maximum length of characters or the number of list items | ||||||||||||||||||||||||||||||||||||||||||
minLength | The length of characters or the number of list items Minimum value of number | ||||||||||||||||||||||||||||||||||||||||||
Range constraint | Types that can compare sizes, such as numerical values, dates, etc. | maxExclusive | The upper limit of the allowed value cannot be equal to the upper limit value | ||||||||||||||||||||||||||||||||||||||||
minExclusive | The lower limit of the allowed value cannot be equal to the lower limit value | ||||||||||||||||||||||||||||||||||||||||||
maxInclusive | The upper limit of the allowed value can be equal to the upper limit value | ||||||||||||||||||||||||||||||||||||||||||
minInclusive | Allow The lower limit of the value can be equal to the lower limit value | ||||||||||||||||||||||||||||||||||||||||||
Regular expression constraints | Various data types | pattern | Use regular expressions to restrict the characters that can appear | ||||||||||||||||||||||||||||||||||||||||
Blank handling constraints | whiteSpace | Define how whitespace characters (line feed, carriage return, space, tab, etc.) are processed preserve: keep all whitespace as is replace: replace all whitespace characters with spaces collapse: replace first, then remove the leading and trailing spaces, and compress the consecutive spaces in the middle into one |
在派生新的类型时,如果原来类型有一个约束,新派生类型使用相同的约束,且新约束范围在原约束范围之内,则新类型的约束将会覆盖原类型的约束。但有时候,想阻止派生类型覆盖已有的约束,这个时候可以在原类型的约束上添加fixed属性(true|false)。
5、通过列表派生自定义数据类型(1)列表4309a73696dbaeac0ddd115cebb6f9b7元素的语法:
<list id=ID itemType=QName any-attributes> (annotatin?,(simpleType?))</list>
(2)指定列表成员类型的方法
A、使用itemType属性
B、使用子元素4f03d1856cfe1dbd6aa7bb2502caba1f,这个时候不能指定itemType属性
(3)实际上,内置的IDREFS、ENTITIES、NMTOKENS分别是IDREF、ENTITY、NMTOKEN类型的列表类型。
(4)使用空格作为列表类型值的分隔符。
(5)可以对列表类型使用长度约束(约束列表项的个数)、枚举约束、正则表达式约束和空白处理约束(但值只能是collapse),需要注意的是,枚举约束和正则表达式约束的是整个列表类型的值,而不仅仅只是其中一个列表项值。
6、通过联合派生自定义数据类型
(1)联合b47ed4bd32efd0b38ac0da4c2f0ea1dd元素的语法:
<union id=ID memberTypes="QName列表" any-attributes> (annotatin?,(simpleType*))</union>
(2)指定联合成员类型的方法
A、使用属性memberTypes,多个类型使用空格分隔
B、使用一个或多个子元素4f03d1856cfe1dbd6aa7bb2502caba1f
(3)可以对联合类型使用枚举约束和正则表达式约束,同样,约束的也是整个联合类型的值。
(4)列表类型和联合类型都是在现有类型的基础上派生新类型,在XSD中还可以使用4309a73696dbaeac0ddd115cebb6f9b7元素由联合类型派生出相应的列表类型,也可以使用b47ed4bd32efd0b38ac0da4c2f0ea1dd元素将一个或多个已有的列表类型派生出新的联合类型,不过需要注意的是,不能使用列表类型派生新的列表类型,也不能使用含有列表类型的联合类型派生新的列表类型。
7、最后看一个使用三种方式自定义简单类型的实例:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <!--从int派生的ageType--> <xs:simpleType name="ageType"> <xs:restriction base="xs:int"> <xs:maxInclusive value="100"/> <xs:minInclusive value="0"/> </xs:restriction> </xs:simpleType> <!--由ageType派生出对应的列表类型--> <xs:simpleType name="ageListType"> <xs:list itemType="ageType"/> </xs:simpleType> <!--从string派生的nameType--> <xs:simpleType name="nameType"> <xs:restriction base="xs:string"> <xs:maxLength value="20"/> <xs:minLength value="4"/> </xs:restriction> </xs:simpleType> <!--由nameType派生出对应的列表类型--> <xs:simpleType name="nameListType"> <xs:list itemType="nameType"/> </xs:simpleType> <!--由ageType和nameType派生出联合类型--> <xs:simpleType name="ageType_nameType"> <xs:union memberTypes="ageType nameType"/> </xs:simpleType> <!--使用两个列表类型派生联合类型--> <xs:simpleType name="ageListType_nameListType"> <xs:union memberTypes="ageListType nameListType"/> </xs:simpleType> <!--使用两个联合类型派生联合类型--> <xs:simpleType name="furtherType"> <xs:union memberTypes="ageType_nameType ageListType_nameListType"/> </xs:simpleType> <!--一个列表类型,一个联合类型派生联合类型--> <xs:simpleType name="furtherMixType"> <xs:union memberTypes="nameListType ageListType_nameListType"/> </xs:simpleType> <!--由联合类型派生出对应的列表类型--> <xs:simpleType name="ageType_nameType_ListType"> <xs:list itemType="ageType_nameType"/> </xs:simpleType></xs:schema>