`
caleb_520
  • 浏览: 247834 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

xml schema的例子

    博客分类:
  • XML
阅读更多
XML Schema介绍

(1)XML Schema 是基于 XML 的 DTD 替代者。

(2)XML Schema 可描述 XML 文档的结构

XML Schema作用:

    * 定义可出现在文档中的元素
    * 定义可出现在文档中的属性
    * 定义哪个元素是子元素
    * 定义子元素的次序
    * 定义子元素的数目
    * 定义元素是否为空,或者是否可包含文本
    * 定义元素和属性的数据类型
    * 定义元素和属性的默认值以及固定值

初步体验xml schema的例子

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="booklist">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="book" type="bookType" maxOccurs="unbounded"></xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	
	<xs:complexType name="bookType">
		<xs:sequence>
			<xs:element name="ISBN" type="xs:string"></xs:element>
			<xs:element name="title" type="xs:string"></xs:element>
			<xs:element name="authorlist" type="authorlistType"></xs:element>
			<xs:element name="price" type="xs:decimal"></xs:element>
		</xs:sequence>
		<xs:attribute name="classify" use="required">
			<xs:simpleType>
				<xs:restriction base="xs:string">
					<xs:enumeration value="社会科学"></xs:enumeration>
					<xs:enumeration value="自然科学"></xs:enumeration>
				</xs:restriction>
			</xs:simpleType>
		</xs:attribute>
	</xs:complexType>
	
	<xs:complexType name="authorlistType">
		<xs:sequence maxOccurs="4">
			<xs:element name="author" type="xs:string"></xs:element>
		</xs:sequence>
	</xs:complexType>
</xs:schema>


由xml schema定义后的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<booklist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\天气预报\3.xsd">
	<book classify="自然科学 ">
		<ISBN/>
		<title/>
		<authorlist>
			<author/>
		</authorlist>
		<price/>
	</book>
	<book classify="社会科学">
		<ISBN></ISBN>
		<title></title>
		<authorlist>
			<author></author>
		</authorlist>
		<price></price>
	</book>
</booklist>
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics