xml模式文档(xml:Schema)详解

XML Schema详解:定义与使用
本文详细介绍了XML Schema的概念,作为XML文档结构的描述工具,XML Schema定义了文档的结构和元素类型。通过示例解释了schemaLocation和noNamespaceSchemaLocation属性的用法,并探讨了如何在XML Schema中声明和使用自定义类型。
部署运行你感兴趣的模型镜像

XML Schema 是基于 XML 的 DTD 替代者。

XML Schema 描述 XML 文档的结构。

XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)。

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

[html]  view plain copy
  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="xsd:string"/>  
  10.            <xsd:element name="from" type="xsd:string"/>  
  11.            <xsd:element name="heading" type="xsd:string"/>  
  12.            <xsd:element name="body" type="xsd:string"/>  
  13.         </xsd:sequence>  
  14.       </xsd:complexType>  
  15. </xsd:element>  
  16. </xsd:schema>  

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

    [html]  view plain copy
    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>  
    note.xsd中,此片段: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模式元素必须属于该名称空间。

  18. 此片段: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文档使用自身定义类型

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

[html]  view plain copy
  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"中,因此应该修改代码如下:

[html]  view plain copy
  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等元素。



xsi:schemaLocation详解

在实例中引用模式文档

XML Schema提供了两个在实例文档中使用的特殊属性,用于指出模式文档的位置。这两个属性是:xsi:schemaLocation和xsi:noNamespaceSchemaLocation,前者用于声明了目标名称空间的模式文档,后者用于没有目标名称空间的模式文档,它们通常在实例文档中使用。

4.5.7.1  xsi:schemaLocation属性

xsi:schemaLocation属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是名称空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配。我们看例4-28。

例4-28  book6.xml

 

<?xml version="1.0" encoding="GB2312"?>
<book xmlns="http://www.sunxin.org/book"   ①
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  ②
xsi:schemaLocation="http://www.sunxin.org/book http://www.sunxin.org/ 
book.xsd">  ③
<title>《Struts 2深入详解》</title>
<author>孙鑫</author>
</book>

① 声明默认的名称空间(http://www.sunxin.org/book)。

② 声明XML Schema实例名称空间(http://www.w3.org/2001/XMLSchema-instance),并将xsi前缀与该名称空间绑定,这样模式处理器就可以识别xsi:schemaLocation属性。XML Schema实例名称空间的前缀通常使用xsi。

③ 使用xsi:schemaLocation属性指定名称空间http://www.sunxin.org/book和模式位置http://www.sunxin.org/book.xsd相关要注意,在这个例子中,book.xsd中声明的目标名称空间要求是http://www.sunxin.org/book

一个可能的模式文档book.xsd如例4-29所示。

例4-29  book.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">

<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:complexType>
</xs:schema>

实际上,xsi:schemaLocation属性的值也可以由多个URI引用对组成,每个URI引用对之间使用空白符分隔。例4-30的实例文档使用了多个名称空间,xsi:schemaLocation属性的值包含了两对URI。

例4-30  books.xml

 

<?xml version="1.0" encoding="GB2312"?>
<books xmlns="http://www.sunxin.org/bks" xmlns:p="http://www.sunxin.org/people"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sunxin.org/bks bks.xsd
http://www.sunxin.org/people people.xsd">
<book>
<title>JSP深入编程</title>
<author>
<p:name>张三</p:name>
<p:title>作家</p:title>
</author>
</book>
<book>
<title>XML从入门到精通</title>
<author>
<p:name>李四</p:name>
<p:title>教师</p:title>
</author>
</book>
</books>

XML Schema推荐标准中指出,xsi:schemaLocation属性可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:schemaLocation属性必须出现在它要验证的任何元素和属性之前。不过一般我们把它放在根元素上

此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:schemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:schemaLocation属性。

xsi:noNamespaceSchemaLocation属性

xsi:noNamespaceSchemaLocation属性用于引用没有目标名称空间的模式文档。与xsi:schemaLocation属性不同的是,xsi:noNamespaceSchemaLocation属性的值是单一的值,只是用于指定模式文档的位置。例4-31显示了在实例文档中xsi:noNamespaceSchema Location属性的使用。

例4-31  book7.xml

 

<?xml version="1.0" encoding="GB2312"?>
<book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="book.xsd" 
isbn="978-7-121-06812-6" >
<title>《Struts 2深入详解》</title>
<author>孙鑫</author>
</book>

与xsi:schemaLocation属性一样,xsi:noNamespaceSchemaLocation属性也可以在实例中的任何元素上使用,而不一定是根元素,不过,xsi:noNamespaceSchemaLocation属性必须出现在它要验证的任何元素和属性之前。不过一般情况下放在根元素下

此外,要注意的是,XML Schema推荐标准并没有要求模式处理器必须要使用xsi:noNamespaceSchemaLocation属性,某些模式处理器可以通过其他的方式来得到模式文档的位置,而忽略xsi:noNamespaceSchemaLocation属性

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值