疯狂XML学习笔记(8)---------schema 的简单类型

互联网 17-2-21
经过一番艰苦的学习和研究,终于将XML的schema的基本原理弄通了!

这是多么的痛的领悟呀!

同一个知识点我至少阅读了3遍,终于将此脉穴打通!

使用过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的直接子节点,包括元素声明、属性声明、复杂/简单类型定义、组定义、属性组定义

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”>  <xsd:schema xmlns:xsd=”http://www.php.cn/”  targetNamespace=“http://www.php.cn/“>     // Schema的目标名称空间用属性targetNamespace在根元素上定义。     //Schema的全局成分被放在名称空间http://www.php.cn/里。  </span>

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

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”>  <xsd:schema xmlns:xsd=”http://www.php.cn/”  targetNamespace=“http://www.php.cn/“  elementFormDefault=“qualified“></span>

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

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”>  <xsd:schema xmlns:xsd=”http://www.php.cn/”  targetNamespace=“http://www.php.cn/“  attributeFormDefault=“qualified“></span>

也可以修改属性form的值,使得某些非全局成分不包含在名称空间中。如下:

<xsd:element name=”name” type=”xsd:string” form=”unqualified”/>

(3)targetNamespace

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

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

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

3、XML文档中命名空间

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

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

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”?>  <member_details xmlns=”http://www.php.cn/”>     <name>Tom</name>     <age>12</age>     <sex>male</sex>  </member_details></span>

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

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”?>  <member_details>     <name xmlns=”http://www.php.cn/”>Tom</name>     <age>12</age>     <sex>male</sex>  </member_details></span>

(3)嵌套的命名空间

<span style="font-family: SimSun; font-size: 14px;"><?xml version=”1.0”?>  <member_details xmlns=”http://www.php.cn/”  xmlns:newns=”http://www.php.cn/”>     <name>Tom</name>     <age>12</age>     <newns:sex>male</sex>  </member_details>  //<span style="line-height: 26px;">&nbsp;此例中,除了元素sex被定义在新的名称空间中外,其余的元素仍然使用原来的名称空间。</span></span>

(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 架构文档提供位置信息。

<span style="font-family: SimSun; font-size: 14px;"><p:Person     xmlns:p="http://contoso.com/People"     xmlns:v="http://contoso.com /Vehicles"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation=       "http://contoso.com/People        http://www.php.cn/        http://www.php.cn/        http://www.php.cn/        http://www.php.cn/        http://www.php.cn/">     <name>John</name>     <age>28</age>     <height>59</height>     ....  </p:Person></span>

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

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

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

  <?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 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"。并且下文的讨论将围绕这两个文档展开。

     <?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>  <?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.php.cn/",表明此schema中使用的元素和数据类型来自于"http://www.php.cn/"名称空间(namespace)。它同样指出来自于"http://www.php.cn/"名称空间的元素和数据类型必须使用带"xsd: "前缀。作为名称空间的标识符(在声明中作为元素或属性的前缀),你也可以不使用xsd或xsi。这个 xmlns属性包含了基本的XML schema元素,比如element, attribute, complexType, group, simpleType等。

对于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必须包含这个名称空间:http://www.php.cn/。即此名称空间是由XML模式规范定义的标准名称空间-所有XML模式元素必须属于该名称空间。 此片段:targetNamespace="http://www.php.cn/",表明此schema (note, to, from, heading, body)定义的元素来自于"http://www.php.cn/"名称空间。这个targetNamespace属性表示了该schema所对应的名称空间的URI。也就是说在引用该Schema的其它文档(包括自身文档)中要声明名称空间,其URI应该是targetNamespace的属性值。例如在这里因为要用到note.xsd自己定义的扩展数据类型(note, to, from, heading, body),所以也声明了名称空间xmlns="http://www.php.cn/"。而且该名称空间是默认名称空间(没有前缀)。targetNamespace属性为在模式中显式创建的所有新类型均声明了XML名称空间。

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

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

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

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

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

     <?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>
<?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.php.cn/",该复杂类型存在于"http://www.php.cn/"中,因此应该修改代码如下:

  <?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>
<?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.php.cn/"名称空间则定义了element, attribute, complexType, group, simpleType等元素。

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

以上就是疯狂XML学习笔记(8)---------schema 的简单类型 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:疯狂XML学习笔记(7)-----------XML Schema

相关资讯