1 / 70

TUTORIAL 4

TUTORIAL 4. WORKING WITH SCHEMAS. SCHEMAS. A schema is an XML document that defines the content and structure of one or more XML documents. The XML document containing the content is called the instance document. COMPARING SCHEMAS AND DTDS. This figure compares schemas and DTDs.

xannon
Télécharger la présentation

TUTORIAL 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TUTORIAL 4 WORKING WITH SCHEMAS New Perspectives on XML, 2nd Edition Tutorial 4

  2. SCHEMAS • A schema is an XML document that defines the content and structure of one or more XML documents. • The XML document containing the content is called the instance document. New Perspectives on XML, 2nd Edition Tutorial 4

  3. COMPARING SCHEMAS AND DTDS This figure compares schemas and DTDs New Perspectives on XML, 2nd Edition Tutorial 4

  4. SCHEMA VOCABULARIES • There is no single schema form. Several schema “vocabularies” have been developed in the XML language. • Support for a particular schema depends on the XML parser being used for validation. New Perspectives on XML, 2nd Edition Tutorial 4

  5. SCHEMA VOCABULARIES This figure shows a few schema vocabularies New Perspectives on XML, 2nd Edition Tutorial 4

  6. STARTING A SCHEMA FILE • A schema is always placed in a separate XML document that is referenced by the instance document. New Perspectives on XML, 2nd Edition Tutorial 4

  7. Example Schema <?xml version="1.0"?>< xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="note">  <xs:complexType>    <xs:sequence>      <xs:element name="to" type="xs:string"/>      <xs:element name="from" type="xs:string"/>      <xs:element name="heading" type="xs:string"/>      <xs:element name="body" type="xs:string"/>    </xs:sequence>  </xs:complexType>< /xs:element>< /xs:schema> New Perspectives on XML, 2nd Edition Tutorial 4

  8. ELEMENTS AND ATTRIBUTES OF THE PATIENTS DOCUMENT This figure shows the elements and attributes of the patients.xml document See pages 147 and 148 New Perspectives on XML, 2nd Edition Tutorial 4

  9. SCHEMA TYPES • XML Schema recognize two categories of types: • Simpletype contains only a single value • Value of an attribute • Or the textual content of an element • Complextype • Contains a structure New Perspectives on XML, 2nd Edition Tutorial 4

  10. SCHEMA TYPES This figure shows types of elements New Perspectives on XML, 2nd Edition Tutorial 4

  11. SIMPLE TYPE ELEMENTS • Use the following syntax to declare a simple type element in XML Schema: <element name=“name” type =“type”/> name = the name of the element in the instance document type = the data type of the element. • Example: <xs:element name = “lastName” type = “xs:string” /> Unlike DTD, schema is an XML language New Perspectives on XML, 2nd Edition Tutorial 4

  12. UNDERSTANDING DATA TYPES • XML Schema supports two data types • A built-in data type is part of the XML Schema specifications and is available to all XML Schema authors. • A user-derived data type is created by the XML Schema author for specific data values in the instance document. New Perspectives on XML, 2nd Edition Tutorial 4

  13. DECLARING AN ATTRIBUTE • An attribute is another example of a simple type. The syntax to define an attribute is <xs:attribute name=“name” type="type” default=“default” fixed=“fixed” /> name = the name of the attribute type = the data type, default = the attribute’s default value fixed = a fixed value for the attribute • For examle: <xs:attribute name=“Gender” type=“xs:string” default=“female” /> By convention, use xs for namespace for XML Schma namespace optional New Perspectives on XML, 2nd Edition Tutorial 4

  14. ASSOCIATING ATTRIBUTES AND ELEMENTS • The basic structure for defining a complex type with XML Schema is <xs:element name="name"> <xs:complexType> declarations </xs:complexType> </xs:element> name = the name of the element declarations = schema commands specific to the type of complex element being defined. New Perspectives on XML, 2nd Edition Tutorial 4

  15. ASSOCIATING ATTRIBUTES AND ELEMENTS • Four complex type elements that usually appear in an instance document are the following: • The element is an empty element and contains only attributes. • The element contains textual content and attributes but no child elements. • The element contains child elements but not attributes. • The element contains both child elements and attributes. The following slides describe each of these New Perspectives on XML, 2nd Edition Tutorial 4

  16. EMPTY ELEMENTS AND ATTRIBUTES • The code to declare the attributes of an empty element is <xs:element name="name"> <xs:complexType> attributes </xs:complexType> </xs:element> attributes = the set of declarations that define the attributes associated with the element New Perspectives on XML, 2nd Edition Tutorial 4

  17. EMPTY ELEMENTS AND ATTRIBUTES • For example: <xs: element name=“subject”> <xs:complexType> <xs:attriubute name=“name” type=“xs:string” /> <xs:attriubute name=“age” type=“xs:string” /> </xs: complexType> </xs:element> • describes the following empty element: <subjectname=“Cynthia Dibbs” age=“62” /> New Perspectives on XML, 2nd Edition Tutorial 4

  18. SIMPLE CONTENT AND ATTRIBUTES • If an element is not empty and contains textual content (but no child elements), the structure of the complex type element is slightly different. <xs:element name="name"> <xs:complexType> <xs:simpleContent> <xs:extension base="type"> attributes </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> New Perspectives on XML, 2nd Edition Tutorial 4

  19. SIMPLE CONTENT AND ATTRIBUTES Indicates text but no child • For example: <xs:element name=“performance”> <xs:complexType> <xs:simpleContent> <xs:extension base=“xs:string”> <xs:attribute name=“scale” type=“xs:string”/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> • defines • <performancescale=“Karnofskyk”>0.81</performance> Extended to include an attribute New Perspectives on XML, 2nd Edition Tutorial 4

  20. SPECIFYING THE USE OF AN ATTRIBUTE • An attribute may or may not be required with a particular element. To indicate whether an attribute is required, you add the use attribute to the element declaration or reference. The use attribute has the following values: • required—The attribute must always appear with the element • optional—(default) The use of the attribute is optional with the element • prohibited—The attribute cannot be used with the element • For example, the previous attribute declaration is modified as: <xs:attribute name=“scale” type=“xs:string” use=“required”/> New Perspectives on XML, 2nd Edition Tutorial 4

  21. REFERENCING AN ELEMENT OR ATTRIBUTE • Rather than nesting the attribute declaration within the element, you can create a reference to it. The code to create a reference to an element or attribute declaration is <xs:element ref="elemName" /> <xs:attribute ref="attName" /> where elemName = the name used in an element declaration attName = the name used in an attribute declaration New Perspectives on XML, 2nd Edition Tutorial 4

  22. REFERENCING AN ELEMENT OR ATTRIBUTE <xs:attribute name=“scale” type=“xs:string” /> <xs:element name=“performance”> <xs:complexType> <xs:simpleContent> <xs:extension base = “xs:string> <xs:attribute> ref=“scale” use=“required” /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> New Perspectives on XML, 2nd Edition Tutorial 4

  23. WORKING WITH CHILD ELEMENTS • Another kind of complex type element contains child elements, but no attributes. To define these child elements, use the code structure <xs:element name="name"> <xs:complexType> <xs:compositor> elements </xs:compositor> </xs:complexType> </xs:element> where elements =the list of simple type element declarations for each child element compositor = defines how the child elements are organized. New Perspectives on XML, 2nd Edition Tutorial 4

  24. USING COMPOSITORS • XML Schema supports the following compositors: • sequence defines a specific order for the child elements • choice allows any one of the child elements to appear in the instance document • all allows any of the child elements to appear in any order in the instance document; however, they must appear either only once or not all. New Perspectives on XML, 2nd Edition Tutorial 4

  25. USING COMPOSITORS <element name=“address”> <xs:complexType> <xs:sequence> <xs:element name = “street” type = “xs:string”/> <xs:element name = “city” type = “xs:string”/> <xs:element name = “state” type = “xs:string”/> </xs:sequence> <xs:complexType> </element> Must be in sequence New Perspectives on XML, 2nd Edition Tutorial 4

  26. USING COMPOSITORS <element name=“Family”> <xs:complexType> <xs:all> <xs:element name = “Father” type = “xs:string”/> <xs:element name = “Mother” type = “xs:string”/> </xs:all> <xs:complexType> </element> Family may contain Father and/or Mother in no particular order New Perspectives on XML, 2nd Edition Tutorial 4

  27. SPECIFYING THE OCCURENCES OF AN ITEM • <xs:element name=“patient” type=“xs:string” minOccurs=“1” maxOccurs=“3”/> New Perspectives on XML, 2nd Edition Tutorial 4

  28. WORKING WITH CHILD ELEMENTS AND ATTRIBUTES • The code for a complex type element that contains both attributes and child elements is <xs:element name="name"> <xs:complexType> <xs:compositor> elements </xs:compositor> </xs:complexType> attributes </xs:element> New Perspectives on XML, 2nd Edition Tutorial 4

  29. CHILD ELEMENTS AND ATTRIBUTES EXAMPLE <xs:element name=“patient”> <xs:complexType> <xs:sequence> <xs:element ref=“lastname”/> <xs:element ref=“firstName”/> </xs:sequence> <xs:attribute ref=“patID use=“required”> </xs:complexType> <xs:element> New Perspectives on XML, 2nd Edition Tutorial 4

  30. SPECIFYING MIXED CONTENT <Summary> Patient <Name>Cynthia Davis</Name> was enrolled in the <Study>Tamoxifen Study</Study> on 8/15/2003. </Summary> can be declared in the schema file using the following complex type: <element name="Summary"> <complexType mixed="true"> <sequence> <element name="Name" type="string"/> <element name="Study" type="string"/> </sequence> </complexType> </element> element contains both text and child elements New Perspectives on XML, 2nd Edition Tutorial 4

  31. APPLYING A SCHEMA • To attach a schema to the document, you must do the following: • Declare a namespace for XML Schema in the instance document. • Indicate the location of the schema file. • Example: xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi=SchemaLocation=“pschema.xsd”> Commonly used for XML Schema instances Schema instance namespace New Perspectives on XML, 2nd Edition Tutorial 4

  32. APPLYING A SCHEMA • Specifying a schema in an XML document • is treated only as a hint by validating parsers • ignored by some parsers • Not allowed to prevent improper financial documents from being fraudulently submitted • If there is no namespace for the contents of the instance document, add the following attribute to the root element: xsi:noNamespaceSchemaLocation="schema" Schema instance namespace New Perspectives on XML, 2nd Edition Tutorial 4

  33. Pause and breath New Perspectives on XML, 2nd Edition Tutorial 4

  34. UNDERSTANDING DATA TYPES • A primitive data type, also called a base type, is one of 19 fundamental data types not defined in terms of other types. • A derived data type is a collection of 25 data types that the XML Schema developers created based on the 19 primitive types. New Perspectives on XML, 2nd Edition Tutorial 4

  35. UNDERSTANDING DATA TYPES This figure shows the 44 built-in data types Page XML 168 Premitive types Dates & times Derived Types New Perspectives on XML, 2nd Edition Tutorial 4

  36. UNDERSTANDING DATA TYPES This figure shows a partial description of XML string data types Page XML 169 New Perspectives on XML, 2nd Edition Tutorial 4

  37. USING DATA TYPES Examples: <xs:attribute name=“patID” type=“xs:ID” /> <xs:attribute name=“onStudy” type=“xs:string” /> <xs:attribute name=“scale” type=“xs:string” /> New Perspectives on XML, 2nd Edition Tutorial 4

  38. UNDERSTANDING DATA TYPES This figure shows a partial description of XML numeric data types Page XML 170 New Perspectives on XML, 2nd Edition Tutorial 4

  39. UNDERSTANDING DATA TYPES This figure shows a partial description of XML date and time data types Page XML 171 New Perspectives on XML, 2nd Edition Tutorial 4

  40. DERIVING NEW DATA TYPES • Three components are involved in deriving new data types: • Value space: the set of values that correspond to the data type. • Lexical space: the set of textual representations of the value space. • Facets: the properties of the data type that distinguish one data type from another. • Text string length, range of allowable values, New Perspectives on XML, 2nd Edition Tutorial 4

  41. USER DERIVED DATA • New data types fall into three categories: • List: a list of values where each list is derived from a base type. • Union: the combination of two or more data types. • Restriction: a limit placed on the facet of a base type. New Perspectives on XML, 2nd Edition Tutorial 4

  42. LIST • List data type is a list of values separated by white space • To create a list data type: <xs:simpleType name=wbcList”> <xs:listitemType=xs:decimal” /> <xs:simpleType> • To use the data type: <xs:element name=“wbc” type=“wbcList” /> <wbe>15.1 15.8. 20.0 9.3 7.1 5.2 </wbc> New Perspectives on XML, 2nd Edition Tutorial 4

  43. DERIVING A RESTRICTED DATA TYPE • The most common way to derive a new data type is to restrict the properties of a base type. XML Schema provides twelve constraining facets for this purpose. New Perspectives on XML, 2nd Edition Tutorial 4

  44. CONSTRAINING FACETS This figure shows the 12 constraining facets New Perspectives on XML, 2nd Edition Tutorial 4

  45. CONSTRAINING FACETS EXAMPLE <xs:simpleType name=“ageType”> <xs:restriction base=“xs:integer”> <xs:minInclusive value = “21” /> </xs:restriction> </xs:simpleType> Constrains the data type to be greater than or equal to the value New Perspectives on XML, 2nd Edition Tutorial 4

  46. The Patterns Facet • A pattern can be created with a formatted text string called a regular expression or regex. • To apply a regular expression in a data type, you use the code <xs:simpleType name="name"> <xs:restriction base="type"> <xs:pattern value="regex"/> </xs:restriction> </xs:simpleType> • Where regex is a regular expression pattern. New Perspectives on XML, 2nd Edition Tutorial 4

  47. Regular Expressions • Figure 4-30 page 184 shows character types, for example • \d a digit from 0 to 9 • \D non digit character • Etc. • Figure 4-31 page 185 shows character sets, for example • [chars] Match any character in the chars list • [a-z] Match any lower case letter • [0-9] matach any digit form 0-9 • Etc. New Perspectives on XML, 2nd Edition Tutorial 4

  48. PATTERN QUANTIFIERS This figure shows pattern quantifiers New Perspectives on XML, 2nd Edition Tutorial 4

  49. EXAMPLE REGULAR EXPRESSIONS • <xs:pattern value=“\d{3}” /> • <xs:pattern value=“[A-Z]*” /> • <xs:pattern value=“[A-Z]{0,10}*” /} <xs:ximpleType name=“mrType”> <sx:restrictiion base=“xs:ID”> <xs:pattern value=“MR\d{3}-\d{3}-d2{2}” /> </xs:restriction> <xs:simpleType> New Perspectives on XML, 2nd Edition Tutorial 4

  50. WORKING WITH NAMED TYPES • Schema authors can create customized complex types. • Advantage: reuse the complex structure in several places in the document. • For example: <xs:element name=“fullName"> <xs:complexType> <xs:sequence> <xs:element name="firstName" type="xs:string"/> <xs:element name="lastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> • Use <xs:element name=“client” type=“fullName”/> <xs:element name=“salesperson” type=“fullName”/> New Perspectives on XML, 2nd Edition Tutorial 4

More Related