案例
(Book.xsb)
<?xml version="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:sequence maxOccurs='unbounded' >
<xs:element name='书' >
<xs:complexType>
<xs:sequence>
<xs:element name='书名' type='xs:string' />
<xs:element name='作者' type='xs:string' />
<xs:element name='售价' type='xs:string' />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
(Book.xml)
<?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:作者>redarmy</itcast:作者>
<itcast:售价>28.00元</itcast:售价>
</itcast:书>
</itcast:书架>
两个文件:XML和xsb 本质上xsb也是XML文件
每个XML都是使用命名空间的。
可以通过名称空间声明(xmlns),来声明当前XML文件的命名空间
注意:
- XML可以没有命名空间xmlns="http://www.itcast.cn"
- XML命名空间太长,可以起别名。如:<itcast:书架 xmlns:itcast="http://www.itcast.cn"/> 这里itcast就是"http://www.itcast.cn"的别名,itcast:书架 就是http://www.itcast.cn 书架
上述两个文件:
book.xsb 的命名空间是
http://www.w3.org/2001/XMLSchema
book.xml的命名空间是:
http://www.itcast.cn
如 book.xsb 被绑定URI命名空间 http://www.w3.org/2001/xmlSchema上 )以后XML文件就可以通过这个URI(即名称空间)来告诉解析引擎,xml文档中编写的元素来自哪里,被谁约束(如book.xml中)
二 为了在一个XML文档中声明它所遵循的Schema文件的具体位置,通常需要在Xml文档中的根结点中使用schemaLocation属性来指定,例如:
<itcast:书架 xmlns:itcast="http://www.itcast.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=“http://www.itcast.cn book.xsd">
•
schemaLocation此属性有两个值。第一个值是需要使用的命名空间。第二个值是供命名空间使用的 XML schema 的位置,两者之间用空格分隔。详细请查看:
点击打开链接
•
注意,在使用schemaLocation属性时,也需要指定该属性来自哪里。即 该属性来自
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"这句话是不能少,必须写的。
本xml文件中要用到某些来自xsi代表的“http://www.w3.org/2001/XMLSchema-instance”这个命名空间的元素
比如用来引入无命名空间schema文件的noNamespaceSchemaLocation="XXX";
以及引入自带命名空间的schema文件的schemaLocation="XXX"这些元素。
这些元素是包含在xsi命名空间中的,所有的xml文件只要引用这些元素 就要引入xsi这个命名空间。
xsi这三个字母不是硬性规定,只是大家都这么用,方便阅读而已。
三
<span style="color:#333333;"> targetNamespace="http://www.itcast.cn"</span>
ta
targetNamespace告诉xsd文件,我这个xsd文件时约束哪个命名空间上的谁的(http://www.itcast.cn 上的book.xml)
使用默认名称空间:
•
基本格式:
xmlns="URI"
•
举例:
<书架 xmlns="http://www.it315.org/xmlbook/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=“http://www.itcast.cn book.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>redarmy</作者>
<售价>28.00元</售价>
</书>
<书架>
使用名称空间引入多个XML Schema文档:
文件清单: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/schema http://www.it315.org/demo.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>redarmy</作者>
<售价 demo:币种=”人民币”>28.00元</售价>
</书>
</书架>
不使用名称空间引入XML Schema文档:
文件清单:xmlbook.xml
<?xml version="1.0" encoding="UTF-8"?>
<书架 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xmlbook.xsd">
<书>
<书名>JavaScript网页开发</书名>
<作者>redarmy</作者>
<售价>28.00元</售价>
</书>
</书架>
在XML Schema文档中声明名称空间:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www. itcast.cn"
elementFormDefault="qualified">
<xs:schema>
targetNamespace元素用于指定schema文档中声明的元素属于哪个名称空间。
elementFormDefault元素用于指定,该schema文档中声明的根元素及其所有子元素都属于targetNamespace所指定的名称空间。