javaweb学习总结十四(xml约束之Schema)

一:schema约束简单介绍

1:xml Schema的定义以及优缺点

2:xml schema入门

3:命名空间

这里http://www.itcast.cn 并没有什么具体的意义,只是命名而已。

4:xml schema的引入

5:命名空间引入schema

6:不使用命名空间引入schema

二:schema约束语法

更多语法可以参考xml schema的约束文档

三:案例分析

根据shiporder.xsd的schema约束,写xml文件

shiporder.xsd:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 3 targetNamespace="http://www.warrior.com"
 4 elementFormDefault="qualified">
 5 <xs:element name="shiporder">
 6  <xs:complexType>
 7   <xs:sequence>
 8    <xs:element name="orderperson" type="xs:string"/>
 9    <xs:element name="shipto">
10     <xs:complexType>
11      <xs:sequence>
12       <xs:element name="name" type="xs:string"/>
13       <xs:element name="address" type="xs:string"/>
14       <xs:element name="city" type="xs:string"/>
15       <xs:element name="country" type="xs:string"/>
16      </xs:sequence>
17     </xs:complexType>
18    </xs:element>
19    <xs:element name="item" maxOccurs="unbounded">
20     <xs:complexType>
21      <xs:sequence>
22       <xs:element name="title" type="xs:string"/>
23       <xs:element name="note" type="xs:string" minOccurs="0"/>
24       <xs:element name="quantity" type="xs:positiveInteger"/>
25       <xs:element name="price" type="xs:decimal"/>
26      </xs:sequence>
27     </xs:complexType>
28    </xs:element>
29   </xs:sequence>
30   <xs:attribute name="orderid" type="xs:string" use="required"/>
31  </xs:complexType>
32 </xs:element>
33 
34 </xs:schema>
targetNamespace="http://www.warrior.com"  :  命名空间,相当于定义给xsd约束文件定义一个名称
elementFormDefault="qualified" : 将元素绑定大命名空间上

xml文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <shiporder  xmlns="http://www.warrior.com"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xsi:schemaLocation="http://www.warrior.com  shiporder.xsd" orderid="xxx">
 5     <orderperson>tom</orderperson>
 6     <shipto>
 7         <name>jack</name>
 8         <address>nanjing</address>
 9         <city>nanjing</city>
10         <country>china</country>
11     </shipto>
12     <item>
13         <title>ddd</title>
14         <note>ddd</note>
15         <quantity>30</quantity>
16         <price>12.0</price>
17     </item>
18 </shiporder>
xmlns="http://www.warrior.com" :使用默认命名空间约束
xsi:schemaLocation="http://www.warrior.com  shiporder.xsd" :默认命名空间对应的约束文件为shiporder.xsd
xsi:schemaLocation 定义来源于
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" w3c组织。
原文地址:https://www.cnblogs.com/warrior4236/p/5874721.html