XML Tutorial/XML Schema/Attribute

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

A globally defined attribute declaration

   <source lang="xml">

<xsd:schema

 targetNamespace=
  "http://www.wbex.ru/namespace/employees"
 xmlns="http://www.wbex.ru/namespace/employees"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 elementFormDefault="qualified">
 <xsd:attribute name="int" use="optional"
  type="xsd:string"/>
 <xsd:element name="employees">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="firstName" type="xsd:string"/>
    <xsd:element name="middleName" type="xsd:string"/>
    <xsd:element name="lastName" type="xsd:string"/>
   </xsd:sequence>
   <xsd:attribute ref="int"/>
  </xsd:complexType>
 </xsd:element>

</xsd:schema></source>


Attribute declarations have the following format

   <source lang="xml">

<attribute name="name of the attribute"

          type="global type" 
          ref="global attribute declaration" 
          form="qualified or unqualified" 
          use="optional or prohibited or required" 
          default="default value" 
          fixed="fixed value"> 

There are two primary methods for declaring attributes: creating a local type or using a global type. Attribute declarations are restricted to simple types. Complex types are used to define types that contain attributes or elements. Simple types are used to restrict text-only content. Because an attribute can contain text only, you can use simple types only to define their allowable content. You can reuse attributes by referring to global attribute declarations.</source>


Attribute declarations may also be defined locally within the complexType element

   <source lang="xml">

<xsd:schema

 targetNamespace=
  "http://www.wbex.ru/namespace/employees"
 xmlns="http://www.wbex.ru/namespace/employees"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 elementFormDefault="qualified">
 <xsd:element name="employees">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="firstName" type="xsd:string"/>
    <xsd:element name="middleName" type="xsd:string"/>
    <xsd:element name="lastName" type="xsd:string"/>
   </xsd:sequence>
   <xsd:attribute name="int" use="optional" type="xsd:string"/>
  </xsd:complexType>
 </xsd:element>

</xsd:schema></source>


Attribute Use: Default and Fixed Values

   <source lang="xml">

To specify a default value, simply include the default attribute with the desired value: <attribute name="kind" type="contacts:KindType" default="Home"/> Fixed values operate much like default values. If the attribute is omitted, then the parser inserts the attribute with the fixed value. <attribute name="version" type="string" fixed="1.0"/> Attribute with fixed value <?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="qualified">
 <xsd:element name="source">
   <xsd:complexType>
     <xsd:complexContent>
       <xsd:extension base="xsd:anyType">
         <xsd:attribute name="sectionid" type="xsd:string"
           use="fixed" value="101" />
         <xsd:attribute name="newspaperid" type="xsd:string" />
       </xsd:extension>
     </xsd:complexContent>
   </xsd:complexType>
 </xsd:element>

</xsd:schema>

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

       sectionid="101" 
       newspaperid="21"/>

Attribute with default value The fixed attribute only sets the content if the attribute actually appears in the XML. If it is omitted, no content is set. If the default attribute is set but the attribute is omitted from the XML, then the attribute"s value is set to the default value. 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="qualified">
 <xsd:element name="source">
   <xsd:complexType>
     <xsd:complexContent>
       <xsd:extension base="xsd:anyType">
         <xsd:attribute name="sectionid" type="xsd:string"
           use="fixed" value="101" />
         <xsd:attribute name="newspaperid" type="xsd:string"
           use="default" value="21" />
       </xsd:extension>
     </xsd:complexContent>
   </xsd:complexType>
 </xsd:element>

</xsd:schema>

File: Data.xml <?xml version="1.0"?> <source sectionid="101" newspaperid="21"/></source>


Creating a Local/Global Type for Attribute

   <source lang="xml">

To create a local type, simply include the type declaration as a child of the <attribute> element: <attribute name="title">

   <simpleType> 
        
   </simpleType> 

</attribute> Using a Global Type for Attribute You can create a global <simpleType> definition. Within your attribute declarations, you can refer to a global type by name. This type can be one of the built-in XML Schema datatypes. Referring to an Existing Global Attribute <attribute ref="contacts:kind"/></source>


Declaring Attributes

   <source lang="xml">

<attribute name="QName"

       ref="QName"
       type="QName"
       use="optional | prohibited | required"
       form="qualified | unqualified"
       id="ID"
       default="string"
       fixed="string"
       {any attributes with non-schema namespace}

>

 Content: (annotation?, (simpleType?))

</attribute> An attribute is always of simple type since it contains neither other elements nor other attributes. An attribute always appears within an element of complex type. An attribute must be declared at the very end of the complex type to which it belongs.

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="qualified">
 <xsd:element name="source">
   <xsd:complexType>
     <xsd:complexContent>
       <xsd:extension base="xsd:anyType">
         <xsd:attribute name="sectionid" type="xsd:string" />
         <xsd:attribute name="newspaperid" type="xsd:string" />
       </xsd:extension>
     </xsd:complexContent>
   </xsd:complexType>
 </xsd:element>

</xsd:schema>

File: Data.xml <?xml version="1.0"?> <source xmlns="http://www.wbex.ru" sectionid="101" newspaperid="21"/></source>


how we declare attributes

   <source lang="xml">

File: Data.xml <?xml version = "1.0" ?> <Customer customerID = "24332">

  <FirstName>Raymond</FirstName>
  <MiddleInitial>G</MiddleInitial>
  <LastName>Bayliss</LastName>

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

  <element name = "Customer">
     <complexType>
        <sequence>
           <element name = "FirstName" type = "string" />
           <element name = "MiddleInitial" type = "string" />
           <element name = "LastName" type = "string" />
        </sequence>
        <attribute name = "customerID" type = "integer" />
     </complexType>
  </element>

</schema></source>


Mark attribute as required

   <source lang="xml">

File: Schema.xsd

<?xml version="1.0" encoding="UTF-8"?> <xs:schema

 targetNamespace="http://www.oreilly.ru/xslt" 
 xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns="http://www.oreilly.ru/xslt">
 
 <xs:element name="part">
   <xs:complexType>
     <xs:sequence>
       <xs:element ref="name" minOccurs="1" maxOccurs="1"/>
     </xs:sequence>
     <xs:attribute name="part-id" type="xs:ID" use="required"/>
   </xs:complexType>
 </xs:element>
 
 <xs:element name="name" type="xs:string"/>
 
 <xs:element name="partref">
   <xs:complexType>
     <xs:attribute name="refid" type="xs:IDREF" use="required"/>
   </xs:complexType>
 </xs:element>

</xs:schema></source>


Predefining an Attribute"s Content with a fixed value or a default value

   <source lang="xml">

File: Schema.xsd

<?xml version="1.0" ?> <schema xmlns="http://www.w3.org/2001/10/XMLSchema" targetNamespace="http://www.wbex.runs/" xmlns:end="http://www.wbex.ru/">

   <xsd:element name="source">
       <xsd:complexType>
           <xsd:complexContent>
               <xsd:extension base="xsd:anyType">
                   <xsd:attribute name="sectionid" type="xsd:string" use="required"/>
                   <xsd:attribute name="newspaperid" type="xsd:string"/>
               </xsd:extension>
           </xsd:complexContent>
       </xsd:complexType>
   </xsd:element>

</schema> File: Data.xml <?xml version="1.0"?> <source sectionid="101"/>

File: Data.xml <?xml version="1.0"?> <source sectionid="101" newspaperid="21"/></source>


Prohibited attributes in the restriction

   <source lang="xml">

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

 targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
 elementFormDefault="qualified">
 <xs:complexType name="tokenWithLangAndNote">
   <xs:simpleContent>
     <xs:extension base="xs:token">
       <xs:attribute name="lang" type="xs:language" />
       <xs:attribute name="note" type="xs:token" />
     </xs:extension>
   </xs:simpleContent>
 </xs:complexType>
 <xs:element name="title">
   <xs:complexType>
     <xs:simpleContent>
       <xs:restriction base="tokenWithLangAndNote">
         <xs:maxLength value="255" />
         <xs:attribute name="lang" type="xs:language" />
         <xs:attribute name="note" use="prohibited" />
       </xs:restriction>
     </xs:simpleContent>
   </xs:complexType>
 </xs:element>

</xs:schema></source>


Requiring an Attribute

   <source lang="xml">

Unless you specify, an attribute is always optional. You can predefine an attribute"s content with a fixed value or a default value. 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="qualified">
 <xsd:element name="source">
   <xsd:complexType>
     <xsd:complexContent>
       <xsd:extension base="xsd:anyType">
         <xsd:attribute name="sectionid" type="xsd:string"
           use="required" />
         <xsd:attribute name="newspaperid" type="xsd:string" />
       </xsd:extension>
     </xsd:complexContent>
   </xsd:complexType>
 </xsd:element>

</xsd:schema>

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

       sectionid="101" 
       newspaperid="21"/></source>
   
  

Restrict the data type by restricting its attributes

   <source lang="xml">

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

 targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
 elementFormDefault="qualified">
 <xs:complexType name="tokenWithLangAndNote">
   <xs:simpleContent>
     <xs:extension base="xs:token">
       <xs:attribute name="lang" type="xs:language" />
       <xs:attribute name="note" type="xs:token" />
     </xs:extension>
   </xs:simpleContent>
 </xs:complexType>
 <xs:element name="title">
   <xs:complexType>
     <xs:simpleContent>
       <xs:restriction base="tokenWithLangAndNote">
         <xs:maxLength value="255" />
         <xs:attribute name="lang">
           <xs:simpleType>
             <xs:restriction base="xs:language">
               <xs:enumeration value="en" />
               <xs:enumeration value="es" />
             </xs:restriction>
           </xs:simpleType>
         </xs:attribute>
       </xs:restriction>
     </xs:simpleContent>
   </xs:complexType>
 </xs:element>

</xs:schema></source>


Value Constraints on Attributes

   <source lang="xml">

File: Data.xml <?xml version = "1.0" ?> <CreditAccount currency = "US$">

  <AccountName>Ray Bayliss</AccountName>
  <AccountNumber>27012</AccountNumber>
  <Amount>200.00</Amount>

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

  <complexType>
     <sequence>
        <element name = "AccountName" type = "string" />
        <element name = "AccountNumber" type = "integer" />
        <element name = "Amount" type = "string" />
     </sequence>
     <attribute name = "currency" default = "US$" />
  </complexType>

</element> </schema></source>


Value of an attribute is the same as the value we prescribe in the schema, whether or not the attribute is present

   <source lang="xml">

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

  <complexType>
     <sequence>
        <element name = "AccountName" type = "string" />
        <element name = "AccountNumber" type = "integer" />
        <element name = "Amount" type = "string" />
     </sequence>
     <attribute name = "currency" fixed = "US$" />
  </complexType>

</element> </schema> File: Data.xml <?xml version = "1.0" ?> <CreditAccount currency = "AUS$">

  <AccountName>Joe</AccountName>
  <AccountNumber>2701 2202</AccountNumber>
  <Amount>200.00</Amount>

</CreditAccount></source>