XML - Schema之数据类型扩展

来源:互联网 发布:监测数据造假 编辑:程序博客网 时间:2024/05/13 22:58

一、定义 

<?xml version="1.0" encoding="UTF-8"?><schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  targetNamespace="http://www.xilen.com/customer" xmlns:customer="http://www.xilen.com/customer"> <!-- 定义当前命名空间的根元素customer  --><element name="customer" type="customer:customerType"/><complexType name="customerType"> <!-- 定义customer的数据类型  --><sequence><element name="name" type="customer:nameType"/> <!-- 定义customer的name子元素,数据类型引用nameType的定义 --><element name="address" type="customer:addressType"/><!-- 定义customer的address子元素,数据类型引用nameType的定义 --><element name="extName" type="customer:extNameType"/> <!-- 扩展后的元素  --><element name="extAddress" type="customer:extAddressType"/>  <!-- 扩展后的元素  --></sequence></complexType><simpleType name="nameType"> <!-- 定义Name --><restriction base="string"><pattern value="[a-zA-Z][_a-zA-Z0-9]*" /> <!-- 定义正则来规范名称 --><minLength value="3" /> <!-- 定义最小长度 --><maxLength value="18" /> <!-- 定义最大长度 --></restriction></simpleType><complexType name="addressType"> <!-- 定义address的数据类型  --><sequence><element name="code" type="customer:codeType" /> <!-- 定义address的code子元素,数据类型引用codeType的定义 --><element name="info" type="customer:infoType"/> <!-- 定义address的info子元素,数据类型引用infoType的定义 --></sequence></complexType><simpleType name="codeType"> <!-- 定义Code --><restriction base="string"> <!-- 定义限定条件,基于string的类型  --><pattern value="[0-9]*"/><length value="6"/> <!-- 定义最小长度 --></restriction></simpleType><simpleType name="infoType" > <!-- 定义Info的类型 --><restriction base="string"> <!-- 定义限定条件,基于string的类型  --><minLength value="0"/> <!-- 定义最小长度 --><maxLength value="255"/> <!-- 定义最大长度 --></restriction></simpleType><!-- - - - - - - - - - - - - - - - - - -  分隔线 - - - - - - - - - - - - - - - -  - --><!-- 对复杂类型addressType进行扩展,添加note子元素  --><complexType name="extAddressType"><complexContent><extension base="customer:addressType"><sequence><element name="note" type="string"/></sequence></extension></complexContent></complexType><!-- 对简单类型nameType进行扩展,添加id属性 --><complexType name="extNameType"><simpleContent><extension base="customer:nameType"><attribute name="id" type="string"/></extension></simpleContent></complexType></schema>
三、使用

<?xml version="1.0" encoding="UTF-8"?><customer xmlns="http://www.xilen.com/customer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.xilen.com/customer ext_customer.xsd"><name>Tom</name><address><code>100000</code><info>BeiJing</info></address><extName id="extId-0001">ExtTom</extName> <!-- 添加了Id属性  --><extAddress><code>100000</code><info>BeiJing</info><note>Home</note> <!-- 添加了note子元素 --></extAddress></customer>

 
 

 
 

0 0
原创粉丝点击