Attribute Declarations

As I said earlier, adding Attributes to Elements and understanding what that does to simple versus complex types is one of the more awkward parts of the Schema Recommendation. However, once you get your head around this idiosyncrasy, Attribute declarations are fairly straightforward. Here's a plain-vanilla version in which we just extend our Columns from a simple type to a complex type by adding the ColumnNumber Attribute.

Specifying a ColumnNumber Attribute in SimpleCSV3.xsd
<xs:complexType name="ColumnType">
  <xs:annotation>
    <xs:documentation>Here we add the ColumnNumberAttribute,
        type integer, and optional to the ColumnType.
    </xs:documentation>
  </xs:annotation>
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute name="ColumnNumber" type="xs:integer"
          use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

The syntax for an Attribute declaration is very similar to an Element declaration. We still have the name and type Attributes, but the schema Element we use is xs:attribute. Note also that we still have simple content, as indicated by the xs:simpleContent Element, but the declaration is for a xs:complexType.

The relationships between restriction and extension and simple and complex become a bit clearer when we consider the next example. We want to add the ColumnNumber Attribute, but we also want to restrict the length of the column to 1,024 characters. We're extending the type from a simple type to a complex type but restricting the content model. We have to do this in two steps. Although it requires a few more lines in the schema, we can thank the Schema Recommendation authors for implementing the concepts with a bit of consistency. I think it would ultimately be more confusing if we could do both operations in one step. In the following snippet, we first create the String1024Type type by restriction from string, then extend it to the ColumnType complex type by adding the Attribute.

Simple Content Restriction and Extension in SimpleCSV4.xsd
<xs:simpleType name="String1024Type">
  <xs:annotation>
    <xs:documentation>This is the base for our ColumnType type,
        showing restriction, then extension.
    </xs:documentation>
  </xs:annotation>
  <xs:restriction base="xs:string">
    <xs:maxLength value="1024"/>
  </xs:restriction>
</xs:simpleType>
<xs:complexType name="ColumnType">
  <xs:annotation>
    <xs:documentation>Here we add the ColumnNumberAttribute,
        type integer, and optional to the ColumnType, but our
        base is our restricted 1024-byte string instead of the
        built-in string type.
    </xs:documentation>
  </xs:annotation>
  <xs:simpleContent>
    <xs:extension base="String1024Type">
      <xs:attribute name="ColumnNumber" type="xs:integer"
          use="optional"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

This shows clearly that we create a new simple type by restriction from a built-in data type, then use it to create a new complex type by extension. Again, note that in both this and the previous example the content of our complex type is still simple.

As I noted earlier, there are many more built-in schema language data types, and there is certainly a lot more involved in creating simple content Elements than what I've presented here. However, this discussion should give you all you need to read about 90 percent of what you'll find in schemas for business data. Let's move on now to Elements with complex content.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset