160 likes | 247 Views
TUTORIAL 4. WORKING WITH SCHEMAS Section 4.3 (p. 190). WORKING WITH NAMED TYPES. Since content can be either simple or complex, it is not surprising that XML Schema also allows schema authors to create customized complex types.
E N D
TUTORIAL 4 WORKING WITH SCHEMAS Section 4.3 (p. 190) New Perspectives on XML, 2nd Edition Tutorial 4
WORKING WITH NAMED TYPES • Since content can be either simple or complex, it is not surprising that XML Schema also allows schema authors to create customized complex types. • The advantage of creating a complex type is that the complex structure can be reused in the document. • For example, the following code declares an element named client containing the complex content of two child elements named firstName and lastName: <xs:element name="client"> <xs:complexType> <xs:sequence> <xs:element name="firstName" type="xs:string"/> <xs:element name="lastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> New Perspectives on XML, 2nd Edition Tutorial 4
NAMED MODEL GROUPS • A named model group is a collection, or group, of elements. The syntax for creating a model group is <xs:group name="name"> elements </xs:group> • Where name is the name of the model group, and elements is a collection of element declarations New Perspectives on XML, 2nd Edition Tutorial 4
WORKING WITH NAMED ATTRIBUTE GROUPS • Attributes can be grouped into collections called named attribute groups. • This is particularly useful for attributes that you want to use with several different elements in a schema. The syntax for a named attribute group is <xs:attributeGroup name="name"> attributes </xs:attributeGroup> • Where name is the name of the attribute group and attributes is a collection of attributes assigned to the group. New Perspectives on XML, 2nd Edition Tutorial 4
STRUCTURING A SCHEMA • One schema design is a Flat Catalog Design. • In this design, all element declarations are made globally. • The structure of the instance document is created by referencing the global element declarations. • The syntax is: <element ref=“name”> New Perspectives on XML, 2nd Edition Tutorial 4
FLAT CATALOG DESIGN This figure shows a Flat Catalog design New Perspectives on XML, 2nd Edition Tutorial 4
STRUCTURING A SCHEMA • Schemas can be structured in a number of ways. One structure is called a Russian Doll design. This design involves sets of nested declarations. • While this design makes it easy to associate the schema with the instance document, it can be confusing and difficult to maintain. New Perspectives on XML, 2nd Edition Tutorial 4
RUSSIAN DOLL DESIGN This figure shows a Russian Doll design New Perspectives on XML, 2nd Edition Tutorial 4
VENETIAN BLIND DESIGN • A Venetian blind design is similar to a flat catalog, except that instead of declaring elements and attributes globally, it creates named types and references those types within a single global element • In this layout, the only globally declared element is the patients element; all other elements and attributes are placed within element or attribute groups or, in the case of the performance element, within a named complex type New Perspectives on XML, 2nd Edition Tutorial 4
VENETIAN BLIND DESIGN New Perspectives on XML, 2nd Edition Tutorial 4
COMPARING SCHEMA DESIGNS This figure compares the three schema designs New Perspectives on XML, 2nd Edition Tutorial 4
PLACING A SCHEMA IN A NAMESPACE: TARGETING A NAMESPACE • To associate a schema with a namespace, you first declare the namespace and then make that namespace the target of the schema. To do this, you add the following attributes to the schema’s root element: prefix:xmlns="uri" targetNamespace="uri" • Where prefix is the prefix of the XML Schema namespace and uri is the URI of the target namespace New Perspectives on XML, 2nd Edition Tutorial 4
VALIDATING A COMBINED DOCUMENT This figure shows how schemas are combined when the data is combined New Perspectives on XML, 2nd Edition Tutorial 4
APPLYING A SCHEMA TO A DOCUMENT WITH A NAMESPACE • To apply a schema to a document with a namespace, add the following attributes to the instance document’s root element: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="uri schema" • Where uri is the URI of the namespace and schema is the location and name of the schema file. • All global elements and attributes from the schema must be qualified in the instance document. New Perspectives on XML, 2nd Edition Tutorial 4
INCLUDING AND IMPORTING SCHEMAS • To include a schema from the same namespace, add the following element as a child of the schema element: <xs:include schemaLocation="schema" /> • Where schema is the name and location of the schema file. • To import a schema from a different namespace, use the syntax <xs:import namespace="uri" schemaLocation="schema" /> • Where uri is the URI of the imported schema’s namespace and schema is the name and location of the schema file. New Perspectives on XML, 2nd Edition Tutorial 4
REFERENCING OBJECTS FROM OTHER SCHEMAS • Once a schema is imported, any objects it contains with global scope can be referenced • To reference an object from an imported schema, you must declare the namespace of the imported schema in the schema element • You can then reference the object using the ref attribute or the type attribute for customized simple and complex types New Perspectives on XML, 2nd Edition Tutorial 4