XML和Schema命名空间实例详解



XML和Schema命名空间实例详解。

上文把XML和Schema的命名空间的一些相关内容进行了详细介绍,下面通过例子来具体了解:

    例一:重点理解名称空间的相关概念。

下面的例子是一个XML Schema文件,名为”note.xsd”

 

  1. <?xml version=”1.0″?>
  2. <xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
  3. targetNamespace=”http://www.w3schools.com”
  4. xmlns=”http://www.w3schools.com”
  5. elementFormDefault=”qualified”>
  6. <xsd:element name=”note”>
  7.       <xsd:complexType>
  8.          <xsd:sequence>
  9.            <xsd:element name=”to” type=”xs:string”/>
  10.            <xsd:element name=”from” type=”xs:string”/>
  11.            <xsd:element name=”heading” type=”xs:string”/>
  12.            <xsd:element name=”body” type=”xs:string”/>
  13.         </xsd:sequence>
  14.       </xsd:complexType>
  15. </xsd:element>
  16. </xsd:schema>

下面的XML文档和上文给出的XML Schema相关联,名为”note.xml”。并且下文的讨论将围绕这两个文档展开。

 

 

  1. <?xml version=”1.0″?>
  2. <note xmlns=”http://www.w3schools.com”
  3. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  4. xsi:schemaLocation=”http://www.w3schools.com note.xsd”>
  5. <to>Tove</to>
  6. <from>Jani</from>
  7. <heading>Reminder</heading>
  8. <body>Don’t forget me this weekend!</body>
  9. </note>

 

此片段:xmlns:xsd=”http://www.w3.org/2001/XMLSchema“,表明此schema中使用的元素和数据类型来自于”http://www.w3.org/2001/XMLSchema“名称空间(namespace)。它同样指出来自于”http://www.w3.org/2001/XMLSchema“名称空间的元素和数据类型必须使用带”xsd: “前缀。作为名称空间的标识符(在声明中作为元素或属性的前缀),你也可以不使用xsd或xsi。这个 xmlns属性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。
     对于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必须包含这个名称空间:http://www.w3.org/2001/XMLSchema。即此名称空间是由XML模式规范定义的标准名称空间-所有XML模式元素必须属于该名称空间。
此片段:targetNamespace=”http://www.w3schools.com“,表明此schema (note, to, from, heading, body)定义的元素来自于”http://www.w3schools.com“名称空间。这个targetNamespace属性表示了该schema所对应的名称空间的URI。也就是说在引用该Schema的其它文档(包括自身文档)中要声明名称空间,其URI应该是targetNamespace的属性值。例如在这里因为要用到note.xsd自己定义的扩展数据类型(note, to, from, heading, body),所以也声明了名称空间xmlns=”http://www.w3schools.com“。而且该名称空间是默认名称空间(没有前缀)。targetNamespace属性为在模式中显式创建的所有新类型均声明了XML名称空间。

我们再来看由该schema规定的XML文档note.xml的开头将是什么样子:

 

 

  1. <note xmlns=”http://www.w3schools.com”
  2. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  3. xsi:schemaLocation=”http://www.w3schools.com note.xsd”>


其中缺省名称空间声明xmlns=”http://www.w3schools.com“就是和刚刚声明的XML Schema的名称空间相结合来规定该XML文档。(即该文档用到了此名称空间中定义的数据) xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 是任何XML实例文档固有的XML模式实例名称空间,它由XML模式规范定义。而xsi:schemaLocation=”http://www.w3schools.com note.xsd”则规定了该名称空间所对应的schema的位置,即在相同路径的note.xsd文件。

 

     例二:重点理解Schema文档使用自身定义类型

xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,类型等都属于该targetNameSpace,其自身或外部xsd文件使用这些元素,属性等都必须从定义的targetNameSpace中找。修改一下note.xsd,去除默认名称空间的声明,并添加一个复杂类型:

 

  1. <?xml version=”1.0″?>
  2. <xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
  3. targetNamespace=”http://www.w3schools.com”
  4. elementFormDefault=”qualified”>
  5. <xsd:element name=”note”>
  6.       <xsd:complexType>
  7.         <xsd:sequence>
  8.        <xsd:element name=”to” type=”xs:string”/>
  9.        <xsd:element name=”from” type=”xs:string”/>
  10. <xsd:element name=”heading” type=”xs:string”/>
  11.        <xsd:element name=”body” type=”xs:string”/>
  12.        </xsd:sequence>
  13.       </xsd:complexType>
  14. </xsd:element>
  15. <xsd:element name=”Student” type=”stu”/>
  16.   <xsd:complexType name=”stu”>
  17.   <xsd:sequence>
  18.    <xsd:element name=”Name” type=”xs:string”/>
  19.    <xsd:element name=”Class” type=”xs:string”/>
  20.   </xsd:sequence>
  21.  </xsd:complexType>
  22. </xsd:schema>

上述代码中,复杂类型stu是找不到的,因为你定义了一个名称空间”http://www.w3schools.com“,该复杂类型存在于”http://www.w3schools.com“中,因此应该修改代码如下:

 

 

  1. <?xml version=”1.0″?>
  2. <xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”
  3. targetNamespace=”http://www.w3schools.com”
  4. xmlns:student=”http://www.w3schools.com”
  5. elementFormDefault=”qualified”>
  6. <xsd:element name=”note”>
  7.       <xsd:complexType>
  8.         <xsd:sequence>
  9.        <xsd:element name=”to” type=”xs:string”/>
  10.        <xsd:element name=”from” type=”xs:string”/>
  11. <xsd:element name=”heading” type=”xs:string”/>
  12.        <xsd:element name=”body” type=”xs:string”/>
  13.        </xsd:sequence>
  14.       </xsd:complexType>
  15. </xsd:element>
  16. <xsd:element name=”Student” type=”student:stu”/>
  17.   <xsd:complexType name=”stu”>
  18.   <xsd:sequence>
  19.    <xsd:element name=”Name” type=”xs:string”/>
  20.    <xsd:element name=”Class” type=”xs:string”/>
  21.   </xsd:sequence>
  22.  </xsd:complexType>
  23. </xsd:schema>

若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。
通过上面的例子,我们可以很深刻的理解targetNameSpace。targetNamespace定义了Schema定义的新元素与属性的名称空间。而”http://www.w3.org/2001/XMLSchema“名称空间则定义了element, attribute, complexType, group, simpleType等元素。

 

理解了上面的两个例子,Schema的命名空间的内容应该就明了了。