我对 xmlschema 的 targetNamespace 、xmlns、include、import的理解

使用xml文档

以下的解释都是基于www.w3school.com.cn文档的理解如果有什么不太周全的地方还往海涵

<?xml version="1.0" encoding="gb2312"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.com test2.xsd" xmlns="http://www.books.com">

<!--使用test2.xsd对xml验证(主要验证是元素或属性类型),但是xml书写的格式必须基于www.w3.org/2001/XMLSchema的格式规范-->
 <book_type>Cisco</book_type>
 <book_type>XML</book_type>
 <book_type>PS</book_type>
 <book_info id="001" type="XML">
  <name>XML学习天下</name>
  <author>张三</author>
  <author>李四</author>
  <price>88.50</price>
 </book_info>
 <book_info id="002" type="PS">
  <name>PS精通</name>
  <author>赵五</author>
  <price>68.00</price>
 </book_info>
 <book_info id="003" type="Ciso">
  <name>CCNP精通</name>
  <author>张三</author>
  <author>李四</author>
  <author>赵五</author>
  <price>78.00</price>
 </book_info>
</books>

 

使用xsd文档 保存为books2.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.com" xmlns="http://www.books.com"  elementFormDefault="qualified" attributeFormDefault="unqualified">

 

<!--targetNamespace是定义当前的文档的命名空间,xmlns这个是确定当前所使用的命名空间(你也可以给你自己定义的类型添加名字例如:xmlns:a=http://www.books.com ,当你再次定义你元素或属性内容时就要在前面加上"a:xxx")

 

<!--记住xmlns:xs=http://www.w3.org/2001/XMLSchema和xmlns:a=http://www.books.com有所不同前者是w3c定义好的后者是你自己为你所要的元素或属性添加的扩展-->
 <xs:simpleType name="book_value">
  <xs:restriction base="xs:string">
   <xs:enumeration value="Cisco"/>
   <xs:enumeration value="XML"/>
   <xs:enumeration value="PS"/>
  </xs:restriction>
 </xs:simpleType>
</xs:schema>

注意:用于import时将http://www.books.com改为http://www.book.com

 

首先是include的理解 保存为test2.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.com" xmlns:ab="http://www.books.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:include schemaLocation="books2.xsd"/>
 <xs:element name="books">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="book_type" type="ab:book_value" maxOccurs="unbounded"/>
    <xs:element name="book_info" maxOccurs="unbounded">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="name" type="xs:string"/>
       <xs:element name="author" type="xs:string" maxOccurs="unbounded"/>
       <xs:element name="price" type="xs:decimal"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:integer" use="required"/>
      <xs:attribute name="type" type="xs:string" use="required"/>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>


  <xs:key name="keybt">  <!--这个是约束要求book_type元素在指定的book_type范围内应是唯一,不为零且始终存在-->
   <xs:selector xpath="book_type"/>
   <xs:field xpath="."/>
  </xs:key>


  <xs:keyref name="keyBt" refer="ab:keybt"><!--这个约束要求type属性的值必须存在(key中的book_type值的集合内)可以重复出显-->
   <xs:selector xpath="book_info"/>
   <xs:field xpath="@type"/>
  </xs:keyref>


  <xs:unique name="NameUnique"><!--这个约束要求name元素的值在整个book_info中必须是唯一或为零-->
   <xs:selector xpath="book_info"/>
   <xs:field xpath="name"/>
  </xs:unique>


  <xs:unique name="idUnique">
   <xs:selector xpath="book_info"/><!--这个约束要求id属性的值在整个book_info中必须是唯一或为零-->
   <xs:field xpath="@id"/>
  </xs:unique>
 </xs:element>
</xs:schema>

 

对于import的理解 保存为test2.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.com" xmlns:ac="http://www.books.com" xmlns:ab="http://www.book.com" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:import namespace="http://www.book.com" schemaLocation="books2.xsd"/>
 <xs:element name="books">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="book_type" type="ab:book_value" maxOccurs="unbounded"/>
    <xs:element name="book_info" maxOccurs="unbounded">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="name" type="xs:string"/>
       <xs:element name="author" type="xs:string" maxOccurs="unbounded"/>
       <xs:element name="price" type="xs:decimal"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:integer" use="required"/>
      <xs:attribute name="type" type="xs:string" use="required"/>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
  <xs:key name="keybt">
   <xs:selector xpath="book_type"/>
   <xs:field xpath="."/>
  </xs:key>
  <xs:keyref name="keyBt" refer="ac:keybt">
   <xs:selector xpath="book_info"/>
   <xs:field xpath="@type"/>
  </xs:keyref>
  <xs:unique name="NameUnique">
   <xs:selector xpath="book_info"/>
   <xs:field xpath="name"/>
  </xs:unique>
  <xs:unique name="idUnique">
   <xs:selector xpath="book_info"/>
   <xs:field xpath="@id"/>
  </xs:unique>
 </xs:element>
</xs:schema>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值