Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Substitution Groups in XML Schemas Tomer Shiran Winter 2003/4 Semester.

Similar presentations


Presentation on theme: "1 Substitution Groups in XML Schemas Tomer Shiran Winter 2003/4 Semester."— Presentation transcript:

1 1 Substitution Groups in XML Schemas Tomer Shiran Winter 2003/4 Semester

2 2 What’s included? Why substitution groups? Substitutions in an XML instance Declaring a substitution group The substitution group hierarchy Type constraints for substitution groups Controlling substitution groups Alternatives to substitution groups

3 3 Why substitution groups? example1.xml 111 Ten-Gallon Hat 27 222 Short-Sleeved Linen Blouse 40 Red … 444 Special Seasonal

4 4 Why Substitution Groups? (Cont.) The previous design has various problems: We want to allow different content models for different kinds of products: Shirts have a mandatory size and an optional color Hats have a mandatory size Umbrellas have a mandatory radius We want to use descriptive element names that indicate the type of product (e.g., shirt and hat instead of product ). We want to accept new kinds of products without altering the original schema. Substitution groups address these issues…

5 5 Substitutions in an XML instance example2.xml <items xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation="example2.xsd"> 111 Ten-Gallon Hat 27 222 Short-Sleeved Linen Blouse 40 Red … 444 Special Seasonal Whenever the head element is referenced in a content model, one of the member elements may be substituted in place of the head.

6 6 Declaring a substitution group example2.xsd Any global element can be the head of a substitution group.

7 7 Declaring a substitution group (Cont.) example2.xsd (Cont.)

8 8 Declaring a substitution group (Cont.) example2.xsd (Cont.)

9 9 The substitution group hierarchy Each substitution group has one head element and one or more members. If the head element is in a different namespace (i.e., the schema document in which it is declared has a target namespace), a prefix must be used in the substitutionGroup attribute (e.g., ord:product ). Each element declaration can only be a direct member of one substitution group – membership is transitive, but not symmetric. An element declaration can be a member of a substitution group and the head of a different substitution group.

10 10 The substitution group hierarchy (Cont.) hat, shirt, blouse, tShirt, and umbrella can substitute for product. blouse and tShirt can substitute for shirt.

11 11 Type constraints for substitution groups Members of a substitution group must have types that are: The same as the type of the head, or Derived from the head by either extension or restriction (directly or indirectly, through multiple levels of restriction or extension) XMLSPY 2004 doesn’t enforce this rule. If an element declaration doesn’t specify a type attribute or a simpleType or complexType child, the type definition of the declaration is the same type as the element specified by the substitutionGroup attribute (the head of the group).

12 12 Controlling substitution groups The final attribute limits the declaration of substitution groups in schemas. The block attribute limits the use of substituted elements in instances. The abstract attribute forces element substitution in instances.

13 13 The final attribute Preventing substitution group declarations ValueDescription "#all" "extension restriction" "restriction extension" Cannot be used as the head of a substitution group "extension" Prevents extension in substitution group members "restriction" Prevents restriction in substitution group members "" (an empty string) There are no restrictions (overrides the finalDefault attribute) No final attribute The final attribute defaults to the value of finalDefault of the schema element (if it exists – otherwise there are no restrictions)

14 14 The block attribute Blocking substitution in instances With this declaration of product, the schema in example2.xsd would have been legal, but the instance in example2.xml would have been illegal.

15 15 final vs. block Terminology: final specifies “substitution group exclusions”, which is a static constraint – it affects the element derivation hierarchy block specifies “disallowed substitutions”, which is a runtime constraint – it affects the instance structure substitutionGroup specifies “substitution group affiliation” There are 32 different ways to set the constraints: 2 2 for final ( extension and restriction ) 2 3 for block ( substitution, extension, and restriction )

16 16 final vs. block (Cont.) An example A type hierarchy: Element declarations: What substitutions can be made in the instance? middle instead of top ? bottom instead of middle ? bottom instead of top ? This is valid because there is no static constraint in middle. Yes No Yes

17 17 The abstract attribute Forcing substitution An abstract element declaration can never apply to an instance element. It can serve as the head of a substitution group (like an abstract class in Java/C#). In XMLSPY 2004 (but not in the standard or in other implementations), members of a substitution group with an abstract head must explicitly specify a type. … … 444 Special Seasonal

18 18 The abstract attribute (Cont.) example3.xsd Specifies that local elements (e.g., number ) also belong to the target namespace (avoids prefixes in the instance).

19 19 The abstract attribute (Cont.) example3.xsd (Cont.) …

20 20 The abstract attribute (Cont.) example3ext.xsd …

21 21 The abstract attribute (Cont.) example3.xml 111 Ten-Gallon Hat 27 … 444 Super Sweater 38 Blue 555 Million-Dollar Suit Notice the whitespace! Won’t work in XMLSPY 2004 (because the suit element declaration doesn’t explicitly specify a type in the schema)!

22 22 Alternatives to substitution groups Reusable choice groups Substituting a derived type in the instance

23 23 Reusable choice groups example4.xsd... …

24 24 Reusable choice groups (Cont.) Pros: Easy to see the list of members of the “substitution group” Cons: Cumbersome to add new element declarations to the “substitution group” (a redefinition is needed, and it can only be done in schema documents with the same target namespace)

25 25 Derived type substitution in the instance Instead of declaring shirt, hat, and umbrella, we just declare their data types and specify all products as product elements. The derived types are substituted in the instance by specifying the xsi:type attribute. The block attribute of complexType or element can be specified in the schema in order to block substitution of derived types. If the block attribute is used in an element declaration (an element element), and its value is " #all " or " substitution ", then the element cannot be substituted in the instance.

26 26 Derived type substitution (Cont.) example5.xsd … … … …

27 27 Derived type substitution (Cont.) example5.xml 111 Ten-Gallon Hat 27 222 Short-Sleeved Linen Blouse 40 Red … 444 Special Seasonal If ProductType was an abstract type, the xsi:type would be required.

28 28 Derived type substitution (Cont.) Pros: Instance may be easier to process Cons: Looks slightly more complicated, and requires a declaration of the XML Schema Instance Namespace Difficult concept for those who unacquainted with schemas (they expect the tag name to identify the “type” of the element) Impossible to write a DTD that would validate this instance to the same degree

29 29 Requirements for XML Schema 1.1 Categories in the requirements document: A requirement must be met in XML Schema 1.1. A desideratum should be met in XML Schema 1.1. An opportunistic desideratum may be met in XML Schema 1.1. Requirements: Improve interaction between exclusions and disallowed substitutions in the element component. Address problems with the interaction between wildcards and substitution groups. Specifically, resolve the bug where if complex type A has a wildcard, and B restricts A, then it can restrict the wildcard to a set of elements that match the wildcard. Not all elements in the substitution groups of those elements necessarily match the wildcard – so B is not a subset of A. Desiderata: Allow an element declaration to be in more than one substitution group.

30 30 Summary Substitution groups – flexibility as a two-edged sword Pros: Content models can accept additional elements from a variety of namespaces and schema documents (unlike reusable choice groups, which require redefine to extend) No special syntax is needed in an instance (no xsi:type required)

31 31 Summary (Cont.) Substitution groups – flexibility as a two-edged sword Cons: Hard to tell which elements are actually substitutable – especially when they belong to different schema documents and different namespaces Instance may be hard to process (but block and final can help) JAXB and.NET XML serialization don’t support schemas with substitution groups Schema authors must be well acquainted with most of the features of XML schemas (namespaces, complex types, type derivation, etc.)

32 32 Any questions?


Download ppt "1 Substitution Groups in XML Schemas Tomer Shiran Winter 2003/4 Semester."

Similar presentations


Ads by Google