Declaring Attributes : Attribute « XML Schema « XML Tutorial






<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.java2s.com" xmlns="http://www.java2s.com"
  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.java2s.com" sectionid="101" newspaperid="21"/>








3.84.Attribute
3.84.1.Declaring Attributes
3.84.2.Attribute declarations have the following format
3.84.3.Creating a Local/Global Type for Attribute
3.84.4.how we declare attributes
3.84.5.Value Constraints on Attributes
3.84.6.Value of an attribute is the same as the value we prescribe in the schema, whether or not the attribute is present
3.84.7.Attribute declarations may also be defined locally within the complexType element
3.84.8.Requiring an Attribute
3.84.9.Attribute Use: Default and Fixed Values
3.84.10.Prohibited attributes in the restriction
3.84.11.Predefining an Attribute's Content with a fixed value or a default value
3.84.12.Restrict the data type by restricting its attributes
3.84.13.Mark attribute as required
3.84.14.A globally defined attribute declaration