天天看点

XML schema的命名空间

schema.xsd

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

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

  targetNamespace="http://www.example.org/schema01"

  xmlns:tns="http://www.example.org/schema01"

  elementFormDefault="qualified">

 <!--xmlns:tns="http://www.example.org/schema01"

 此处的名称和自己的命名空间的名称一致,但是增加了tns的前缀

 此时,如果要想引用当前文件所创建的类型,需加上tns前缀

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

 表示的是schema的默认命名空间  不能改动 但是可以为他定义命名空间 如:xsd

 命名空间加xsd

 targetNamespace

 自己这个文档的命名空间,可以方便其他xml或者sechma文件引用

 -->

 <xsd:element name="user">

  <xsd:complexType>

   <xsd:sequence>

    <xsd:element name="id" type="int"/>

    <xsd:element name="userName" type="string"/>

    <xsd:element name="bort" type="date"/>

   </xsd:sequence>

  </xsd:complexType>

 </xsd:element>

</xsd:schema>

schema.xml

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

 <user

   xmlns="http://www.example.org/schema01"

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

   xsi:schemaLocation="http://www.example.org/schema01">

   <id>1</id>

   <userName>张三</userName>

   <bort>2012-12-12</bort>

   <!--

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

    创建了一个可以引入其他schema文件的命名空间

    xmlns="http://www.example.org/schema01"

    xsi:schemaLocation="http://www.example.org/schema01"

    引入其他命名空间

    特别注意:如果要在Eclipse中使用命名空间的引入

    需要为xml增加xml的category

   -->

    </user>

schema02.xml

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

 <user

   xmlns="http://www.example.org/schema01"

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

   xsi:xsi:noNamespaceSchemaLocation="schema01.xml"   

   >

   <id>1</id>

   <userName>张三</userName>

   <bort>2012-12-12</bort>

   <!--

    xsi:xsi:noNamespaceSchemaLocation="schema01.xml"

    可以引入其他的xml文件

    如果不希望通过命名空间引入 可以以使用文件路径引入 

   -->

    </user>