【WebServices前传二部曲】下部_Schema的元素类型

完整版见https://jadyer.github.io/2013/03/19/what-is-dtd-and-schema/


下部_Schema的元素类型——圣思园张龙   编辑:玄玉


Schemagroup
elementchoice
attributesimpleType
complexTypeattributeGroup

Schema的元素类型
schema,element,attribute,group,attributeGroup,simpleType,simpleContent,complexType
choice,list,union,unique,sequence,restriction等等
补充:在Schema中使用<any>和<anyAttribute>两个元素可以放宽Schema对XML文件内容的限制
      即它容许我们在XML文件中使用没有在Schema中定义的元素和属性
      它们均可用于制作可扩展的文档,它们使文档有能力包含未在Schema中声明过的附加元素

 

 

Schema元素
作用:包含已经定义的schema
用法:<xs:schema>
属性:xmlsn:后面跟命名空间的前缀
      targetNamespace:当前schema所定义的元素or属性所从属的命名空间

 

 

element元素
作用:声明一个元素
属性:name/type/ref/minOccurs/maxOccurs/substitutionGroup/fixed(表示确定或固定的值)/default(指明默认值)
示例:

	<!-- 这里定义了一个名为cat的类型为string的元素 -->
	<!-- 这里之所以写成"xs:string",是因为string来自于<xs:schema>命名空间,而该命名空间的前缀是xs -->
	<!-- 如果直接写成"type=string",那么它会在当前命名空间下找,即在targetNamespace里面去找 -->
	<!-- 而我们所定义的cat,它是在当前命名空间里的,所以后面引用时直接写成cat,它就会在当前命名空间里找到 -->
	<!-- 就是说,当使用schema属性or元素时,一定要加上命名空间前缀才可以 -->
	<xs:element name="cat" type="xs:string"/>
	<xs:element name="dog" type="xs:string"/>
	<xs:element name="pets">
		<xs:complexType>
			<!-- sequence元素用于给一组元素一个特定的序列,即cat要在dog前面 -->
			<!-- 并且它们最小出现次数为零次..最大出现次数为未绑定,即不限制 -->
			<!-- 若未指明minOccurs和maxOccurs属性,则表示cat和dog要按顺序出现,且只能出现一次 -->
			<xs:sequence minOccurs="0" maxOccurs="unbounded">
				<xs:element ref="cat"/>
				<xs:element ref="dog"/>
			</xs:sequence>
			<!-- all元素表示不限制子元素的出现顺序,但每个子元素必须出现且只能出现一次 -->
			<!-- 它也是complexType元素默认值,即若在complexType中直接定义element元素,它会采用all -->
			<xs:all>
				<xs:element name="pig" type="xs:string"/>
				<xs:element name="horse" type="xs:string"/>
			</xs:all>
		</xs:complexType>
	</xs:element>
	

 

 

group元素
作用:把一组元素声明组合在一起,以便它们能够一起被复合类型使用
属性:name/ref
示例:

	<!-- 
	实际上它对应的XML就是
	<myComplexType myAttribute="2.2">
		<thing11>aa</thing11>
		<thing22>bb</thing22>
	</myComplexType>
	 -->
	<xs:element name="thing11" type="xs:string"/>
	<xs:element name="thing22" type="xs:string"/>
	<xs:attribute name="myAttribute" type="xs:decimal"/>
	<xs:group name="myGroupOfThings">
		<xs:sequence>
			<xs:element ref="thing11"/>
			<xs:element ref="thing22"/>
		</xs:sequence>
	</xs:group>
	<xs:complexType name="myComplexType">
		<xs:group ref="myGroupOfThings"/>
		<xs:attribute ref="myAttribute"/>
	</xs:complexType>
	

 

 

attribute元素
作用:声明一个属性
属性:name/type/ref/use
示例:

	<xs:complexType name="myComplexType">
		<!-- use表示属性的使用方法,它还有两个值:optional和prohibited,其中use默认值为optional -->
		<xs:attribute name="myBaseAttribute" type="xs:string" use="required"/>
	</xs:complexType>
	

 

 

attributeGroup元素
作用:把一组属性声明组合在一起,以便可以被复合类型应用
属性:name/ref
示例:

	<xs:attributeGroup name="myAttributeGroup">
		<xs:attribute name="someAttribute11" type="xs:integer"/>
		<xs:attribute name="someAttribute22" type="xs:string"/>
	</xs:attributeGroup>
	<xs:complexType name="myElementType">
		<xs:attributeGroup ref="myAttributeGroup"/>
	</xs:complexType>
	

 

 

simpleType元素
作用:定义一个简单类型,它决定了元素和属性值的约束和相关信息
属性:name
内容:应用已经存在的简单类型,三种方式:
      restrict:限定一个范围
      list:从列表中选择
      union:包含一个值的结合
示例:

	<!-- 定义了一个myType类型,其值是基于整型的,可取值5,6,7 -->
	<!-- 也就是说这里的hello元素的值,只能是5,6,7,如<hello>6</hello> -->
	<xs:simpleType name="myType">
		<xs:restriction base="xs:integer">
			<xs:enumeration value="5"/>
			<xs:enumeration value="6"/>
			<xs:enumeration value="7"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="hello" type="myType"/>

	<!-- 定义名为hello的元素,其值为date类型,且可以有多个值,空格隔开即可 -->
	<!-- 如<hello>2013-03-15 2013-11-02 2013-12-12</hello>是允许的 -->
	<!-- 经测试,这里月和日必须是两位,且有效的,比如32号是无效的,不被验证通过 -->
	<xs:simpleType name="myType">
		<xs:list itemType="xs:date"/>
	</xs:simpleType>
	<xs:element name="hello" type="myType"/>

	<!-- 定义了一个名为java的元素,其子元素为string类型的level,其有一个version属性 -->
	<!-- version属性有两类可选值,一类为number类型的正整数,可取值为11,22,33 -->
	<!-- 另一类为size类型的字符串,可取值为small,medium,large -->
	<!-- 最后的xml文档就像下面这样 -->
	<!-- 
	<java version="22">
		<level/>
	</java>
	 -->
	<xs:attribute name="version">
		<xs:simpleType>
			<xs:union memberTypes="number size"/>
			<!-- 也可以直接写成下面这个样子 -->
			<!-- 
			<xs:union>
				<xs:simpleType>
					<xs:restriction base="number"/>
				</xs:simpleType>
				<xs:simpleType>
					<xs:restriction base="size"/>
				</xs:simpleType>
			</xs:union>
			 -->
		</xs:simpleType>
	</xs:attribute>
	<xs:simpleType name="number">
		<xs:restriction base="xs:positiveInteger">
			<xs:enumeration value="11"/>
			<xs:enumeration value="22"/>
			<xs:enumeration value="55"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:simpleType name="size">
		<xs:restriction base="xs:string">
			<xs:enumeration value="small"/>
			<xs:enumeration value="medium"/>
			<xs:enumeration value="large"/>
		</xs:restriction>
	</xs:simpleType>
	<xs:element name="java">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="level" type="xs:string"/>
			</xs:sequence>
			<xs:attribute ref="version" use="required"/>
		</xs:complexType>
	</xs:element>
	

 

 

complexType元素
作用:定义一个复合类型,它决定了一组元素和属性的约束和相关信息
属性:name
示例:

	<!-- 定义一个名为myComType的复合类型,该类型的元素内容是基于decimal的,即decimal类型的内容 -->
	<!-- 即extension表示元素类型,并且它还有一个名为sizing的,类型为字符串的属性 -->
	<!-- 最后在定义myShoeSize元素时指定了其类型为myComType类型 -->
	<!-- 注意:若指明mixed属性,即<complexType mixed="true">则表示该复合类型里面既可以有文本内容,也可以包含子元素 -->
	<!-- 注意:如<message>This message comes from<from>Jadyer</from></message> -->
	<xs:complexType name="myComType">
		<!-- simpleContent元素通常应用于complexType,用于对complexType的内容进行约束和扩展 -->
		<!-- 如果一个元素的类型是用simpleContent表示的,则该元素是没有子元素的,而只有内容 -->
		<xs:simpleContent>
			<xs:extension base="xs:decimal">
				<xs:attribute name="sizing" type="xs:string"/>
				<!-- 如果想限定sizing属性的取值,那么就可以通过下面这种方式 -->
				<!-- 
				<xs:attribute name="sizing">
					<xs:simpleType>
						<xs:restriction base="xs:string">
							<xs:enumeration value="US"/>
							<xs:enumeration value="European"/>
							<xs:enumeration value="UK"/>
						</xs:restriction>
					</xs:simpleType>
				</xs:attribute>
				 -->
			</xs:extension>
		</xs:simpleContent>
	</xs:complexType>
	<xs:element name="myShoeSize" type="myComType"/>
	<!-- simpleType与complexType的区别 -->
	<!-- simpleType类型的元素中不能包含元素或者属性 -->
	<!-- 当需要声明一个元素的子元素或属性时,用complexType -->
	<!-- 当需要基于内置的基本数据类型定义一个新的数据类型时,用simpleType -->
	

 

 

choice元素
作用:允许唯一的一个元素从一个组中被选择
属性:minOccurs/maxOccurs
示例:

	<xs:complexType name="chadState">
		<!-- 即以下四个元素最少出现一次,最多出现一次,即必须出现一次,而出现的这个元素就来源于这四个元素中的一个 -->
		<!-- minOccurs和maxOccurs属性默认取值为1,即直接写成<xs:choice>......</xs:choice>和下面效果是一样的 -->
		<xs:choice minOccurs="1" maxOccurs="1">
			<xs:element ref="selected"/>
			<xs:element ref="unselected"/>
			<xs:element ref="dimpled"/>
			<xs:element ref="perforated"/>
		</xs:choice>
	</xs:complexType>
	

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值