XML Tutorial/XML Schema/Global Local

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

An XML Schema Using Inline (or Local) Declarations

<xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   elementFormDefault="qualified"
   targetNamespace="http://www.wbex.ru/namespaces/pub"
   xmlns="http://www.wbex.ru/namespaces/pub">
    <xsd:element name="publications">
      <xsd:complexType>
       <xsd:sequence>
        <xsd:element name="book">
         <xsd:complexType>
          <xsd:sequence>
           <xsd:element name="title" type="xsd:string"/>
           <xsd:element name="author" type="xsd:string"/>
           <xsd:element name="description" type="xsd:string"/>
          </xsd:sequence>
          <xsd:attribute name="isbn" type="xsd:string"/>
         </xsd:complexType>
        </xsd:element>
       </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
</xsd:schema>


Global type versus Local type

Global declarations are declarations that appear as direct children of the <schema> element. 
Global element declarations can be reused throughout the XML Schema. 
Local declarations do not have the <schema> element.
Local types can be used only in their specific context. 
Referring to an Existing Global Element 
To refer to a global element declaration, include a ref attribute and specify the name of the global element as the value.
<element ref="target:first"/> 
An XML Schema Document Referencing Globally Defined Declarations
<xsd:schema
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   elementFormDefault="qualified"
   targetNamespace="http://www.wbex.ru/namespaces/pub"
   xmlns="http://www.wbex.ru/namespaces/pub">
    <xsd:element name="publications">
      <xsd:complexType>
       <xsd:sequence>
        <xsd:element ref="book"/>
       </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
    
    <xsd:element name="book">
      <xsd:complexType>
       <xsd:sequence>
        <xsd:element ref="title"/>
        <xsd:element ref="author"/>
        <xsd:element ref="description"/>
       </xsd:sequence>
       <xsd:attribute name="isbn" type="xsd:string"/>
      </xsd:complexType>
    </xsd:element>
    
    <xsd:element name="title" type="xsd:string"/>
    <xsd:element name="author" type="xsd:string"/>
    <xsd:element name="description" type="xsd:string"/>
</xsd:schema>


Local and Global Declarations

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="animal" type="endType" />
  <xsd:element name="name" type="xsd:string" />
  <xsd:complexType name="endType">
    <xsd:sequence>
      <xsd:element name="animal">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="name" minOccurs="0" />
            <xsd:element name="source" type="xsd:string" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="habitatType">
    <xsd:sequence>
      <xsd:element name="river">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element ref="name" minOccurs="1" maxOccurs="unbounded" />
            <xsd:element name="source" type="xsd:string" />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
Globally components appear on the first level below the xsd:schema element.
You can have two locally declared elements with the same name but different definitions.
Their context keeps them distinct. 
Global element declarations must have unique names. 
You must reference a global element in order to have it appear in a corresponding XML document.
When you define a complex type, you can either reference existing globally declared elements, or you can declare and define newelements on the spot. 
Locally declared elements are limited to the complex type definition in which they are declared.
Locally declared elements may not be used elsewhere in the schema.


XML Schema Structures

<xsd:schema
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified"
  targetNamespace="http://www.wbex.ru/namespaces/pub"
  xmlns="http://www.wbex.ru/namespaces/pub">
    <xsd:element name="publications">
      <xsd:complexType>
       <xsd:sequence>
        <xsd:element name="book" maxOccurs="unbounded">
         <xsd:complexType>
          <xsd:sequence>
          <xsd:element name="title" type="xsd:string"/>
          <xsd:element name="author" type="xsd:string"/>
          <xsd:element name="description" type="xsd:string"/>
          </xsd:sequence>
         </xsd:complexType>
        </xsd:element>
       </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
</xsd:schema>
XML Document Instance
<?xml version="1.0"?>
<publications>
  <book>
   <title>Mastering XHTML</title>
   <author>B</author>
   <description>XML.
   </description>
  </book>
  <book>
   <title>Java</title>
   <author>A</author>
   <description>Java</description>
  </book>
</publications>