The new XML Schema system is about to become the recommended standard for the consortium to overcome the limitations of DTDs (see Sidebar, the limitations of DTDs) and provide a rich syntax for XML documents. This article demonstrates the flexibility of the schema by using XML Schema systems to define the most basic XML document building blocks-elements.
XML schemas are more powerful than DTDs. To illustrate the powerful features of the XML Schema mechanism, the following three list of programs briefly compares different ways of representing elements. Listing 1 shows an XML document segment, listing 2 declares the two elements with a DTD syntax, and listing 3 is the corresponding XML Schema syntax form. Note that the same XML syntax is used in Listing 3. Through the schema, the validation parser can check whether the element Invoiceno is a positive integer, and whether the first character of the element ProductID is the letter between A and Z, followed by six Arabic digits. In contrast, the validation parsers that reference DTDs can only check that these elements are represented by strings.
List 1:xml Document segment
<InvoiceNo>123456789</InvoiceNo>
<ProductID>J123456</ProductID>
Listing 2: A DTD segment that describes the elements in Listing 1
<!ELEMENT InvoiceNo (#PCDATA)>
<!ELEMENT ProductID (#PCDATA)>
Listing 3: XML Schema that describes the elements in Listing 1
<element name='InvoiceNo' type='positive-integer'/>
<element name='ProductID' type='ProductCode'/>
<simpleType name='ProductCode' base='string'>
<pattern value='[A-Z]{1}d{6}'/>
</simpleType>
Using Namespaces in XML schemas
In this collaborative world, a person can process documents from multiple other groups, and different groups may want to represent their data elements in different ways. In addition, they may refer to an element of the same name created by different groups in a document. How do you differentiate between different definitions of the same name? XML schemas use namespaces to differentiate between these definitions.
Report:
Limitations of DTDs
(although, as a mechanism for describing structured information, DTDs successfully serviced SGML and HTML developers for 20 of years, it has serious limitations compared to XML schemas.)
DTDs require elements consisting of the following three components:
- Text string
- Blending of text strings with other child elements
- A set of child elements
DTDs do not use XML syntax and provide only limited support for types and namespaces. )
A given XML Schema defines a new set of names, such as element names, type names, property names, and attribute group names, whose definitions and declarations are written in the schema. The names defined in Listing 3 include Invoiceno, ProductID, and ProductCode.
We say that the name defined in the pattern belongs to its target namespace. The namespace itself has a fixed but unrestricted name and must conform to the URL syntax. For example, for the pattern segment in Listing 3, you can set the name of the namespace to: Http://www.SampleStore.com/Account.
The name syntax for namespaces is confusing, although starting with http://, that URL does not point to a file that contains schema definitions. In fact, this URL Http://www.SampleStore.com/Account does not point to any files, just an assigned name.
Definitions and declarations in a pattern may refer to names that belong to other namespaces. In this article, we call these namespaces the source namespaces. Each pattern has a target namespace, but there may be multiple source namespaces. The name space may be quite long, but in an XML document it is possible to use an xmlns declaration by using the shorthand form. To illustrate these concepts, we can add more content to the example pattern in Listing 4.
Listing 4: Target namespace and source namespace
<!--XML Schema fragment in file schema1.xsd-->
<xsd:schema targetNamespace='http://www.SampleStore.com/Account'
xmlns:xsd='http://www.w3.org/1999/XMLSchema'
xmlns:ACC= 'http://www.SampleStore.com/Account'>
<xsd:element name='InvoiceNo' type='xsd:positive-integer'/>
<xsd:element name='ProductID' type='ACC:ProductCode'/>
<xsd:simpleType name='ProductCode' base='xsd:string'>
<xsd:pattern value='[A-Z]{1}d{6}'/>
</xsd:simpleType>