1 Global versus Local XML Schemas Best Practices
1 Global versus Local XML Schemas: Best Practices A set of guidelines for designing XML Schemas Created by discussions on xml-dev
2 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 complex. Types and simple. Types.
3 <? xml version="1. 0"? > <xsd: schema xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" target. Namespace="http: //www. publishing. org" xmlns="http: //www. publishing. org" element. Form. Default="qualified"> <xsd: complex. Type name="Publication"> <xsd: sequence> <xsd: element name="Title" type="xsd: string" min. Occurs="1" max. Occurs="unbounded"/> <xsd: element name="Author" type="xsd: string" min. Occurs="1" max. Occurs="unbounded"/> <xsd: element name="Date" type="xsd: year" min. Occurs="1" max. Occurs="1"/> </xsd: sequence> </xsd: complex. Type> <xsd: complex. Type name="Book"> <xsd: complex. Content> <xsd: extension base="Publication" > <xsd: sequence> <xsd: element name="ISBN" type="xsd: string" min. Occurs="1" max. Occurs="1"/> <xsd: element name="Publisher" type="xsd: string" min. Occurs="1" max. Occurs="1"/> </xsd: sequence> </xsd: extension> </xsd: complex. Content> </xsd: complex. Type> <xsd: element name="Book. Catalogue"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Book" type="Book" min. Occurs="1" max. Occurs="unbounded"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> </xsd: schema> Local type definition Local element declarations Global type definition Global element declaration
4 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
5 Russian Doll Design <xsd: element name="Book"> <xsd: complex. Type> <xsd: sequence> <xsd: element name="Title" type="xsd: string”/> <xsd: element name="Author" type="xsd: string"/> </xsd: sequence> </xsd: complex. Type> </xsd: element> Components with components (boxes within boxes) just like a Russian Doll
6 Salami Slice Design <xsd: element name="Title" type="string"/> <xsd: element name="Author" type="string"/> <xsd: element name="Book"> <xsd: complex. Type> <xsd: sequence> <xsd: element ref="Title”/> <xsd: element ref="Author”/> </xsd: sequence> </xsd: complex. Type> </xsd: element> Slice up the components into individual element declarations, then assemble them together (using “ref”) - just like a salami sandwich
element. Form. Default hide expose 7 Venetian Blind Design <xsd: simple. Type name="Title"> <xsd: restriction base="xsd: string"> <xsd: enumeration value="Mr. "/> <xsd: enumeration value="Mrs. "/> <xsd: enumeration value="Dr. "/> </xsd: restriction> </xsd: simple. Type> <xsd: simple. Type name="Name"> <xsd: restriction base="xsd: string"> <xsd: min. Length value="1"/> </xsd: restriction> </xsd: simple. Type> <xsd: complex. Type name="Publication"> <xsd: sequence> <xsd: element name="Title" type="Title”/> <xsd: element name="Author" type="Name”/> </xsd: sequence> </xsd: complex. Type> <xsd: element name="Book" type="Publication"/> Lay out the components as individual type definitions, use element. Form. Default to hide/expose component namespaces - just like slats on a Venetian blind
8 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
9 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
10 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 - element. Form. Default switches namespace exposure on or off • Coupled - components are interconnected • Cohesive - all related components are grouped together
11 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
- Slides: 11