XML/XML Schema/elementFormDefault

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

attributeGroup for elementFormDefault = "qualified"

<?xml version = "1.0" ?> 
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
        targetNamespace = "http://www.wbex.ru/employees"
        xmlns:cust = "http://www.wbex.ru/employees"
        elementFormDefault = "qualified"
        attributeFormDefault = "unqualified">
   
   <element name = "employee" type = "cust:employeeType" />
   
   <complexType name = "employeeType">
      <sequence>
         <element name = "FirstName" type = "string" />
         <element name = "MiddleInitial" type = "string" />
         <element ref = "cust:LastName" />
      </sequence>
      <attributeGroup ref = "cust:rootAttributes" />
   </complexType>
   
   <element name = "LastName" type = "string" />
   
   <attributeGroup name = "rootAttributes">
      <attribute name = "clubCardMember" type = "boolean" />
      <attribute name = "employeeID" type = "integer" />
   </attributeGroup>
   
</schema>
The following instance document will be valid:
<?xml version = "1.0" ?>
<employee xmlns = "http://www.wbex.ru/employees"
          employeeID = "2442"
          clubCardMember = "true">
   <FirstName>first</FirstName>
   <MiddleInitial>middle</MiddleInitial>
   <LastName>last</LastName>
</employee>



Each element and attribute is declared globally

File: Schema.xsd
<?xml version = "1.0" ?> 
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
        targetNamespace = "http://www.wbex.ru/employees"
        xmlns:cust = "http://www.wbex.ru/employees"
        elementFormDefault = "qualified"
        attributeFormDefault = "unqualified">
   
   <element name = "FirstName" type = "string" />
   <element name = "MiddleInitial" type = "string" />
   <element name = "LastName" type = "string" />
   <attribute name = "employeeID" type = "integer" />
   <attribute name = "clubCardMember" type = "boolean" />
   
   <element name = "employee">
      <complexType>
         <sequence>
            <element ref = "cust:FirstName" />
            <element ref = "cust:MiddleInitial" minOccurs = "0" 
               maxOccurs = "1" />
            <element ref = "cust:LastName" />
         </sequence>
         <attribute ref = "cust:employeeID" use = "required" />
         <attribute ref = "cust:clubCardMember" />
      </complexType>
   </element>
   
</schema>
File: Data.xml
 
<?xml version = "1.0" ?>
<cust:employee xmlns:cust = "http://www.wbex.ru/employees"
               cust:employeeID = "2442"
               cust:clubCardMember = "true" >
   <cust:FirstName>first</cust:FirstName>
   <cust:MiddleInitial>middle</cust:MiddleInitial>
   <cust:LastName>last</cust:LastName>
</cust:employee>



elementFormDefault = "qualified", attributeFormDefault = "unqualified"

<?xml version = "1.0" ?> 
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
        targetNamespace = "http://www.wbex.ru/employees"
        xmlns:cust = "http://www.wbex.ru/employees"
        elementFormDefault = "qualified"
        attributeFormDefault = "unqualified">
   
   <element name = "employee" type = "cust:employeeType" />
   
   <complexType name = "employeeType">
      <sequence>
         <element name = "FirstName" type = "string" />
         <element name = "MiddleInitial" type = "string" />
         <element ref = "cust:LastName" />
      </sequence>
      <attribute name = "clubCardMember" type = "boolean" />
      <attribute ref = "cust:employeeID" />
   </complexType>
   
   <element name = "LastName" type = "string" />
   <attribute name = "employeeID" type = "integer" />
   
</schema>
The following instance document will not work:
<?xml version = "1.0" ?>
<employee xmlns = "http://www.wbex.ru/employees"
          employeeID = "2442"
          clubCardMember = "true">
   <FirstName>first</FirstName>
   <MiddleInitial>middle</MiddleInitial>
   <LastName>last</LastName>
</employee>
The schema is expecting that we qualify the globally declared attribute name.
It wants to see the following: 
<?xml version = "1.0" ?>
<employee xmlns = "http://www.wbex.ru/employees"
          xmlns:cust = "http://www.wbex.ru/employees"
          clubCardMember = "true"
          cust:employeeID = "2442">
   <FirstName>first</FirstName>
   <MiddleInitial>middle</MiddleInitial>
   <LastName>last</LastName>
</employee>



elementFormDefault="unqualified"

File: Data.xml

<?xml version="1.0"?>
<bk:Books xmlns:bk="http://www.wbex.ru"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation= "http://www.wbex.ru Schema.xsd">
        <Publication>
                <Title>title 1</Title>
                <Author>author 1</Author>
                <Date>1999</Date>
        </Publication>
        <Publication xsi:type="bk:BookType">
                <Title>title 2</Title>
                <Author>author 2</Author>
                <Date>1977</Date>
                <ISBN>2-222-22222-2</ISBN>
                <Publisher>publisher 2</Publisher>
        </Publication>
</bk:Books>
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="unqualified">
    <xsd:complexType name="PublicationType">
        <xsd:sequence>
            <xsd:element name="Title" type="xsd:string"/>
            <xsd:element name="Author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
            <xsd:element name="Date" type="xsd:gYear"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="BookType">
        <xsd:complexContent>
            <xsd:extension base="PublicationType">
                <xsd:sequence>
                    <xsd:element name="ISBN" type="xsd:string"/>
                    <xsd:element name="Publisher" type="xsd:string"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:element name="Books">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Publication" maxOccurs="unbounded" type="PublicationType"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



elementFormDefault=unqualified Salami Slice

File: Schema.xsd
<?xml version = "1.0" ?> 
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
        targetNamespace = "http://www.wbex.ru/employees"
        xmlns:cust = "http://www.wbex.ru/employees"
        elementFormDefault = "qualified"
        attributeFormDefault = "unqualified">
   
   <element name = "FirstName" type = "string" />
   <element name = "MiddleInitial" type = "string" />
   <element name = "LastName" type = "string" />
   <attribute name = "employeeID" type = "integer" />
   <attribute name = "clubCardMember" type = "boolean" />
   
   <element name = "employee">
      <complexType>
         <sequence>
            <element ref = "cust:FirstName" />
            <element ref = "cust:MiddleInitial" minOccurs = "0" 
               maxOccurs = "1" />
            <element ref = "cust:LastName" />
         </sequence>
         <attribute ref = "cust:employeeID" use = "required" />
         <attribute ref = "cust:clubCardMember" />
      </complexType>
   </element>
   
</schema>
File: Data.xml
<?xml version = "1.0" ?>
<cust:employee xmlns:cust = "http://www.wbex.ru/employees"
               cust:employeeID = "2442"
               cust:clubCardMember = "true" >
   <cust:FirstName>first</cust:FirstName>
   <cust:MiddleInitial>middle</cust:MiddleInitial>
   <cust:LastName>last</cust:LastName>
</cust:employee>



If the schema had a value of unqualified for elementFormDefault it would look like so RussianDoll_eu_au.xml

File: Schema.xsd
<?xml version = "1.0" ?> 
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
        targetNamespace = "http://www.wbex.ru/employees"
        xmlns:cust = "http://www.wbex.ru/employees"
        elementFormDefault = "unqualified"
        attributeFormDefault = "unqualified">
   
   <element name = "employee">
      <complexType>
         <sequence>
         <element name = "FirstName" type = "string" />
         <element name = "MiddleInitial" type = "string" 
                  minOccurs = "0" maxOccurs = "1" />
         <element name = "LastName" type = "string" />
         </sequence>
         <attribute name = "employeeID" use = "required" type = "integer" />
         <attribute name = "clubCardMember" type = "boolean" />
      </complexType>
   </element>
</schema>
File: Data.xml

<?xml version = "1.0" ?>
<cust:employee xmlns:cust = "http://www.wbex.ru/employees"
               employeeID = "2442"
               clubCardMember = "true" >
   <FirstName>first</FirstName>
   <MiddleInitial>middle</MiddleInitial>
   <LastName>last</LastName>
</cust:employee>



Requiring All Elements and Attributes Be Qualified

<?xml version = "1.0" ?> 
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
        targetNamespace = "http://www.wbex.ru/employees"
        xmlns:cust = "http://www.wbex.ru/employees"
        elementFormDefault = "qualified"
        attributeFormDefault = "qualified">
   
   <element name = "employee" type = "cust:employeeType" />
   
   <complexType name = "employeeType">
      <sequence>
         <element name = "FirstName" type = "string" />
         <element name = "MiddleInitial" type = "string" />
         <element ref = "cust:LastName" />
      </sequence>
      <attribute name = "clubCardMember" type = "boolean" />
      <attribute ref = "cust:employeeID" />
   </complexType>
   
   <element name = "LastName" type = "string" />
   <attribute name = "employeeID" type = "integer" />
   
</schema>
We could qualify each element and attribute individually like so: 
<?xml version = "1.0" ?>
<cust:employee xmlns:cust = "http://www.wbex.ru/employees"
               cust:clubCardMember = "true" 
               cust:employeeID = "24427">
   <cust:FirstName>Ray</cust:FirstName>
   <cust:MiddleInitial>G</cust:MiddleInitial>
   <cust:LastName>Bayliss</cust:LastName>
</cust:employee>
But the following would NOT be valid:
<?xml version = "1.0" ?>
<employee xmlns = "http://www.wbex.ru/employees"
          clubCardMember = "true" 
          employeeID = "24427">
   <FirstName>first</FirstName>
   <MiddleInitial>middle</MiddleInitial>
   <LastName>last</LastName>
</employee>



using local element declarations for the children of the employee element

<?xml version = "1.0"?> 
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
        targetNamespace = "http://www.wbex.ru/employees"
        xmlns:cust = "http://www.wbex.ru/employees">
   
   <element name = "employee" type = "cust:employeeType" />
   
   <complexType name = "employeeType">
      <sequence>
         <element name = "FirstName" type = "string" />
         <element name = "MiddleInitial" type = "string" />
         <element name = "LastName" type = "string" />
      </sequence>
   </complexType>
   
</schema>
The following will not work in an instance document, 
because we are only expected to qualify globally declared elements and attributes.
<?xml version = "1.0" ?>
<employee xmlns = "http://www.wbex.ru/employees">
   <FirstName>Ray</FirstName>
   <MiddleInitial>G</MiddleInitial>
   <LastName>Bayliss</LastName>
</employee>
The proper way of showing this in a document is: 
<?xml version = "1.0" ?>
<cust:employee xmlns:cust = "http://www.wbex.ru/employees">
   <FirstName>first</FirstName>
   <MiddleInitial>middle</MiddleInitial>
   <LastName>last</LastName>
</cust:employee>