XML Tutorial/XML Schema/attributeFormDefault

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

Define prefixes in the schema for both our target namespace and for the W3C XML Schema namespace

   <source lang="xml">

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

 <book id="b0836217462" available="yes">
   <isbn>0836217462</isbn>
   <title>Java</title>
   <authors>
     <person id="CMS">
       <name>name 1</name>
       <born>1956-11-26</born>
       <dead>2000-02-12</dead>
     </person>
   </authors>
   <characters>
     <person id="PP">
       <name>Swing</name>
       <born>1966-08-22</born>
       <qualification>GUI</qualification>
     </person>
     <person id="Snoopy">
       <name>Database</name>
       <born>1950-10-04</born>
       <qualification>JDBC</qualification>
     </person>
   </characters>
 </book>

</library>

File: Schema.xsd <?xml version="1.0"?> <xs:schema targetNamespace="http://wbex.ru/ns/library"

 elementFormDefault="qualified" attributeFormDefault="unqualified"
 xmlns:lib="http://wbex.ru/ns/library"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="library">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="book" type="lib:bookType" />
     </xs:sequence>
   </xs:complexType>
 </xs:element>
 <xs:element name="person">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="name" type="xs:string" />
       <xs:element name="born" type="xs:date" />
       <xs:element name="dead" type="xs:date" minOccurs="0" />
       <xs:element name="qualification" type="xs:string"
         minOccurs="0" />
     </xs:sequence>
     <xs:attribute name="id" type="xs:ID" use="required" />
   </xs:complexType>
 </xs:element>
 <xs:complexType name="bookType">
   <xs:sequence>
     <xs:element name="isbn" type="xs:NMTOKEN" />
     <xs:element name="title" type="xs:string" />
     <xs:element name="authors">
       <xs:complexType>
         <xs:sequence>
           <xs:element ref="lib:person"
             maxOccurs="unbounded" />
         </xs:sequence>
       </xs:complexType>
     </xs:element>
     <xs:element name="characters">
       <xs:complexType>
         <xs:sequence>
           <xs:element ref="lib:person"
             maxOccurs="unbounded" />
         </xs:sequence>
       </xs:complexType>
     </xs:element>
   </xs:sequence>
   <xs:attribute name="id" type="xs:ID" use="required" />
   <xs:attribute name="available" type="xs:string" use="required" />
 </xs:complexType>

</xs:schema></source>


Set attributeFormDefault to qualified

   <source lang="xml">

<xsd:schema

 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://www.wbex.ru/namespaces/employee"
 xmlns="http://www.wbex.ru/namespaces/employee"
 elementFormDefault="qualified"
 attributeFormDefault="qualified">
 <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:attribute name="dept" type="xsd:string"/>
   <xsd:attribute name="client" type="xsd:string"/>
  </xsd:complexType>
 </xsd:element>

</xsd:schema> We have set both attributes to qualified; therefore, all elements and attributes in our instance document must be qualified: <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"
 em:dept="programming"
 em:client="Smith and Co">
 <em:name>Joe Smith</em:name>
 <em:email>a@a.ru</em:email>
 <em:hireDate>2008-10-29</em:hireDate>

</em:employee> If we set elementFormDefault to qualified and omit attributeFormDefault (its default value is unqualified), we could use the following document instance: <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"
 dept="programming"
 client="Smith and Co">
 <em:name>Joe Smith</em:name>
 <em:email>a@a.ru</em:email>
 <em:hireDate>2008-10-29</em:hireDate>

</em:employee></source>


Use prefixes on the components you are defining and use the W3C XML Schema vocabulary without prefixes

   <source lang="xml">

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

 <book id="b0836217462" available="yes">
   <isbn>1</isbn>
   <title>Full-Time Job</title>
   <authors>
     <person id="i1">
       <name>A</name>
       <born>1999-11-26</born>
       <dead>2000-02-12</dead>
     </person>
   </authors>
   <characters>
     <person id="i2">
       <name>B</name>
       <born>1966-08-22</born>
       <qualification>bold, brash and tomboyish</qualification>
     </person>
     <person id="i3">
       <name>Snoopy</name>
       <born>1950-10-04</born>
       <qualification>extroverted beagle</qualification>
     </person>
   </characters>
 </book>

</library> File: Schema.xsd <?xml version="1.0"?> <schema targetNamespace="http://wbex.ru/ns/library"

 elementFormDefault="qualified" attributeFormDefault="unqualified"
 xmlns="http://www.w3.org/2001/XMLSchema"
 xmlns:lib="http://wbex.ru/ns/library">
 <element name="library">
   <complexType>
     <sequence>
       <element name="book" type="lib:bookType" />
     </sequence>
   </complexType>
 </element>
 <element name="person">
   <complexType>
     <sequence>
       <element name="name" type="string" />
       <element name="born" type="date" />
       <element name="dead" type="date" minOccurs="0" />
       <element name="qualification" type="string"
         minOccurs="0" />
     </sequence>
     <attribute name="id" type="ID" use="required" />
   </complexType>
 </element>
 <complexType name="bookType">
   <sequence>
     <element name="isbn" type="string" />
     <element name="title" type="string" />
     <element name="authors">
       <complexType>
         <sequence>
           <element ref="lib:person" maxOccurs="unbounded" />
         </sequence>
       </complexType>
     </element>
     <element name="characters">
       <complexType>
         <sequence>
           <element ref="lib:person" maxOccurs="unbounded" />
         </sequence>
       </complexType>
     </element>
   </sequence>
   <attribute name="id" type="ID" use="required" />
   <attribute name="available" type="string" use="required" />
 </complexType>

</schema></source>


Use the target namespace as the default namespace of the schema document

   <source lang="xml">

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

 <book id="b0836217462" available="yes">
   <isbn>0836217462</isbn>
   <title>Java</title>
   <authors>
     <person id="CMS">
       <name>name 1</name>
       <born>1956-11-26</born>
       <dead>2000-02-12</dead>
     </person>
   </authors>
   <characters>
     <person id="PP">
       <name>Swing</name>
       <born>1966-08-22</born>
       <qualification>GUI</qualification>
     </person>
     <person id="Snoopy">
       <name>Database</name>
       <born>1950-10-04</born>
       <qualification>JDBC</qualification>
     </person>
   </characters>
 </book>

</library>

File: Schema.xsd <?xml version="1.0"?> <xs:schema targetNamespace="http://wbex.ru/ns/library"

 elementFormDefault="qualified" attributeFormDefault="unqualified"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns="http://wbex.ru/ns/library">
 <xs:element name="library">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="book" type="bookType" />
     </xs:sequence>
   </xs:complexType>
 </xs:element>
 <xs:element name="person">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="name" type="xs:string" />
       <xs:element name="born" type="xs:date" />
       <xs:element name="dead" type="xs:date" minOccurs="0" />
       <xs:element name="qualification" type="xs:string"
         minOccurs="0" />
     </xs:sequence>
     <xs:attribute name="id" type="xs:ID" use="required" />
   </xs:complexType>
 </xs:element>
 <xs:complexType name="bookType">
   <xs:sequence>
     <xs:element name="isbn" type="xs:NMTOKEN" />
     <xs:element name="title" type="xs:string" />
     <xs:element name="authors">
       <xs:complexType>
         <xs:sequence>
           <xs:element ref="person" maxOccurs="unbounded" />
         </xs:sequence>
       </xs:complexType>
     </xs:element>
     <xs:element name="characters">
       <xs:complexType>
         <xs:sequence>
           <xs:element ref="person" maxOccurs="unbounded" />
         </xs:sequence>
       </xs:complexType>
     </xs:element>
   </xs:sequence>
   <xs:attribute name="id" type="xs:ID" use="required" />
   <xs:attribute name="available" type="xs:string" use="required" />
 </xs:complexType>

</xs:schema></source>