码迷,mamicode.com
首页 > 其他好文 > 详细

按照xml文件写Schema文件样例——orders.xml

时间:2018-06-18 23:20:40      阅读:410      评论:0      收藏:0      [点我收藏+]

标签:cat   color   targe   getname   tree   ace   enc   phone   org   

这里没有语法,只是把做过去的一个简单小练习放上来。以便查阅。

1、xml文件:

技术分享图片
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <orders xmlns="http://www.w3school.com.cn"
 3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4         xsi:schemaLocation="http://www.w3school.com.cn orders.xsd"> <!-- 所有订单信息 -->
 5     <order> <!-- 订单,至少包含1个订单 -->
 6         <orderDate>2018-05-20</orderDate><!--orderDate为日期型 -->
 7         <shipTo country="CN"><!-- 配送信息,country属性必须出现 -->
 8             <name>张三峰</name> <!-- 收件人,长度小于50 -->
 9             <street>市中区滨河路778号5+3大酒店</street> <!-- 县/区及街道地址 -->
10             <city>乐山市</city> <!-- 市/区,长度小于50 -->
11             <state>四川省</state> <!-- 省/自治区/直辖市,长度小于50 -->
12             <phone>13999999999</phone> <!-- 联系电话,要求必须是1开头,后面第2位数字3-9,再后面是9个数字 -->
13         </shipTo>
14         <items> <!-- 商品列表,item应至少出现1次 -->
15             <item partNum="872-AA"> <!-- 商品编号属性,必须有 -->
16                 <productName>香辣鸡翅</productName><!-- 商品名称,长度小于50 -->
17                 <quantity>1</quantity> <!-- 购买数量,至少是1 -->
18                 <price>18.95</price> <!-- 单价为浮点数,大于0.0 -->
19                 <shipDate>2018-05-21</shipDate> <!-- 配送日期 -->
20             </item>
21             <item partNum="926-AA">
22                 <productName>烧烤五花肉</productName>
23                 <quantity>20</quantity>
24                 <price>39.98</price>
25                 <shipDate>2018-05-20</shipDate>
26             </item>
27         </items>
28     </order>
29 </orders>
30 
31 <!-- 
32 xmlns="http://www.example.org/01"
33         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
34         xsi:noNameSpaceSchemaLocation="orders.xsd"
35  -->
orders.xml

2、schema文件:

技术分享图片
  1 <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  2             targetNamespace="http://www.w3school.com.cn"
  3             xmlns="http://www.w3school.com.cn"
  4             elementFormDefault="qualified">
  5     <xsd:element name="orders">
  6         <xsd:complexType>
  7             <xsd:sequence>
  8                 <xsd:element ref="order" minOccurs="1" maxOccurs="unbounded"></xsd:element>
  9             </xsd:sequence>
 10         </xsd:complexType>
 11     </xsd:element>
 12     
 13     <xsd:element name="order">
 14         <xsd:complexType>
 15             <xsd:sequence>
 16                 <xsd:element name="orderDate" type="xsd:date" minOccurs="1" maxOccurs="1"></xsd:element>
 17                 <xsd:element ref="shipTo" minOccurs="1" maxOccurs="1"></xsd:element>
 18                 <xsd:element ref="items" minOccurs="1" maxOccurs="1"></xsd:element>
 19             </xsd:sequence>
 20         </xsd:complexType>
 21     </xsd:element>
 22     
 23     <xsd:element name="shipTo">
 24         <xsd:complexType>
 25             <xsd:sequence>
 26                 <xsd:element ref="name"></xsd:element>
 27                 <xsd:element name="street"></xsd:element>
 28                 <xsd:element name="city"></xsd:element>
 29                 <xsd:element name="state"></xsd:element>
 30                 <xsd:element ref="phone"></xsd:element>
 31             </xsd:sequence>
 32             <xsd:attribute name="country" type="xsd:string" use="required"></xsd:attribute>
 33         </xsd:complexType>
 34     </xsd:element>
 35     
 36     <xsd:element name="items">
 37         <xsd:complexType>
 38             <xsd:sequence>
 39                 <xsd:element ref="item" minOccurs="1" maxOccurs="unbounded"></xsd:element>
 40             </xsd:sequence>
 41         </xsd:complexType>
 42     </xsd:element>
 43     
 44     <xsd:element name="item">
 45         <xsd:complexType>
 46             <xsd:sequence>
 47                 <xsd:element ref="productName"></xsd:element>
 48                 <xsd:element ref="quantity"></xsd:element>
 49                 <xsd:element ref="price"></xsd:element>
 50                 <xsd:element name="shipDate" type="xsd:date"></xsd:element>
 51             </xsd:sequence>
 52             <xsd:attribute name="partNum" type="xsd:string" use="required"></xsd:attribute>
 53         </xsd:complexType>
 54     </xsd:element>
 55     
 56     <xsd:element name="name">
 57         <xsd:simpleType>
 58             <xsd:restriction base="xsd:string">     
 59                 <xsd:minLength value="0"/>
 60                 <xsd:maxLength value="50"/>
 61             </xsd:restriction>
 62         </xsd:simpleType>
 63     </xsd:element>
 64     
 65     <xsd:element name="phone">
 66         <xsd:simpleType>
 67             <!-- <xsd:restriction base="xsd:integer">
 68                 <xsd:minInclusive value="13000000000"></xsd:minInclusive>
 69                 <xsd:maxInclusive value="19999999999"></xsd:maxInclusive>
 70             </xsd:restriction> -->
 71             <xsd:restriction base="xsd:string">
 72                 <xsd:minLength value="11"></xsd:minLength>
 73                 <xsd:maxLength value="11"></xsd:maxLength>
 74                 <xsd:pattern value="1[3-9]{1}[0-9]{9}"></xsd:pattern>
 75             </xsd:restriction>
 76         </xsd:simpleType>
 77     </xsd:element>
 78     
 79     <xsd:element name="productName">
 80         <xsd:simpleType>
 81             <xsd:restriction base="xsd:string">
 82                 <xsd:maxLength value="50"/>
 83             </xsd:restriction>
 84         </xsd:simpleType>
 85     </xsd:element>
 86     
 87     <xsd:element name="quantity">
 88         <xsd:simpleType>
 89             <xsd:restriction base="xsd:integer">
 90                 <xsd:minInclusive value="1" />
 91             </xsd:restriction>
 92         </xsd:simpleType>
 93     </xsd:element>
 94     
 95     <xsd:element name="price">
 96         <xsd:simpleType>
 97             <xsd:restriction base="xsd:float">
 98                 <xsd:minInclusive value="0.1"></xsd:minInclusive>
 99             </xsd:restriction>
100         </xsd:simpleType>
101     </xsd:element>
102 </xsd:schema>
orders.xsd

和dtd有异曲同工之妙,但是各有利弊,不存在谁取代谁的,如果你是初学者又不幸看到我的博客,那么请你去百度一下,找最前面的那几个好好学习一下。

按照xml文件写Schema文件样例——orders.xml

标签:cat   color   targe   getname   tree   ace   enc   phone   org   

原文地址:https://www.cnblogs.com/Ddlm2wxm/p/9196604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!