Defining the XML Schema

Extensible Markup Language (XML) is quite like a database itself, in that it stores data along with the basic data definition and, with an XML schema, defines the complete data structure and business rules. XML is much more portable than a database and is quickly becoming the standard for data exchange among Web sites, applications, and other implementations that require exchanging data.

Defining an XML schema is, in a sense, taking the logical data structure that resulted from ER modeling to the next step. It is an alternative to using Document Type Definitions (DTDs) for defining a data structure to be used on the Web.

Schema Versus DTD

Both schemas and DTDs enable developers to take data and provide a set of rules that can test the data against a data structure definition to ensure that the data is properly formed. In this respect, both set up a type of data record definition with specifications for the data fields in each record.

Although DTDs have served as a means of using HTML data on the Web for many years, they have many restrictions compared to using XML schemas. DTDs call for elements to consist of one of three things: a text string, a text string with other child elements, or a set of child elements. DTDs have only limited support for data types and namespaces.

The XML schema provides a grammatical structure for XML data that is far superior to that of a DTD. Schemas are much more flexible and represent the fundamental building block of an XML data structure. With schemas, you can allow for data validation as a positive and/or negative integer, specify that an element contain only letters between A and Z, or create more complex data pictures that have specific formatting, such as an invoice number that must be a single letter followed by four digits. By contrast, DTDs can only verify that these elements are represented as strings. Also, schemas provide precise control over element frequency, as opposed to the crude frequency operators (?, *, +) provided by DTDs.

Parts of a Schema Document

An XML schema is also a type of XML document, which is advantageous to new developers because, unlike DTDs, you use the same structure to define the schema as you do the data. XML schemas enable developers to create and validate the structure of XML data. A schema provides a way to define the data structure. You specify elements that can be used in the data as well as the structure and data types the elements conform to in order to be valid. A sample schema illustrated in Figure 8.6 is taken from documentation specifications submitted to the World Wide Web Consortium. XML definitions for the language itself are also presented by using XML schemas.

Figure 8.6. A complex XML schema.

graphics/08fig06.gif


Typically, the schema document has an .xsd extension. The XML schema describes the contents of XML data documents. Valid data is declared by using element and attribute elements, and the data structure is created by using simpleType and complexType elements, as described in the next sections. Although you do not have to define a schema on the exam, you do need to know how schemas are implemented. To learn more about XML data, XML schemas, and other elements related to XML, refer to XML Services > XML Core in the MSDN Library. The following sections briefly describe the basic content of an XML schema document.

Element

The basic unit of an XML document is the element. The schema defines the element object and sets up the definition for the element. An element declaration associates a name with a type definition, which can be based on a built-in data type, a simple type, or a complex type.

The following example shows setting up an optional element (minOccurs="0") with three possible entries:

 <xs:element name="prescript" type="xs:string"/> <xs:element name="nonprescript" type="xs:string"/> <xs:element name="overcounter" type="xs:string"/> <xs:element name="codeinBased" type="xs:string"     substitutionGroup="overCounter" /> <xs:element name="narcotic" type="xs:string"     substitutionGroup ="prescript" /> <xs:element name="drugs">   <xs:complexType>     <xs:choice minOccurs="0" maxOccurs="unbounded">       <xs:element ref="prescript"/>       <xs:element ref="nonprescript"/>       <xs:element ref="overcounter"/>     </xs:choice>   </xs:complexType> </xs:element> 

Attribute

An attribute element defines properties of an element. Often the element defines the record or row content, and the attribute defines the field or column content. Attribute declarations can be present as child elements of the schema, complexType elements, and attributeGroup elements, or they can exist within complex type definitions.

 <xs:simpleType name="Doses">    <xs:restriction base="xs:decimal">       <xs:enumeration value="0.5"/>       <xs:enumeration value="1"/>       <xs:enumeration value="2"/>       <xs:enumeration value="5"/>    </xs:restriction> </xs:simpleType> <xs:attribute name="patientDose">    <xs:simpleType>       <xs:list itemType="Doses"/>    </xs:simpleType> </xs:attribute> 

complexType

A complexType is a type definition for an element. It can contain attributes and other elements. An element can be declared with a type attribute that refers to a complexType element, which defines the element's structure, content, and attributes. An element can also take a reference to a simpleType element within its type attribute.

The following example shows a complex type (pharDrugMeasure) that contains a decimal simple type with an attribute and element declaration that uses a complex type:

 <xs:complexType name='pharDrugMeasure'>   <xs:simpleContent>   <xs:extension base='xs:decimal'>    <xs:attribute name='milligrams' type='xs:string' />   </xs:extension>  </xs:simpleContent> </xs:complexType> <xs:element name='DrugMeasure' type='pharDrugMeasure'/> 

simpleType

A simpleType is defined by deriving built-in data types and other derived simple types. A simpleType cannot contain elements and cannot have attributes. A simpleType determines the constraints on data. The constraints set up the rules for the values of attributes or elements with text-only content. The following is an example of a simpleType element named listingOfDates:

 <xs:simpleType name="listingOfDates">   <xs:list itemType="xs:date"> </xs:simpleType> 

The listingOfDates element derives content from the built-in date type. The following example shows how you can set up restrictions, in this case through enumerating positive integers, restricting the value of matterchange to 32 or 212:

 <xs:simpleType name="matterchange">   <xs:restriction base="xs:positiveInteger">     <xs:enumeration value="32"/>     <xs:enumeration value="212"/>   </xs:restriction> </xs:simpleType> 

Using simpleType elements, you can set up very specific rules and types for data content.

Namespaces and Other Schema Content

A schema defines XML data by specifying the structure or model and can define rules for data content. The XML schema also serves as a definition for the vocabulary (rules or grammar) that compliant XML data must follow to be considered valid. Validation of an XML document containing XML data ensures that the document conforms to the grammar specified by the schema.

The true power of a data schema is not necessarily having a schema that lays out a strict data model that every consuming process must use. Within the processing of XML data, "massaging" can occur that transforms data from one logical data structure to another. This transformation allows separate companies and applications to use the data in a recognized format.

An XML namespace is a set of names that can be used as element or attribute names in an XML document. The namespace uniquely qualifies element names to avoid conflicts between elements with the same name. The namespace is identified by some Uniform Resource Identifier (URI), either a Uniform Resource Locator (URL) or a Uniform Resource Number (URN). You define a namespace with an explicit declaration and then define a shorthand, to substitute for the namespace's full name. A sample schema borrowed from the MSDN Web site (XML Web Services > XML Core > MSXML > XML Tutorial > Lesson 6: Authoring XML Schemas) is shown in the following listing:

 <Schema xmlns="urn:schemas-microsoft-com:xml-data"   xmlns:dt="urn:schemas-microsoft-com:datatypes">   <AttributeType name="studentID" dt:type="string" required="yes"/>   <ElementType name="name" content="textOnly"/>   <ElementType name="GPA" content="textOnly" dt:type="float"/>   <ElementType name="student" content="mixed">     <attribute type="studentID"/>     <element type="name"/>     <element type="GPA"/>   </ElementType>   <ElementType name="class" content="eltOnly">     <element type="student"/>   </ElementType> </Schema> 

XML data, documents, and schemas can be quite complex, and because this chapter is intended to be a higher-end approach to the theory of XML, a lot of the specifics of XML development have been left out. Because XML development represents a significant technology for future development in the Microsoft .NET Framework, you should work further with XML to grasp its intricacies.



Analyzing Requirements and Defining. Net Solution Architectures (Exam 70-300)
MCSD Self-Paced Training Kit: Analyzing Requirements and Defining Microsoft .NET Solution Architectures, Exam 70-300: Analyzing Requirements and ... Exam 70-300 (Pro-Certification)
ISBN: 0735618941
EAN: 2147483647
Year: 2006
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net