学习笔记——XML Schema自定义简单数据类型

本文详细介绍了XML Schema中的自定义数据类型,包括限制<restriction/>、列表<list/>和联合<union/>。fractionDigits和totalDigits约束用于限定数值精度,whiteSpace约束则规定了字符串中空白的处理方式。在限制派生中,通过添加约束创建新类型;列表由单个数据类型扩展,需要注意whiteSpace约束;联合类型由多种成员类型组成,其值可为任意成员类型的值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XML Schema规范共推荐12种约束。

范围约束

minInclusice、manInclusice、minExclusive、maxExcusive

长度约束length、minLength、maxLength
精度约束fractionDigits、totalDigits
枚举约束enumeration
正则表达式约束pattern
空白处理whiteSpace

 

fractionDigits:用于定义十进制数小数部分的精度。

totalDigits:用于指定decimal及其派生类型的数值精度。

whiteSpace:指定字符串中空白的处理方式,preserve(保留空白)、replace(字符串中Newline、tab和carriage-return都会被替换成空白)、collapse(字符串中Newline、tab和carriage-return都会被替换成空白,并且字符串中间连续的空白会被压缩成一个,字符串前后的空白会被自动删除)。

 

 约束的使用方式:

 

<xs:facet value="option" />

 

Table B1.a. Simple Types & Applicable Facets
Simple TypesFacets
 lengthminLengthmaxLengthpatternenumerationwhiteSpace
stringyyyyyy
normalizedStringyyyyyy
tokenyyyyysee (1)
base64Binaryyyyyysee (1)
hexBinaryyyyyysee (1)
integer   yysee (1)
positiveInteger   yysee (1)
negativeInteger   yysee (1)
nonNegativeInteger   yysee (1)
nonPositiveInteger   yysee (1)
long   yysee (1)
unsignedLong   yysee (1)
int   yysee (1)
unsignedInt   yysee (1)
short   yysee (1)
unsignedShort   yysee (1)
byte   yysee (1)
unsignedByte   yysee (1)
decimal   yysee (1)
float   yysee (1)
double   yysee (1)
boolean   y see (1)
duration   yysee (1)
dateTime   yysee (1)
date   yysee (1)
time   yysee (1)
gYear   yysee (1)
gYearMonth   yysee (1)
gMonth   yysee (1)
gMonthDay   yysee (1)
gDay   yysee (1)
Nameyyyyysee (1)
QNameyyyyysee (1)
NCNameyyyyysee (1)
anyURIyyyyysee (1)
languageyyyyysee (1)
IDyyyyysee (1)
IDREFyyyyysee (1)
IDREFSyyyyysee (1)
ENTITYyyyyysee (1)
ENTITIESyyyyysee (1)
NOTATIONyyyyysee (1)
NMTOKENyyyyysee (1)
NMTOKENSyyyyysee (1)
Note: (1) Although the whiteSpace facet is applicable to this type, the only value that can be specified iscollapse.

The facets listed in Table B1.b apply only to simple types which are ordered. Not all simple types are ordered and so B1.b does not list all of the simple types.

Table B1.b. Simple Types & Applicable Facets
Simple TypesFacets
 
max
Inclusive
max
Exclusive
min
Inclusive
min
Exclusive
total
Digits
fraction
Digits
integeryyyyysee (1)
positiveIntegeryyyyysee (1)
negativeIntegeryyyyysee (1)
nonNegativeIntegeryyyyysee (1)
nonPositiveIntegeryyyyysee (1)
longyyyyysee (1)
unsignedLongyyyyysee (1)
intyyyyysee (1)
unsignedIntyyyyysee (1)
shortyyyyysee (1)
unsignedShortyyyyysee (1)
byteyyyyysee (1)
unsignedByteyyyyysee (1)
decimalyyyyyy
floatyyyy  
doubleyyyy  
durationyyyy  
dateTimeyyyy  
dateyyyy  
timeyyyy  
gYearyyyy  
gYearMonthyyyy  
gMonthyyyy  
gMonthDayyyyy  
gDayyyyy  
Note: (1) Although the fractionDigits facet is applicable to this type, the only value that can be specified is zero.
 

 

 

一:限制<restriction.../>

限制派生:是以某个现有的类型为基础,通过对其添加N个约束,使其成为一个新的数据类型。

 基类型的指定有两种方式:

 

全局数据类型作为基类型格式:

 

<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType id="" name="数据类型名">
		<xs:restriction id="" base="基类型">
			...
		</xs:restriction>
	</xs:simpleType>	
</xs:schema>

 

局部数据类型作为基类型格式:

 

<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType id="" name="数据类型名">
		<xs:restriction id="">
			<xs:simpleType>
				<xs:restriction id="">
				...
				</xs:restriction>
			</xs:simpleType>
			...
		</xs:restriction>
	</xs:simpleType>	
</xs:schema>
 

id是可选属性。



二:列表<list.../>

列表是由 单个数据类型(原子类型、联合类型)扩展出来的,就像DTD中IDREFS是由IDREF扩展出来的。

列表以空白作为分隔符,所以不要使用本身就可以包含空格的类型作为基类型(e.g. string、normalizedString、token等元素),whiteSpace约束的值也只能是collapse。

列表使用enumeration和pattern约束时,是对整个列表内容起作用。(使用全局数据类型可避免对整个列表起作用)

定义列类型有两种方式:

 

全局数据类型作为列类型:

 

<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="aName">
		<xs:restriction base="基类型">
			...
		</xs:restriction>
	</xs:simpleType>
	
	<xs:simpleType name="aName_list">
		<xs:list itemType="aName" />
	</xs:simpleType>

	<xs:element name="" type="aName_list />
</xs:schema>

 

局部数据类型作为列类型:

 

 <?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="aName_list">
		<xs:list>
			<xs:simpleType>
				<xs:restriction base="">
				...
				</xs:restriction>
			</xs:simpleType>
		</xs:list>
	</xs:simpleType>

	<xs:element name="" type="aName_list" />
</xs:schema>
三:联合<union.../>

联合类型由成员类型(原子类型、列表类型、联合类型)组成,而联合类型的值可以是任意一种成员类型的值。

联合类型使用enumeration和pattern约束时,是对整个列表内容起作用。(使用全局数据类型可避免对整个联合起作用)

定义联合类型有两种方式:

 

全局数据类型作为联合类型:

 

<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="aName1">
		<xs:restriction base="基类型">
			...
		</xs:restriction>
	</xs:simpleType>

	<xs:simpleType name="aName2">
		<xs:restriction base="基类型">
			...
		</xs:restriction>
	</xs:simpleType>
	
	<xs:simpleType name="aName_union">
		<xs:union memberTypes="aName1 aName2" />
	</xs:simpleType>

	<xs:element name="" type="aName_union" />
</xs:schema>

 

局部数据类型作为联合类型:

 

<?xml version="1.0" encoding="GBK"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:simpleType name="aName_union">
		<xs:union>
			<xs:simpleType>
				<xs:restriction base="">
				...
				</xs:restriction>
			</xs:simpleType>

			<xs:simpleType>
				<xs:restriction base="">
				...
				</xs:restriction>
			</xs:simpleType>
			...
		</xs:union>
	</xs:simpleType>

	<xs:element name="" type="aName_union" />
</xs:schema>


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值