XML Tutorial/Namespace/targetNamespace

Материал из Web эксперт
Перейти к: навигация, поиск

Defining the XML Schema Namespace as the Default

   <source lang="xml">

<?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"

 targetNamespace="http://www.wbex.ru/namespaces/pub"
 xmlns:pub="http://www.wbex.ru/namespaces/pub"
 elementFormDefault="qualified">
 <include schemaLocation="book.xsd"/>
 <element name="products">
  <complexType>
   <sequence>
    <element name="publications">
     <complexType>
      <sequence>
       <element ref="pub:book" maxOccurs="unbounded"/>
      </sequence>
     </complexType>
    </element>
   </sequence>
  </complexType>
 </element>

</schema></source>


Document Models with a Namespace

   <source lang="xml">

File: Schema.xsd <xsd:schema

 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.wbex.ru/namespaces/employee"
 xmlns="http://www.wbex.ru/namespaces/employee">
 <xsd:element name="employee">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="email" type="xsd:string"/>
    <xsd:element name="hireDate" type="xsd:string"/>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>

</xsd:schema> File: Data.xml <?xml version="1.0"?> <em:employee

 xmlns:em="http://www.wbex.ru/namespaces/employee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/namespaces/employee employee.xsd">
 <name>Joe Smith</name>
 <email>a@a.ru</email>
 <hireDate>2008-10-29</hireDate>

</em:employee></source>


Referencing Components with Namespaces

   <source lang="xml">

<?xml version="1.0" ?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/10/XMLSchema"

 targetNamespace="http://www.wbex.ru"
 xmlns:end="http://www.wbex.ru">
 <xsd:element name="employees">
   <xsd:complexType>
     <xsd:sequence>
       <xsd:element name="animal" type="end:animalType"
         maxOccurs="unbounded" />
     </xsd:sequence>
   </xsd:complexType>
 </xsd:element>
 <xsd:complexType name="animalType">
   <xsd:sequence>
     <xsd:element name="name" type="end:nameType" minOccurs="2" />
     <xsd:element name="projects" type="end:projectsType" />
     <xsd:element name="weight" type="xsd:string" minOccurs="0"
       maxOccurs="1" />
   </xsd:sequence>
 </xsd:complexType>

</xsd:schema></source>


Specify a target namespace

   <source lang="xml">

<?xml version="1.0" ?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/10/XMLSchema"

 targetNamespace="http://www.wbex.ru/">
 <xsd:element name="employees">
   <xsd:complexType>
     <xsd:sequence>
       <xsd:element name="animal" type="animalType"
         maxOccurs="unbounded" />
     </xsd:sequence>
   </xsd:complexType>
 </xsd:element>
 

</xsd:schema></source>


Use targetNamespace as the Default Namespace

   <source lang="xml">

<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 targetNamespace="http://www.wbex.ru/namespaces/pub"
 xmlns="http://www.wbex.ru/namespaces/pub"
 elementFormDefault="qualified">
 <xsd:include schemaLocation="book.xsd"/>
 <xsd:element name="products">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="publications">
     <xsd:complexType>
      <xsd:sequence>
       <xsd:element ref="book" maxOccurs="unbounded"/>
      </xsd:sequence>
     </xsd:complexType>
    </xsd:element>
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>

</xsd:schema></source>