欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

XML Schema 模式与命名空间 有大用

image.png

XMLSchema 模式与命名空间 

XMLStruts 

模式与名称空间

DTD 的问题就在于和名称空间不相关,当你为一个 XML 文档使用了名称空间后,文档的 DTD 就需要重写。XML Schema 对名称空间提供了很好的支持。在编写模式文档时,你可以指定该模式文档是为哪一个名称空间声明和定义组件,这个名称空间称为目标名称空间。

4.5.6.1  目标名称空间

目标名称空间使用 xs:schema 元素的 targetNamespace 属性来指定,如例 4-16 所示。

例 4-16  book3.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.sunxin.org/book"  ①

xmlns:book="http://www.sunxin.org/book">   ②

<xs:element name="book" type="book:bookType"/>  ③

 

<xs:complexType name="bookType">     ④

<xs:sequence>

<xs:element name="title" type="xs:string"/>

<xs:element name="author" type="xs:string"/>

</xs:sequence>

<xs:attribute name="isbn" type="xs:token"/>

</xs:complexType>

</xs:schema>

① 我们在 xs:schema 元素上使用 targetNamespace 属性指定目标名称空间为:http://www.sunxin.org/book,即指明了在该模式文档中声明的元素、定义的类型都属于 http://www.sunxin.org/book 名称空间。

② 声明名称空间 http://www.sunxin.org/book,并为该名称空间绑定前缀 book,使用该前缀来引用 http://www.sunxin.org/book 名称空间中的类型。

③ 由于指定了目标名称空间,文档中定义的类型都属于目标名称空间,因此在引用类型时,需要加上 book 前缀。

④ 在指定类型名时,不需要加上任何前缀,当使用了 targetNamespace 属性后,在模式文档中定义的任何类型都属于目标名称空间。

在模式文档中,我们很容易就能区分出不同名称空间中的元素和类型,带有 xs 前缀的元素和类型属于 http://www.w3.org/2001/XMLSchema 名称空间,而其他的元素和类型则属于目标名称空间。需要注意的是,只有模式文档中的全局元素和全局属性才属于目标名称空间,在本例中,声明的全局元素 name 属于目标名称空间,而局部元素 title 和 author 则不属于目标名称空间。

为了简化模式文档对目标名称空间中的元素和类型的引用,我们可以利用默认名称空间来简化引用,例 4-16 如果使用了默认名称空间,可以修改为如例 4-17 所示。

例 4-17  使用了默认名称空间的 book4.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.sunxin.org/book"

xmlns="http://www.sunxin.org/book">

<xs:element name="book" type="bookType"/>

 

<xs:complexType name="bookType">

<xs:sequence>

<xs:element name="title" type="xs:string"/>

<xs:element name="author" type="xs:string"/>

</xs:sequence>

<xs:attribute name="isbn" type="xs:token"/>

</xs:complexType>

</xs:schema>

我们在 xs:schema 元素上声明了默认名称空间 http://www.sunxin.org/book,因此在声明 book 元素时,引用 bookType 类型就无须再添加前缀了。

符合上述模式文档的 XML 实例文档如例 4-18 所示。

例 4-18  book.xml

<?xml version="1.0" encoding="GB2312"?>

<book:book xmlns:book="http://www.sunxin.org/book" isbn="978-7-121-06812-6"> 

<title>《Struts 2 深入详解》</title>

<author> 孙鑫 </author>

</book:book>

在例 4-17 的模式文档中,在 bookType 类型定义中声明的元素 title 和 author 是局部元素,没有被目标名称空间所限定,因此在实例文档中,对 title 和 author 元素不要添加 book 前缀。局部元素和属性的限定

在例 4-17 中,局部元素 title 和 author 没有被目标名称空间所限定,如果要限定局部元素和属性,可以通过 xs:schema 元素的 elementFormDefault 和 attibuteFormDefault 属性来设置。

为了指定模式文档中局部声明的元素必须被限定,可以将 xs:schema 元素的 elementFormDefault 属性的值设为 "qualified"。

将例 4-17 中的 xs:schema 元素修改为如例 4-19 所示。

例 4-19  book5.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.sunxin.org/book"

xmlns="http://www.sunxin.org/book"

elementFormDefault="qualified">

<xs:element name="book" type="bookType"/>

 

<xs:complexType name="bookType">

<xs:sequence>

<xs:element name="title" type="xs:string"/>

<xs:element name="author" type="xs:string"/>

</xs:sequence>

<xs:attribute name="isbn" type="xs:token"/>

</xs:complexType>

</xs:schema>

elementFormDefault 属性的默认值是 unqualified,即对局部声明的元素不加限定。

我们修改了模式文档,相应的就要修改实例文档,如例 4-20 所示。

例 4-20  book2.xml

<?xml version="1.0" encoding="GB2312"?>

<book:book xmlns:book="http://www.sunxin.org/book" isbn="978-7-121-06812-6">

<book:title>《Struts 2 深入详解》</book:title>

<book:author> 孙鑫 </book:author>

</book:book>

在例 4-20 的实例文档中,所有的元素都属于同一个名称空间,因此我们可以声明一个默认的名称空间,来省略前缀的使用,如例 4-21 所示。

例 4-21  book3.xml

<?xml version="1.0" encoding="GB2312"?>

<book xmlns="http://www.sunxin.org/book" isbn="978-7-121-06812-6">

<title>《Struts 2 深入详解》</title>

<author> 孙鑫 </author>

</book>

属性的限定和元素的限定是类似的。如果属性被声明为全局属性或者 xs:schema 元素的 attibuteFormDefault 属性被设置成 "qualified" 的话,那么属性就必须被限定,在实例文档中将以带名称空间前缀的方式出现。实际上,需要限定的属性必须明确的加上名称空间前缀,因为 XML 名称空间推荐标准中并没有给出关于属性的默认名称空间的机制,一个没有前缀的属性将不在任何的名称空间中。属性是附属于元素的,对属性添加名称空间不是很有必要,因此在多数应用中都没有给属性添加限定。

使用了 attibuteFormDefault 属性的模式文档,如例 4-22 所示。

例 4-22  book6.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns="http://www.sunxin.org/book"

targetNamespace="http://www.sunxin.org/book"

elementFormDefault="qualified"

attributeFormDefault="qualified">

 <xs:element name="book" type="bookType"/>

<xs:complexType name="bookType">

<xs:sequence>

<xs:element name="title" type="xs:string"/>

<xs:element name="author" type="xs:string"/>

</xs:sequence>

<xs:attribute name="isbn" type="xs:token"/>

</xs:complexType>

</xs:schema>

attributeFormDefault 属性的默认值是 unqualified,即对局部声明的属性不加限定。

一个符合模式(例 4-22)的实例文档如例 4-23 所示。

例 4-23  book4.xml

<?xml version="1.0" encoding="GB2312"?>

<book:book xmlns:book="http://www.sunxin.org/book" book:isbn="978-7-121-06812-6">

<book:title>《Struts 2 深入详解》</book:title>

<book:author> 孙鑫 </book:author>

</book:book>

除了使用 xs:schema 元素的 elementFormDefault 和 attibuteFormDefault 属性来限定局部元素和局部属性外,我们还可以在 xs:element 和 xs:attribute 元素上分别使用 form 属性来限定局部元素和局部属性。我们看例 4-24。

例 4-24  book7.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"

xmlns="http://www.sunxin.org/book"

targetNamespace="http://www.sunxin.org/book">

<xs:element name="book" type="bookType"/>

<xs:complexType name="bookType">

<xs:sequence>

<xs:element name="title" type="xs:string" form="qualified"/>

<xs:element name="author" type="xs:string" form="qualified"/>

</xs:sequence>

<xs:attribute name="isbn" type="xs:token" form="qualified"/>

</xs:complexType>

</xs:schema>

例 4-24 所示的模式文档和例 4-22 所示的模式文档的效果是一样的。 

要注意的是,在 xs:schema 元素上使用 elementFormDefault 和 attibuteFormDefault 属性,影响的是整个模式文档中的局部元素和局部属性,而在 xs:element 和 xs:attribute 元素上使用 form 属性,影响的是当前的局部元素和局部属性。

未声明的目标名称空间

对于很多不使用名称空间的 XML 文档,你需要使用没有声明目标名称空间的模式文档。我们看例 4-25。

例 4-25  book8.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="book" type="bookType"/>

<xs:complexType name="bookType">

<xs:sequence>

<xs:element name="title" type="xs:string"/>

<xs:element name="author" type="xs:string"/>

</xs:sequence>

<xs:attribute name="isbn" type="xs:token"/>

</xs:complexType>

</xs:schema>

在这个模式文档中我们没有声明目标名称空间,因此定义的 bookType 类型和声明的 book 元素都是没有名称空间限定的。在引用类型或元素时,不需要添加任何的名称空间前缀。在本例中,声明 book 元素时,type 属性直接引用了 bookType 类型。与之相对的是,例 4-25 的模式文档中使用的所有 XML Schema 元素和类型都是通过与 XML Schema 名称空间相关联的名称空间前缀 "xs" 来明确进行限定的。

一个符合模式(例 4-25)的实例文档如例 4-26 所示。

例 4-26  book5.xml

<?xml version="1.0" encoding="GB2312"?>

<book isbn="978-7-121-06812-6">

<title>《Struts 2 深入详解》</title>

<author> 孙鑫 </author>

</book>

要注意的是,在没有声明目标名称空间的模式文档中,强烈建议对所有的 XML Schema 元素和类型使用一个和 XML Schema 名称空间相关联的名称空间前缀(如 xs 或 xsd)来明确实施限定。如果你对 XML Schema 的元素和类型使用了默认名称空间,那么对 XML Schema 类型的引用也许不能和对用户自定义类型的引用相区分,从而导致模式文档出现错误。例 4-27 错误的使用了默认名称空间,使得对自定义类型的引用和对 XML Schema 类型的引用不能区分。

例 4-27  book9.xsd

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema">

<element name="book" type="bookType"/>

<complexType name="bookType">

<sequence>

<element name="title" type="string"/>

<element name="author" type="string"/>

</sequence>

<attribute name="isbn" type="token"/>

</complexType>

</schema>

在 XMLSpy 中验证这个模式文档时,将提示如下的错误信息:

'bookType' must refer to an existing simple or complex type.

这是因为模式验证器认为对 bookType 类型的引用(没有前缀的引用),引用的是默认名称空间(即 http://www.w3.org/2001/XMLSchema 名称空间)中的类型,而 XML Schema 名称空间中并没有 bookType 这种类型,因此就报出了错误。

 

 

 

 

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

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

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

[html] view plain copy print?

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 version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.w3schools.com"

xmlns="http://www.w3schools.com"

elementFormDefault="qualified">

<xsd:element name="note">

      <xsd:complexType>

         <xsd:sequence>

           <xsd:element name="to" type="xs:string"/>

           <xsd:element name="from" type="xs:string"/>

           <xsd:element name="heading" type="xs:string"/>

           <xsd:element name="body" type="xs:string"/>

        </xsd:sequence>

      </xsd:complexType>

</xsd:element>

</xsd:schema>


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

[html] view plain copy print?

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>  

<?xml version="1.0"?>

<note xmlns="http://www.w3schools.com"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.w3schools.com note.xsd">

<to>Tove</to>

<from>Jani</from>

<heading>Reminder</heading>

<body>Don't forget me this weekend!</body>

</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 的开头将是什么样子:

[html] view plain copy print?

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,去除默认名称空间的声明,并添加一个复杂类型:

[html] view plain copy print?

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>  

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.w3schools.com"

elementFormDefault="qualified">

<xsd:element name="note">

      <xsd:complexType>

        <xsd:sequence>

       <xsd:element name="to" type="xs:string"/>

       <xsd:element name="from" type="xs:string"/>

<xsd:element name="heading" type="xs:string"/>

       <xsd:element name="body" type="xs:string"/>

       </xsd:sequence>

      </xsd:complexType>

</xsd:element>

<xsd:element name="Student" type="stu"/>

  <xsd:complexType name="stu">

  <xsd:sequence>

   <xsd:element name="Name" type="xs:string"/>

   <xsd:element name="Class" type="xs:string"/>

  </xsd:sequence>

 </xsd:complexType>

</xsd:schema>


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

[html] view plain copy print?

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>  

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.w3schools.com"

xmlns:student="http://www.w3schools.com"

elementFormDefault="qualified">

<xsd:element name="note">

      <xsd:complexType>

        <xsd:sequence>

       <xsd:element name="to" type="xs:string"/>

       <xsd:element name="from" type="xs:string"/>

<xsd:element name="heading" type="xs:string"/>

       <xsd:element name="body" type="xs:string"/>

       </xsd:sequence>

      </xsd:complexType>

</xsd:element>

<xsd:element name="Student" type="student:stu"/>

  <xsd:complexType name="stu">

  <xsd:sequence>

   <xsd:element name="Name" type="xs:string"/>

   <xsd:element name="Class" type="xs:string"/>

  </xsd:sequence>

 </xsd:complexType>

</xsd:schema>


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

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

初学 schema---schema 命名空间理解

2013-11-26 19:48 572 人阅读 评论 (0) 收藏 举报 

本文章已收录于: 

版权声明:本文为博主原创文章,未经博主允许不得转载。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" <!-- schema 的命名空间 -->
         targetNamespace="http://www.example.org/01"  <!-- 在其他文件中要引用该 schema 文件就要在 schemaLocation 中引入它 -->
         xmlns:tns="http://www.example.org/01" <!-- 在自己中引用自己定义的元素时使用的 命名空间 -->
         elementFormDefault="qualified">
         
         <complexType name="mytype"></complexType>
         
         <element name="tt" type="tns:mytype"></element> <!-- 在自己中引用自己定义的元素时使用的 命名空间 -->

</schema>

 

转载于:https://my.oschina.net/u/3560874/blog/995701

来自  https://blog.csdn.net/chongzhaxue1316/article/details/101006567  






XML和Schema命名空间详解

来源:https://blog.csdn.net/wanghuan203/article/details/9204337

XML和Schema具有无关平台,技术厂商,简单,规范统一等特点,极具开放性,所以使用极为广泛,而且使用简单,在XML和Schema和,个人认为比较不好理解的一点是其命名空间问题,在这篇博客里详细进行理解。

    名称空间是W3C推荐标准提供的一种统一命名XML文档中的元素和属性的机制。使用名称空间可以明确标识和组合XML文档中来自不同标记词汇表的元素和属性,避免了名称之间的冲突。

    使用过DTD的人应该知道,命名冲突是DTD的一大问题,而Schema里引入了命名空间的概念,就很好的解决了这个问题。具体来看:

 

    1、声明名称空间

    名称空间声明的一般形式为:第一部分是一个关键字xmlns:,第二部分是名称空间的前缀,第三部分是一个等号,第四部分是双引号,将第五部分的名称空间标识URI包括起来。需要注意的是,名称空间的前缀不能为xml,因为在XML中这个字符串是保留作特殊用途的。例:
     xmlns:tns="http://www.whtest.com/"    //其中tns为前缀。
     还可以隐式声明名称空间,即省略掉冒号和名称空间前缀。例:
     xmlns="http://www.whtest.com/"   //注意在一个文档中只能有一个隐式声明的命名空间

 

    2、Schema中的命名空间:

    (1)Schema中的全局成分

     全局成分指的是元素xsd:schema的直接子节点,包括元素声明、属性声明、复杂/简单类型定义、组定义、属性组定义。

 

  1.  

    <?xml version=”1.0”>
  2.  

    <xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema
  3.  

    targetNamespace=“http://www.test.com/ns/ns_test“>
  4.  

    // Schema的目标名称空间用属性targetNamespace在根元素上定义。
  5.  

    //Schema的全局成分被放在名称空间http://www.test.com/ns/ns_test里。
  6.   

    (2)Schema中的非全局成分

     有时希望将非全局成分定义在目标空间中去,可使用下面方法。

 

 

  1.  

    <?xml version=”1.0”>
  2.  

    <xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema
  3.  

    targetNamespace=“http://www.test.com/ns/ns_test
  4.  

    elementFormDefault=“qualified“>

     属性elementFormDefault的默认值是unqualified,也就是规定了只有全局成分才被定义在目标名称空间中。将elementFormDefault的值赋为qualified,使得目标名称空间包含非全局的元素定义。同样,使属性attributeFormDefault的值赋为qualified,可使得目标名称空间包含非全局属性定义。如下:

  1.  

    <?xml version=”1.0”>
  2.  

    <xsd:schema xmlns:xsd=”http://www.w3.org/XML_Schema
  3.  

    targetNamespace=“http://www.test.com/ns/ns_test
  4.  

    attributeFormDefault=“qualified“>

          也可以修改属性form的值,使得某些非全局成分不包含在名称空间中。如下:
<xsd:element name=”name” type=”xsd:string” form=”unqualified”/>

 

     (3)targetNamespace

     xsd文件中定义了一个targetNameSpace后,其内部定义的元素,属性,类型等都属于该targetNameSpace,其自身或外部xsd文件使用这些元素,属性等都必须从定义的targetNameSpace中找。

     targetNamespace定义了Schema定义的新元素与属性的名称空间。而"http://www.w3.org/2001/XMLSchema"名称空间则定义了element, attribute, complexType, group, simpleType等元素。

     若自身并不使用重用组件,仅供外部使用的话,则只定义targetNameSpace就可以,不用指定别名。     

 

     3、XML文档中命名空间

      在XML中,名称空间的使用涉及范畴的概念,范畴即名称空间的覆盖范围,它指的是哪些元素和属性在该名称空间中,哪些不在该名称空间中。名称空间既可以限定整个XML文档,也可以只针对XML文档中的一部分。

     (1).名称空间限定整个XML文档

 

  1.  

    <?xml version=”1.0”?>
  2.  

    <member_details xmlns=http://www.testns.com/ns.xsd”>
  3.  

    <name>Tom</name>
  4.  

    <age>12</age>
  5.  

    <sex>male</sex>
  6.  

    </member_details>

 

     (2)名称空间只针对XML文档中的一部分

 

 

  1.  

    <?xml version=”1.0”?>
  2.  

    <member_details>
  3.  

    <name xmlns=http://www.testns.com/ns.xsd”>Tom</name>
  4.  

    <age>12</age>
  5.  

    <sex>male</sex>
  6.  

    </member_details>

 

     (3)嵌套的命名空间

 

 

  1.  

    <?xml version=”1.0”?>
  2.  

    <member_details xmlns=http://www.testns.com/ns.xsd
  3.  

    xmlns:newns=http://www.testns/newns.xsd”>
  4.  

    <name>Tom</name>
  5.  

    <age>12</age>
  6.  

    <newns:sex>male</sex>
  7.  

    </member_details>
  8.  

    // 此例中,除了元素sex被定义在新的名称空间中外,其余的元素仍然使用原来的名称空间。

 

     (4)schemaLocation

 

    schemaLocation 属性引用具有目标名称空间的 XML 架构文档(.xsd)。该xml文件中用到的所有新创的元素、属性等的.xsd文件都必须在这里声明。
<xsi:schemaLocation="list of anyURI" >

    其中的anyURI是一个架构位置,该架构包含限定的(具有名称空间的架构)架构构造。每一对中的第一个 URI 引用是名称空间名称,第二个则是描述名称空间的架构的位置。

将具有目标名称空间的架构文档与实例文档相关联。可以列出多对 URI 引用,每一对都有不同的名称空间名称部分。
    根据万维网联合会 (W3C) XML 架构建议,XML 实例文档可以同时指定 xsi:schemaLocation 和 xsi:noNamespaceSchemaLocation 属性。此外,还可以多次列出同一个命名空间。

    以下示例显示如何使用 xsi:schemaLocation 属性为多个 XML 架构文档提供位置信息。

 

  1.  

    <p:Person
  2.  

  3.  

    xmlns:v="http://contoso.com /Vehicles"
  4.  

  5.  

    xsi:schemaLocation=
  6.  

  7.  

  8.  

  9.  

  10.  

  11.  

  12.  

    <name>John</name>
  13.  

    <age>28</age>
  14.  

    <height>59</height>
  15.  

    ....
  16.  

    </p:Person>

 

   (5)noNamespaceSchemaLocation 

 

    noNamespaceSchemaLocation 属性引用没有目标名称空间的 XML 架构文档。
    <xsi:noNamespaceSchemaLocation="anyURI" >

与SchemaLocation相同anyURI是一个架构位置,该架构包含非限定的(没有名称空间的架构)架构构造。

    不要求 XML 架构有名称空间。若要为没有目标名称空间的 XML 架构指定位置,请使用 noNamespaceSchemaLocation 属性。此属性中引用的 XML 架构不能有目标名称空间。因为此属性不接受 URL 列表,所以只能指定一个架构位置。
    根据万维网联合会 (W3C) XML 架构建议,XML 实例文档可以同时指定 xsi:schemaLocation 和   xsi:noNamespaceSchemaLocation 属性。

    以下示例显示如何对包含非限定元素的实例文档使用 xsi:noNamespaceSchemaLocation 属性。

 

  1.  

    <?xml version="1.0" encoding="UTF-8"?>
  2.  

    <book:books xmlns:book="http://www.example.org/02"
  3.  

  4.  

    xsi:noNamespaceSchemaLocation="02.xsd">
  5.  

    <book:book id="1">
  6.  

    <book:title>Java in action</book:title>
  7.  

    <book:content>Java is good</book:content>
  8.  

    <book:author>Bruce</book:author>
  9.  

    </book:book>
  10.  

    </book:books>


     通过上边的分析,我们可以看到,XML和Schema的命名空间标签使用格式是相同的(这也是Schema相对与DTD的优势),但XML和Schema都有各自的独特的属性,这也是由他们不同的功能决定的,Schema主要给XML提供服务,所以会规定好targetNameSpace来声明命名空间的名字,而XML需要使用schema的服务,所以需要SchemaLocation来声明使用的命名空间。

 

 

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

 

    下面的例子是一个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.  

  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.  

  3.  

  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.  

  2.  

  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等元素。

来自  https://www.cnblogs.com/zzdbullet/p/9480766.html


普通分类: