xml模式(xml:Schema)实验

本文通过实验演示XML Schema(xml:Schema)的使用,强调了目标命名空间(targetNamespace)的重要性。实验中,当XML文档引用schema定义的元素时,必须匹配targetNamespace指定的URI。错误的命名空间会导致验证失败。同时,文章还介绍了如何在schema中定义新的复杂类型并应用于XML文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在前面的xml模式文档(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="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>  

对于的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>  
  10. 验证通过,如果将note.xml中的<to>Tove</to>改为<to1>Tove</to1>则报错,说明验证是起作用的

详解中讲到:   对于任何一个XML Schema定义文档(XSD)都有一个最顶层的schema (XSD)元素。而且该schema (XSD)元素定义必须包含这个名称空间: http://www.w3.org/2001/XMLSchema 。即此名称空间是由XML模式规范定义的标准名称空间-所有XML模式元素必须属于该名称空间。即note.xsd中<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 名称空间是不能更改的(xsd前缀是可以更改的)

验证,将上文note.xsd中该行改为:<xsd:schema xmlns:xsd="http://www.w3.org/2001",
报错:The namespace of element 'schema' must be from the schema namespace.说明详解中将的是对的
将前缀xsd全部改为xs也是验证通过的


详解中:
  1. 此片段: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名称空间。

note.xml文件中默认命名空间为"http://www.w3schools.com",由上文知,它应该是与note.xsd文件的targetNamespace属性值相同。
将note.xml默认命名空间改为"http://www.w3schools.com1",
验证报错:未知的对象note.


xsi:schemaLocation属性的值由一个URI引用对组成,两个URI之间以空白符分隔。第一个URI是名称空间的名字,第二个URI给出模式文档的位置,模式处理器将从这个位置读取模式文档,该模式文档的目标名称空间必须与第一个URI相匹配
验证:(1),将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">  
     
  1. 改为
    1. <note xmlns="http://www.w3schools.com"  
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    3. xsi:schemaLocation="http://www.w3schools1.com note.xsd">  
  2. 验证报错:Schema in note.xsd has a different target namespace from the one specified in the instance document http://www.w3schools1.com



详解中:

重点理解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"中,
验证结果:Type not found in :stu
因此应该修改代码添加一行:<xmlns="http://www.w3schools.com">
然后验证通过。


xml文件中的命名空间是否是任意的呢,例如我把上文中的"http://www.w3schools.com"改为"http://www.w3schools.com1"可以吗。
理论上来说是可以的,命名空间只是一个标识。
将上文中的"http://www.w3schools.com"都改为"http://www.w3schools.com1",验证一下
验证结果:通过

  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="uri"可以是任意的,但xmlns:xsi=”uri"的uri是否是任意的,验证一下,将
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 改为xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance1"
验证结果:no DTD found !验证是不通过的。
查资料看了下,
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"中xsi的意思是 :
本xml文件中要用到某些来自xsi代表的“http://www.w3.org/2001/XMLSchema-instance”这个命名空间的元素 
比如用来引入无命名空间schema文件的noNamespaceSchemaLocation="XXX";
以及引入自带命名空间的schema文件的schemaLocation="XXX"这些元素。
这里使用了schemaLocation元素,所以必须引入“http://www.w3.org/2001/XMLSchema-instance”命名空间
这些元素是包含在“http://www.w3.org/2001/XMLSchema-instance”命名空间中的,所有的xml文件只要引用这些元素 就要引入这个命名空间,前缀可以修改,但命名空间不能改。


总结起来
 

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="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. 元素:schema对应的命名空间固定:"http://www.w3.org/2001/XMLSchema"
对于的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>

xml:xsi对应的命名空间固定: "http://www.w3.org/2001/XMLSchema-instance"

当然,如果不要求xml验证,只要求格式良好,则命名空间没什么限制。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值