Schema
<?xmlversion="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.itcast.cn"
elementFormDefault="qualified">
<xs:element name='书架' >
<xs:complexType>
<xs:sequencemaxOccurs='unbounded' >
<xs:elementname='书' >
<xs:complexType>
<xs:sequence>
<xs:elementname='书名'type='xs:string' />
<xs:elementname='作者'type='xs:string' />
<xs:element name='售价' type='xs:string' />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
targetNamespace="http://www. itcast.cn"elementFormDefault="qualified">
将本文档中所有的标签全部都绑定到目标名字空间"http://www.itcast.cn"上
<?xml version="1.0" encoding="UTF-8"?>
<itcast:书架xmlns:itcast="http://www.itcast.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=“http://www.itcast.cn book.xsd">
<itcast:书>
<itcast:书名>JavaScript网页开发</itcast:书名>
<itcast:作者>张孝祥</itcast:作者>
<itcast:售价>28.00元</itcast:售价>
</itcast:书>
</itcast:书架>
itcast : 解释书架来自itcast, itcast来自于:xmlns:itcast=”http://www.itcast.cn”
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance不需要指定约束位置
1.使用默认名称空间
没有前缀名:
<书架 xmlns="http://www.class3g.org/xmlbook/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=“http://www.class3g.cn book.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>
2.使用名称空间引入多个XMLSchema文档
文件清单:xmlbook.xml
<?xml version="1.0"encoding="UTF-8"?>
<书架 xmlns="http://www.it315.org/xmlbook/schema"
xmlns:demo="http://www.it315.org/demo/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.it315.org/xmlbook/schema http://www.it315.org/xmlbook.xsd
http://www.it315.org/demo/schemahttp://www.it315.org/demo.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价 demo:币种=”人民币”>28.00元</售价>
</书>
</书架>
三个名字空间:xmlns="http://www.it315.org/xmlbook/schema
xmlns:demo=http://www.it315.org/demo/schema
xsi:schemaLocation="http://www.it315.org/xmlbook/schema
xsi:schemaLocation不需要指定绑定的约束文件,所以schemaLocation属性中只需声明两外两个约束文件即可:
xsi:schemaLocation="
http://www.it315.org/xmlbook/schema http://www.it315.org/xmlbook.xsd
http://www.it315.org/demo/schema http://www.it315.org/demo.xsd"
另外在
<售价 demo:币种=”人民币”>28.00元</售价>
其中“售价”这个标签来自于默认名字空间xmlns="http://www.it315.org/xmlbook/schema
而属性“币种”却来自于demo,xmlns:demo=http://www.it315.org/demo/schema
3.不使用名称空间引入XMLSchema文档
文件清单:xmlbook.xml
<?xml version="1.0"encoding="UTF-8"?>
<书架 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xmlbook.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>张孝祥</作者>
<售价>28.00元</售价>
</书>
</书架>
其中各标签并没有声明源自于那个名字空间,也没有定义默认名字空间,而是使用属性xsi:noNamespaceSchemaLocation="xmlbook.xsd"直接声明这些标签不使用名字空间,而且直接受到xmlbook.xsd文件的约束
<xs:attribute name="lang"type="xs:string"/>不需要特别声明属性是谁的,因为会被嵌套在属性所有者的标签内部。
4.分析xsd文件,写出xml
shipOrder.xsd
<?xml version="1.0"encoding="UTF-8" ?>
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:elementname="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson"type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:elementname="name" type="xs:string"/>
<xs:element name="address"type="xs:string"/>
<xs:element name="city"type="xs:string"/>
<xs:element name="country"type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title"type="xs:string"/>
<xs:element name="note"type="xs:string" minOccurs="0"/>
<xs:element name="quantity"type="xs:positiveInteger"/>
<xs:element name="price"type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string"use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
shipOrder.xml
<?xml version="1.0"encoding="UTF-8"?>
<shiporder xmlns="http://www.class3g.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.class3g.com shipOrder.xsd"
orderid="3">
<orderperson>Tom</orderperson>
<shipto>
<name>xxx</name>
<address>asdff.asdf</address>
<city>pk</city>
<country>jap</country>
</shipto>
<item>
<title>asd </title>
<note>ssssssss</note>
<quantity>3</quantity>
<price>20.3</price>
</item>
</shiporder>
根据xsd文件写出xml文件内容
order.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="orderItem"maxOccurs="10" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="orderItem">
<xsd:complexType>
<xsd:sequence>
<xsd:choice>
<xsd:element name="id"type="idType" />
<xsd:element name="name"type="xsd:string" />
</xsd:choice>
<xsd:element name="quantity"type="quantityType" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="idType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="7-5058-3496-7" />
<xsd:enumeration value="7-5005-6450-3" />
<xsd:enumeration value="7-3020-6069-7" />
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="quantityType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1" />
<xsd:maxInclusive value="10" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>