首先,xml與xml schema(xsd)文件都是xml格式的文件,都遵循相同的命名空間規則
在schema元素中通過xmlns指定約束文件位置
如下面的xsd文件中的代碼
1 <?xml version="1.0" encoding="UTF-8"?> 2 <schema xmlns="http://www.w3.org/2001/XMLSchema" 3 targetNamespace="http://www.example.org/athena" 4 elementFormDefault="qualified"> 5 <element name="body"> 6 <complexType> 7 <sequence> 8 <element name="id" type="int"/> 9 <element name="name" type="string"/> 10 <element name="birthday" type="dateTime"/> 11 </sequence> 12 </complexType> 13 </element> 14 </schema>
通過xmlns指定xsd文件的約束文件位置
當一個文件中需要指定多個約束文件時,可以通過xmlns:namespace區分不同的約束文件,在調用元素時需要在元素前面加上相應的namespace
如下圖的xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <song:body xmlns:song="http://www.example.org/athena" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.example.org/athena athena.xsd"> 5 <song:id>12</song:id> 6 <song:name>song</song:name> 7 <song:birthday>2002-10-10T12:00:00-05:00</song:birthday> 8 </song:body>