XML/XML Schema/targetNamespace

Материал из Web эксперт
Версия от 11:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Creating Vocabularies that Belong to a Namespace

   <source lang="xml">

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

          targetNamespace = "http://www.wbex.ru/Customers"
          xmlns = "http://www.wbex.ru/Customers">
  
  <xs:element name = "Customer" >
     <xs:complexType>
        <xs:sequence>
           <xs:element name = "FirstName" type = "xs:string" />
           <xs:element name = "MiddleInitial" type = "xs:string" />
           <xs:element ref = "LastName" />
        </xs:sequence>
     </xs:complexType>
  </xs:element>
  
  <xs:element name = "LastName" type = "xs:string" />
  

</xs:schema> File: Data.xml <?xml version = "1.0" ?> <cust:Customer xmlns:cust = "http://www.wbex.ru/Customers"

              xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation = "Schema.xsd">
  <FirstName>first</FirstName>
  <MiddleInitial>middle</MiddleInitial>
  <cust:LastName>last</cust:LastName>

</cust:Customer>

</source>
   
  


Creating Vocabularies that Do Not Belong to a Namespace

   <source lang="xml">

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

  <xs:element name = "Customer" >
     <xs:complexType>
        <xs:sequence>
           <xs:element name = "FirstName" type = "xs:string" />
           <xs:element name = "MiddleInitial" type = "xs:string" />
           <xs:element name = "LastName" type = "xs:string" />
        </xs:sequence>
     </xs:complexType>
  </xs:element>
  

</xs:schema> A conforming instance document would look something like this: <?xml version = "1.0" ?> <Customer xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"

         xsi:noNamespaceSchemaLocation = "Schema.xsd">
  
  <FirstName>first</FirstName>
  <MiddleInitial>middle</MiddleInitial>
  <LastName>last</LastName>

</Customer>

</source>
   
  


Looking Behind Namespace Qualification

   <source lang="xml">

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

          targetNamespace = "http://www.wbex.ru/Customers"
          xmlns = "http://www.wbex.ru/Customers">
  
  <xs:element name = "Customer" type = "CustomerType" />
  <xs:complexType name = "CustomerType">
     <xs:sequence>
        <xs:element name = "FirstName" type = "xs:string" />
        <xs:element name = "MiddleInitial" type = "xs:string" />
        <xs:element ref = "LastName" />
     </xs:sequence>
     <xs:attribute name = "clubCardMember" type = "xs:boolean" />
     <xs:attribute ref = "customerID" />
  </xs:complexType>
  <xs:element name = "LastName" type = "xs:string" />
  <xs:attribute name = "customerID" type = "xs:integer" />
  

</xs:schema> File: Data.xml <?xml version = "1.0" ?> <cust:Customer xmlns:cust = "http://www.wbex.ru/Customers"

              clubCardMember = "true" 
              cust:customerID = "24427">
  <FirstName>first</FirstName>
  <MiddleInitial>middle</MiddleInitial>
  <cust:LastName>last</cust:LastName>

</cust:Customer>

</source>
   
  


Set targetNamespace

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <employees

 xmlns="http://www.wbex.ru/employees"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.wbex.ru/employees Schema.xsd"
 source="from where"
 version="1.0">
 <employee person="personName" tags="tagName">
   <name>
     <first>first</first>
     <middle>middle</middle>
     <last>last</last>
   </name>
   <location>
     <latitude>1</latitude>
     <longitude>1</longitude>
     <address>USA</address>
   </location>
   <phone>12345678</phone>
   <knows employees="name of employees"/>
   <description>description</description>
 </employee>

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

 xmlns:employees="http://www.wbex.ru/employees"
 targetNamespace="http://www.wbex.ru/employees"
 elementFormDefault="qualified">
 <attributeGroup name="employeeAttributes">
   <attribute name="version" type="decimal" fixed="1.0" />
   <attribute name="source" type="string"/>
 </attributeGroup>
 <element name="employees">
   <complexType>
     <sequence>
       <element name="employee" minOccurs="0" maxOccurs="unbounded">
         <complexType>
           <sequence>
             <element name="name" type="employees:NameType"/>
             <element name="location" type="employees:LocationType"/>
             <element name="phone" type="employees:PhoneType"/>
             <element name="knows" type="employees:KnowsType"/>
             <element name="description" type="employees:DescriptionType"/>
           </sequence>
           <attribute name="tags" type="token"/>
           <attribute name="person" type="ID"/>
         </complexType>
       </element>
     </sequence>
     <attributeGroup ref="employees:employeeAttributes"/>
   </complexType>
 </element>
 <complexType name="NameType">
   <group ref="employees:NameGroup"/>
   <attribute name="title" type="string"/>
 </complexType>
 <group name="NameGroup">
   <sequence>
     <element name="first" type="string" minOccurs="1" maxOccurs="unbounded"/>
     <element name="middle" type="string" minOccurs="0" maxOccurs="1"/>
     <element name="last" type="string"/>
   </sequence>
 </group>
 <complexType name="LocationType">
   <choice minOccurs="0" maxOccurs="unbounded">
     <element name="address" type="string"/>
     <sequence>
       <element name="latitude" type="float"/>
       <element name="longitude" type="float"/>
     </sequence>
   </choice>
 </complexType>
 <complexType name="PhoneType">
   <simpleContent>
     <extension base="string">
       <attribute name="kind" type="string" default="Home" />
     </extension>
   </simpleContent>
 </complexType>
 
 <complexType name="KnowsType">
   <attribute name="employees" type="string"/>
 </complexType>
 <complexType name="DescriptionType" mixed="true">
   <choice minOccurs="0" maxOccurs="unbounded">
     <element name="em" type="string"/>
     <element name="strong" type="string"/>
     <element name="br" type="string"/>
   </choice>
 </complexType>

</schema>

</source>
   
  


targetNamespace and non-default namespace

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <products xmlns ="http://www.wbex.ru"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
     <product>
         <name>name 1</name>
         <image>a.gif</image>
         <description>description 1</description>
         <warranty>lifetime warranty</warranty>
         <cost>41.95</cost>
         <retailer>http://www.wbex.ru</retailer>
     </product>
     <product>
         <name>name 2</name>
         <image>b.gif</image>
         <description>description 2</description>
         <cost>239.00</cost>
         <retailer>http://www.wbex.ru</retailer>
     </product>

</products>

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

       targetNamespace="http://www.wbex.ru"
       elementFormDefault="qualified"
       xmlns:j="http://www.wbex.ru">
   <element name="products">
       <complexType>
           <sequence>
               <element ref="j:product" minOccurs="0" maxOccurs="unbounded"/>
           </sequence>
       </complexType>
   </element>
   <element name="product">
       <complexType>
           <sequence>
               <element ref="j:name"/>
               <element ref="j:image"/>
               <element ref="j:description"/>
               <element ref="j:warranty" minOccurs="0"/>
               <element ref="j:weight" minOccurs="0"/>
               <element ref="j:cost" maxOccurs="unbounded"/>
               <element ref="j:retailer"/>
           </sequence>
       </complexType>
   </element>
   <element name="name" type="string"/>
   <element name="image" type="string"/>
   <element name="description" type="string"/>
   <element name="warranty" type="string"/>
   <element name="weight" type="string"/>
   <element name="cost" type="string"/>
   <element name="retailer" type="string"/>

</schema>

</source>