21
XML Schemas II CIS-189 XML

Schemas 2 - Restricting Values

Embed Size (px)

Citation preview

Page 1: Schemas 2 - Restricting Values

XML Schemas IICIS-189 XML

Page 2: Schemas 2 - Restricting Values

Qualification refers to whether a value (element, attribute) must be qualified by a its namespace◦ When an element (or attribute) doesn’t have a

namespace declaration it’s unqualified◦ Determines how name is used in data (instance)

document A schema has the attributes

elementFormDefault and attributeFormDefault◦ Set to qualified or unqualified◦ By setting to qualified, must include a namespace

when use attributes or elements

Qualification

Page 3: Schemas 2 - Restricting Values

<schema attiributeFormDefault=“unqualified” elementFormDefault=“unqualified”>

Element and attributes can be specified individually as qualified (or not)<element form=“unqualified”><attribute form=“qualified”>

Qualification Examples

Page 4: Schemas 2 - Restricting Values

Have several different ways to specify how elements fit together

Sequence: specifies that elements must be in order, and there’s variability in when and how many times a child element occurs

All: elements may appear (or not), and in any order

Choice: one of several child elements may appear

Group: a set of elements may be referenced by name

Content Models

Page 5: Schemas 2 - Restricting Values

Allows elements to appear in any order or not at all

Rules governing use1. Must be only content model declaration of a

<complexType> definition For example, can’t follow with <sequence>

2. Can only have element declarations as children3. The children of the <all> element may appear

once – or not at all

<all> Declaration

Page 6: Schemas 2 - Restricting Values

<element name=“name”><complexType>

<all><element name=“first”

type=“xs:string” /><element name=“middle”

type=“xs:string” /><element name=“last”

type=“xs:string” /></all>

</complextType></element>

<all> Example

Page 7: Schemas 2 - Restricting Values

Similar in syntax to sequence Only one member of list can appear in the

document Allow a middle initial or a middle name

<choice><element name=“middleInitial” type=“xs:string” /><element name=“middleName type=“xs:string” />

</choice>

Note: Best case would restrict Middle Initial to one character with a simple type.

Choice

Page 8: Schemas 2 - Restricting Values

A group allows you to tie elements together, and then reference then with a single name

Definition:<group name=“nameFields”>

<element name=“first” type=“xs:string” /><element name=“middle” type=“xs:string” /><element name=“last” type=“xs:string” />

</group>Use:<element name=“name”>

<complexType><group ref=“nameFields” />

</complexType></element>

Element Groups

Page 9: Schemas 2 - Restricting Values

Can create a group of attributes similar to element groups

Allows re-use of common members without multiple definitions◦ Attribute groups cannot be recursive (refer to

themselves

Attribute Groups

Page 10: Schemas 2 - Restricting Values

Create limits on acceptable values as a simple type

A facet is a single property or trait of a simple type

Twelve facets can be applied to limit acceptable values

Restriction

Page 11: Schemas 2 - Restricting Values

minExclusive: smallest value, excluding what’s specified

minInclusive: smallest value, including what’s specified

maxExclusive: largest value, excluding what’s specified

maxInclusive: largest value, including what’s specified

totalDigits: total number of digits of a numeric type

Restriction Facets

Page 12: Schemas 2 - Restricting Values

fractionDigits: number of decimal places length: number of items in a list or characters

in a string minLength: minimum number of list items or

characters maxLength: maximum number of list items or

characters enumeration: specify member of a list whitespace: how whitespace should be treated pattern: restrict string types by pattern

Facets – 2

Page 13: Schemas 2 - Restricting Values

Specifying a positive integer<xs:simpleType name="creditHours">

<xs:restriction base="xs:integer"><xs:minInclusive value="0" />

</xs:restriction></xs:simpleType>

Sample Restrictions – 1

Page 14: Schemas 2 - Restricting Values

Specifying a list:<xs:simpleType name="cisDepartments">

<xs:restriction base="xs:string"><xs:enumeration value="CIS" /><xs:enumeration value="CNA" /><xs:enumeration value="CS" />

</xs:restriction></xs:simpleType>

Sample Restrictions – 2

Page 15: Schemas 2 - Restricting Values

Specify a 3 digit value

<xs:simpleType name="courseNumbering"><xs:restriction base="xs:string">

<xs:pattern value="\b[0-9]{3}\b" /></xs:restriction>

</xs:simpleType>

Sample Restrictions – 3

Page 16: Schemas 2 - Restricting Values

<?xml version="1.0" encoding="utf-8"?><xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema”> <xs:element name="course"> <xs:complexType> <xs:sequence> <xs:element name="department" type="cisDepartments" /> <xs:element name="number" type="courseNumbering" /> <xs:element name="title" type="xs:string" /> <xs:element name="credits" type="creditHours" /> </xs:sequence> </xs:complexType> </xs:element>

Sample Course Schema Part 1

Page 17: Schemas 2 - Restricting Values

<xs:simpleType name="creditHours"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="cisDepartments"> <xs:restriction base="xs:string"> <xs:enumeration value="CIS" /> <xs:enumeration value="CNA" /> <xs:enumeration value="CS" /> </xs:restriction> </xs:simpleType> <xs:simpleType name="courseNumbering"> <xs:restriction base="xs:string"> <xs:pattern value="\b[0-9]{3}\b" /> </xs:restriction> </xs:simpleType> </xs:schema>

Sample Part 2

Page 18: Schemas 2 - Restricting Values

A list allows an element or attribute to store multiple values◦ Uses enumerated values◦ Values are separated by whitespace, so

whitespace cannot be part of the content◦ Can be built-in XML or a defined simpleType data

type

<list> Declarations

Page 19: Schemas 2 - Restricting Values

<simpleType name=“Degrees”><restriction base=“string”><enumeration value=“AA” /><enumeration value=“AS” /><enumeration value=“AAS” /></restriction>

</simpleType>

<simpleType name=“DegreesList”><list itemType=“Degrees”/>

</simpleType>

<element name=“degreesEarned” type=“DegreesList” />

<list> ExampleDefine different values

Define data type using values

Define element storing different values

Page 20: Schemas 2 - Restricting Values

<union> allows the combination of two data type for an element or attribute

If have a possiblePoints element, expected value would be an integer; <union> would allow a string entry to note a “Missing” value

Separate data types with whitespace

<simpleType name=“CreditValue”><union memberTypes=“xs:integer

xs:string /></simpleType

<union> Declarations

Page 21: Schemas 2 - Restricting Values

<xs:simpleType name="zipCode"> <xs:union> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{5}" /> </xs:restriction> </xs:simpleType> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{5}-[0-9]{4}" /> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType>

<union> (continued)