110 likes | 133 Views
Global versus Local. XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev. Issue. When should an element or type be declared global versus when should it be declared local?
E N D
Global versus Local XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev
Issue • When should an element or type be declared global versus when should it be declared local? • Recall: a global element is an element declaration that is an immediate child of <schema>. A local element is an element declaration that is nested within another component. Ditto for complexTypes and simpleTypes.
<?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.publishing.org" xmlns="http://www.publishing.org" elementFormDefault="qualified"> <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="Author" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:year" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="Book"> <xsd:complexContent> <xsd:extension base="Publication" > <xsd:sequence> <xsd:element name="ISBN" type="xsd:string" minOccurs="1" maxOccurs="1"/> <xsd:element name="Publisher" type="xsd:string" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="BookCatalogue"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" type="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Global type definition Global type definition Global element declaration Local element declarations Local type definition
Three Design Approaches • Russian Doll Design • Components are nested (inlined) within other components - i.e., self-contained components • Salami Slice Design • Components are declared as separate element declarations, then assembled them together • Venetian Blind Design • Components are defined as type definitions, then elements are constructed using the types
Russian Doll Design <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string”/> <xsd:element name="Author" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> Components with components (boxes within boxes) - just like a Russian Doll
Salami Slice Design <xsd:element name="Title" type="string"/> <xsd:element name="Author" type="string"/> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title”/> <xsd:element ref="Author”/> </xsd:sequence> </xsd:complexType> </xsd:element> Slice up the components into individual element declarations, then assemble them together (using “ref”) - just like a salami sandwich
elementFormDefault Venetian Blind Design hide expose <xsd:simpleType name="Title"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Mr."/> <xsd:enumeration value="Mrs."/> <xsd:enumeration value="Dr."/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="Name"> <xsd:restriction base="xsd:string"> <xsd:minLength value="1"/> </xsd:restriction> </xsd:simpleType> <xsd:complexType name="Publication"> <xsd:sequence> <xsd:element name="Title" type="Title”/> <xsd:element name="Author" type="Name”/> </xsd:sequence> </xsd:complexType> <xsd:element name="Book" type="Publication"/> Lay out the components as individual type definitions, use elementFormDefault to hide/expose component namespaces - just like slats on a Venetian blind
Characteristics of Russian Doll Design • Opaque Content: nested, invisible components • Impact: few reusable components • Localized Scope: nested components have scope limited to the component they are nested within • Impact: can hide (localize) namespaces • Compact: components are bundled into a single, tidy unit • Decoupled: no dependencies on other components • Cohesive: all related components are grouped together
Characteristics of Salami Slice Design • Transparent Content: the components are globally declared • Impact: components are reusable • Global scope: all components are global • Impact: namespaces are always exposed; no namespace hiding is possible • Coupled: components are interconnected • Impact: changes to components have a ripple effect • Cohesive: related components are grouped together
Characteristics of Venetian Blind Design • Maximum reuse - the components are globally defined as reusable types • Maximum namespace control - can hide or expose the elements since they are nested within type definitions • Easy exposure switching - elementFormDefault switches namespace exposure on or off • Coupled - components are interconnected • Cohesive - all related components are grouped together
Guidelines • Use the Russian Doll design when • minimal schema size is required • decoupled components is required • Use the Salami Slice design when • instance document authors require the ability to use synonyms/aliases for tag names • Use the Venetian blind design when • you require flexibility in hiding/exposing namespaces in instance documents • component reuse is important Do Labs 1,2,3