xml通过schema方式的文档定义xsd,及使用示例

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/t3" elementFormDefault="qualified"> <!-- 如果声明部分为: <schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 则下面节点和属性必须加上xs:可以修改xs 。 默认为<schema xmlns="http://www.w3.org/2001/XMLSchema" --> <xs:element name="ISBN"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]-[0-9]{3}-[0-9]{5}-[0-9]"> </xs:pattern> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="author" type="xs:string"> </xs:element> <xs:element name="bookName" type="xs:string"> </xs:element> <xs:element name="price"> <xs:simpleType> <!--xml schema中restriction定义可接受的值。 fractionDigits定义值的最大的小数位数,必须大于等于0--> <xs:restriction base="xs:decimal"> <xs:fractionDigits value="2"> </xs:fractionDigits> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="quantity"> <xs:simpleType> <!-- xml schema中restriction定义可接受的值。 maxExclusive定义值的上限(大于),minExclusive定义值的下限(小于等于)。 --> <xs:restriction base="xs:byte"> <xs:maxExclusive value="100"></xs:maxExclusive> <xs:minExclusive value="0"></xs:minExclusive> </xs:restriction> </xs:simpleType> </xs:element> <xs:complexType name="bookType"> <!-- xml schema中sequence定义值必须按此顺序出现--> <xs:sequence> <xs:element ref="bookName"></xs:element> <xs:element ref="author"></xs:element> <xs:element ref="ISBN"></xs:element> <xs:element ref="price"></xs:element> <xs:element ref="quantity"></xs:element> </xs:sequence> <xs:attribute name="id" use="required"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{3}"></xs:pattern> </xs:restriction> </xs:simpleType> </xs:attribute> <xs:attribute name="type" use="required"> <xs:simpleType> <!-- xml schema中restriction定义可接受的值。 enumeration定义可接受的值的列表。--> <xs:restriction base="xs:string"> <xs:enumeration value="IT"></xs:enumeration> <xs:enumeration value="education"></xs:enumeration> <xs:enumeration value="novel"></xs:enumeration> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> <xs:element name="books"> <!-- xml schema中complexType定义复合元素 --> <xs:complexType> <xs:sequence> <!--xml schema中 maxOccurs定义某个元素最多可出现的次数, minOccurs定义某个元素最少可出现的次数--> <xs:element name="book" type="bookType" maxOccurs="unbounded" minOccurs="1"></xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>


<?xml version="1.0" encoding="UTF-8"?> <books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="t3.xsd"> <book id="001" type="IT"> <bookName>mastering XML</bookName> <author>Jack LEE</author> <ISBN>0-125-11111-6</ISBN> <price>36.00</price> <quantity>6</quantity> </book> <book id="002" type="education"> <bookName>the method of study</bookName> <author>Thomas</author> <ISBN>0-125-22222-7</ISBN> <price>45.00</price> <quantity>5</quantity> </book> <book id="003" type="novel"> <bookName>The hero</bookName> <author>TuDou</author> <ISBN>0-125-88888-6</ISBN> <price>88.00</price> <quantity>2</quantity> </book> </books>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值