General Features





General Features

Having laid down some basic terminology, we're ready to look at some commonly used features. We'll start with a couple of general features, then move on to simple and complex Elements.

Let's begin with a document that references a schema and the schema it references.

SimpleCSV with Schema (SimpleCSV1.xml)
<?xml version="1.0" encoding="UTF-8"?>
<SimpleCSV
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="SimpleCSV1.xsd">
  <Row>
    <Column01>Jones</Column01>
    <Column02>Mary</Column02>
    <Column03>312 Renner Road</Column03>
    <Column04>Apartment C</Column04>
    <Column05>Richardson</Column05>
    <Column06>TX</Column06>
    <Column07>75080</Column07>
    <Column08>USA</Column08>
    <Column09>972-996-1051</Column09>
  </Row>
  <Row>
    <Column01>Smith</Column01>
    <Column02>Sue</Column02>
    <Column03>Highway 118</Column03>
    <Column05>Terlingua</Column05>
    <Column06>TX</Column06>
    <Column07>79852</Column07>
    <Column10>[email protected]</Column10>
  </Row>
</SimpleCSV>
Schema for SimpleCSV1 (SimpleCSV1.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSPY v4.3 U (http://www.xmlspy.com)
    by Michael C Rawlins (Rawlins EC Consulting) -->
<!--W3C Schema generated by XMLSPY v4.3 U
    (http://www.xmlspy.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="unqualified">
  <xs:annotation>
    <xs:documentation>This is the schema for the SimpleCSV
        format developed in Chapters 2 and 3
    </xs:documentation>
  </xs:annotation>
  <xs:element name="SimpleCSV">
    <xs:annotation>
      <xs:documentation>This is the root element for the
          document, defined anonymously as a complexType
      </xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Row" maxOccurs="unbounded">
          <xs:annotation>
            <xs:documentation>This is the Row element defined
                anonymously as a complexType
            </xs:documentation>
          </xs:annotation>
          <xs:complexType>
            <xs:sequence>
              <xs:annotation>
                <xs:documentation>In this constrained example
                    we have ten columns, all using the built-in
                    string data type and all are optional
                </xs:documentation>
              </xs:annotation>
              <xs:element name="Column01" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column02" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column03" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column04" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column05" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column06" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column07" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column08" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column09" type="xs:string"
                  minOccurs="0"/>
              <xs:element name="Column10" type="xs:string"
                  minOccurs="0"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

We'll go into all the details shortly, but here are a couple of things to quickly note. One is that the schema is itself a well-formed XML instance document. You don't see it in the schema, but it even conforms to a schema and DTD that are part of the Schema Recommendation. The other thing to note is the indentation. As with all "ignorable whitespace" in XML documents, the indentation is strictly for readability. With a few exceptions it has no significance to XML processors. The exceptions are documentation Elements and Elements of the string data type. Both will be discussed later in the chapter.

Schema Declaration in Instance Documents

Let's first look at the schema declaration in the instance document. It takes the form of Attributes on the document's root Element.

<SimpleCSV
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="SimpleCSV1.xsd">

The first Attribute, xmlns, indicates the value of the xsi namespace prefix. (We'll talk more about namespaces later in this chapter.) It is needed here so that we know how to interpret the second Attribute, which is the one we're really interested in. The noNamespaceSchemaLocation (in the xsi namespace) indicates both that we don't have an explicit target namespace for this instance document and where to look for the schema. In this case, since there is no additional information, it tells us to look for SimpleCSV1.xsd in the same location as this instance document. The XML Schema Recommendation specifies that the value of this Attribute is a Uniform Resource Identifier (URI). We'll talk more about the interesting world of URIs, URNs, and URLs later in this chapter.

Comments and Documentation

Apparently, many people have bought into the notion that XML is self-documenting. Or maybe they subscribe to the old programming adage, "It was hard to write. It should be hard to read!" Whatever the case, not everyone believes in documenting his or her schemas. For the blessed folks who do, schema language offers two choices. You will see both in the schemas you use.

  • xs:documentation Element inside an xs:annotation Element: This is the preferred style. When used in this fashion, tools such as schema authors display the documentation nicely. You can see a few such examples throughout this chapter.

  • XML comments: The traditional style of creating XML comments by using the start (<!--) and end (-->) characters is allowed but not preferred. It is usually only readable when looking at the raw XML version of a schema and is not normally displayed in other views offered by schema authors. The comments in SimpleCSV1.xsd were inserted by XMLSPY. This is not a plug for XMLSPY; I just left them in so you'll know what they look like. (They will not show up in later schemas.)

Fortunately, almost no one expects anyone to read comments in instance documents. They are rarely used.

Element Declarations

Elements are declared using the schema language xs:element Element. There are two basic methods for declaring an Element. An Element may be declared with a name Attribute that specifies the name of the Element. It may also, as with the column Elements in our example, have a type Attribute that indicates the name of the Element's type. Every Element must have a type, but there are various ways in schema language to specify the type. We'll talk in this chapter about those options as we encounter their usage.

In addition to being declared via a name, an Element may be declared by a reference using the ref Attribute. In this method the reference is to another declaration where the Element has been declared with a name. This method is discussed in more detail in the subsection that deals with global Elements later in this chapter.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows