天天看點

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>