XML中的属性:attribute

在.XSD文件中添加属性约束

必须是在<complex></complex> 中间添加属性约束

代码:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="book" type="booktype"></xs:element>
		<xs:complexType name="booktype"  >
			<xs:sequence>
				<xs:element name="bookName" type="xs:string" minOccurs="1" maxOccurs="unbounded"></xs:element>
				<xs:element name="bookPrice" type="xs:float"></xs:element>
				<xs:element name="author" type="xs:string"></xs:element>
			</xs:sequence>
			<xs:attribute name="stuno" type="xs:int" use="required"></xs:attribute>
		</xs:complexType>
</xs:schema>

在XML中的表现

代码:

<?xml version="1.0" encoding="UTF-8"?>
<book  stuno="1"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Untitled3.xsd">
<bookName>倚天屠龙记</bookName>
<bookName>红龙门</bookName>
<bookPrice>23.0</bookPrice>
<author>金庸</author>


</book>

Name:属性名

Type:属性类型

ref:外部属性的引用

use:是否是必填


引用外部的属性约束

ref:

代码:xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="book">
		<xs:complexType >
			<xs:sequence>
				<xs:element name="bookName" type="xs:string"></xs:element>
				<xs:element name="bookPrice" type="xs:float"></xs:element>
				<xs:element name="author" type="xs:string"></xs:element>
			</xs:sequence>
			<xs:attribute  ref="stuno" use="required"></xs:attribute>
		</xs:complexType>
		
	</xs:element>
	<xs:attribute name="stuno" type="xs:int"></xs:attribute>
</xs:schema>

XML中代码:

<?xml version="1.0" encoding="UTF-8"?>
<book stuno="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Untitled3.xsd">
<bookName>倚天屠龙记</bookName>
<bookPrice>23.0</bookPrice>
<author>金庸</author>


</book>

属性组:定义一组属性!

约束:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="book">
		<xs:complexType >
			<xs:sequence>
				<xs:element name="bookName" type="xs:string"></xs:element>
				<xs:element name="bookPrice" type="xs:float"></xs:element>
				<xs:element name="author" type="xs:string"></xs:element>
			</xs:sequence>
			<xs:attributeGroup ref="stuattr"></xs:attributeGroup>
		</xs:complexType>
		
	</xs:element>
	<xs:attributeGroup name="stuattr" >
		<xs:attribute name="stuno" type="xs:int" use="required"></xs:attribute>
		<xs:attribute name="score" type="xs:string" use="required"></xs:attribute>
	</xs:attributeGroup>
</xs:schema>

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<book  score="99"   stuno="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Untitled3.xsd">
<bookName>倚天屠龙记</bookName>
<bookPrice>23.0</bookPrice>
<author>金庸</author>


</book>

这里是属性约束是对主属性进行的约束!










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值