Schemas 2 - Restricting Values


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Schemas 2 - Restricting Values

  1. 1. CIS-189 XML
  2. 2.  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
  3. 3. <schema attiributeFormDefault=“unqualified”elementFormDefault=“unqualified”> Element and attributes can be specified individually as qualified (or not) <element form=“unqualified”> <attribute form=“qualified”>
  4. 4.  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
  5. 5.  Allows elements to appear in any order or not at all Rules governing use 1. Must be only content model declaration of a <complexType> definition  For example, can’t follow with <sequence> 2. Can only have element declarations as children 3. The children of the <all> element may appear once – or not at all
  6. 6. <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>
  7. 7.  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 onecharacter with a simple type.
  8. 8.  A group allows you to tie elements together, and then reference then with a single nameDefinition:<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>
  9. 9.  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
  10. 10.  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
  11. 11.  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
  12. 12.  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
  13. 13. Specifying a positive integer<xs:simpleType name="creditHours"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0" /> </xs:restriction></xs:simpleType>
  14. 14. 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>
  15. 15.  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>
  16. 16. <?xml version="1.0" encoding="utf-8"?><xs:schema targetNamespace="" elementFormDefault="qualified" xmlns="" xmlns:mstns="" xmlns:xs="”> <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>
  17. 17. <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>
  18. 18.  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
  19. 19. Define<simpleType name=“Degrees”> different <restriction base=“string”> values <enumeration value=“AA” /> <enumeration value=“AS” /> <enumeration value=“AAS” /> </restriction></simpleType> Define<simpleType name=“DegreesList”> data type <list itemType=“Degrees”/> using</simpleType> values<element name=“degreesEarned” type=“DegreesList” /> Define element storing different values
  20. 20.  <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
  21. 21. <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>