New Perspectives on XML, 2nd Edition - PowerPoint PPT Presentation

About This Presentation
Title:

New Perspectives on XML, 2nd Edition

Description:

times new roman arial xml schemas comparing schemas and dtds schema vocabularies schema vocabularies starting a schema file example schema elements and attributes of ... – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 71
Provided by: WillandAn2
Category:

less

Transcript and Presenter's Notes

Title: New Perspectives on XML, 2nd Edition


1

TUTORIAL 4

WORKING WITH SCHEMAS
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.

3
COMPARING SCHEMAS AND DTDS
  • This figure compares schemas and DTDs

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.

5
SCHEMA VOCABULARIES
  • This figure shows a few schema vocabularies

6
STARTING A SCHEMA FILE
  • A schema is always placed in a separate XML
    document that is referenced by the instance
    document.

7
Example Schema
  • lt?xml version"1.0"?gtlt xsschema
    xmlnsxs"http//www.w3.org/2001/XMLSchema"gtltxs
    element name"note"gt  ltxscomplexTypegt   
    ltxssequencegt      ltxselement name"to"
    type"xsstring"/gt      ltxselement name"from"
    type"xsstring"/gt      ltxselement
    name"heading" type"xsstring"/gt     
    ltxselement name"body" type"xsstring"/gt   
    lt/xssequencegt  lt/xscomplexTypegtlt
    /xselementgtlt /xsschemagt

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
9
SCHEMA TYPES
  • XML Schema recognize two categories of types
  • Simple type contains only a single value
  • Value of an attribute
  • Or the textual content of an element
  • Complex type
  • Contains a structure

10
SCHEMA TYPES
  • This figure shows types of elements

11
SIMPLE TYPE ELEMENTS
  • Use the following syntax to declare a simple type
    element in XML Schema
  • ltelement namename type type/gt
  • name the name of the element in the instance
    document
  • type the data type of the element.
  • Example
  • ltxselement name lastName type xsstring
    /gt

Unlike DTD, schema is an XML language
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.

13
DECLARING AN ATTRIBUTE
  • An attribute is another example of a simple type.
    The syntax to define an attribute is
  • ltxsattribute namename type"type
    defaultdefault fixedfixed /gt
  • name the name of the attribute
  • type the data type,
  • default the attributes default value
  • fixed a fixed value for the attribute
  • For examle
  • ltxsattribute nameGender typexsstring
    defaultfemale /gt

By convention, use xs for namespace for XML Schma
namespace
optional
14
ASSOCIATING ATTRIBUTES AND ELEMENTS
  • The basic structure for defining a complex type
    with XML Schema is
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • declarations
  • lt/xscomplexTypegt
  • lt/xselementgt
  • name the name of the element
  • declarations schema commands specific to the
    type of complex element being defined.

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
16
EMPTY ELEMENTS AND ATTRIBUTES
  • The code to declare the attributes of an empty
    element is
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • attributes
  • lt/xscomplexTypegt
  • lt/xselementgt
  • attributes the set of declarations that define
    the attributes associated with the element

17
EMPTY ELEMENTS AND ATTRIBUTES
  • For example
  • ltxs element namesubjectgt
  • ltxscomplexTypegt
  • ltxsattriubute namename typexsstring /gt
  • ltxsattriubute nameage typexsstring /gt
  • lt/xs complexTypegt
  • lt/xselementgt
  • describes the following empty element
  • ltsubject nameCynthia Dibbs age62 /gt

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.
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • ltxssimpleContentgt
  • ltxsextension base"type"gt
  • attributes
  • lt/xsextensiongt
  • lt/xssimpleContentgt
  • lt/xscomplexTypegt
  • lt/xselementgt

19
SIMPLE CONTENT AND ATTRIBUTES
Indicates text but no child
  • For example
  • ltxselement nameperformancegt
  • ltxscomplexTypegt
  • ltxssimpleContentgt
  • ltxsextension basexsstringgt
  • ltxsattribute namescale
    typexsstring/gt
  • lt/xsextensiongt
  • lt/xssimpleContentgt
  • lt/xscomplexTypegt
  • lt/xselementgt
  • defines
  • ltperformance scaleKarnofskykgt0.81lt/performancegt

Extended to include an attribute
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
  • requiredThe attribute must always appear with
    the element
  • optional(default) The use of the attribute is
    optional with the element
  • prohibitedThe attribute cannot be used with the
    element
  • For example, the previous attribute declaration
    is modified as
  • ltxsattribute namescale typexsstring
    userequired/gt

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
  • ltxselement ref"elemName" /gt
  • ltxsattribute ref"attName" /gt
  • where
  • elemName the name used in an element
    declaration
  • attName the name used in an attribute
    declaration

22
REFERENCING AN ELEMENT OR ATTRIBUTE
  • ltxsattribute namescale typexsstring /gt
  • ltxselement nameperformancegt
  • ltxscomplexTypegt
  • ltxssimpleContentgt
  • ltxsextension base xsstringgt
  • ltxsattributegt refscale userequired /gt
  • lt/xsextensiongt
  • lt/xssimpleContentgt
  • lt/xscomplexTypegt
  • lt/xselementgt

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
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • ltxscompositorgt
  • elements
  • lt/xscompositorgt
  • lt/xscomplexTypegt
  • lt/xselementgt
  • where
  • elements the list of simple type element
    declarations for each child element
  • compositor defines how the child elements are
    organized.

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.

25
USING COMPOSITORS
  • ltelement nameaddressgt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement name street type
    xsstring/gt
  • ltxselement name city type xsstring/gt
  • ltxselement name state type
    xsstring/gt
  • lt/xssequencegt
  • ltxscomplexTypegt
  • lt/elementgt

Must be in sequence
26
USING COMPOSITORS
  • ltelement nameFamilygt
  • ltxscomplexTypegt
  • ltxsallgt
  • ltxselement name Father type
    xsstring/gt
  • ltxselement name Mother type
    xsstring/gt
  • lt/xsallgt
  • ltxscomplexTypegt
  • lt/elementgt

Family may contain Father and/or Mother in no
particular order
27
SPECIFYING THE OCCURENCES OF AN ITEM
  • ltxselement namepatient typexsstring
    minOccurs1 maxOccurs3/gt

28
WORKING WITH CHILD ELEMENTS AND ATTRIBUTES
  • The code for a complex type element that contains
    both attributes and child elements is
  • ltxselement name"name"gt
  • ltxscomplexTypegt
  • ltxscompositorgt
  • elements
  • lt/xscompositorgt
  • lt/xscomplexTypegt
  • attributes
  • lt/xselementgt

29
CHILD ELEMENTS AND ATTRIBUTES EXAMPLE
  • ltxselement namepatientgt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement reflastname/gt
  • ltxselement reffirstName/gt
  • lt/xssequencegt
  • ltxsattribute refpatID userequiredgt
  • lt/xscomplexTypegt
  • ltxselementgt

30
SPECIFYING MIXED CONTENT
  • ltSummarygt
  • Patient ltNamegtCynthia Davislt/Namegt was enrolled
    in
  • the ltStudygtTamoxifen Studylt/Studygt on 8/15/2003.
  • lt/Summarygt
  • can be declared in the schema file using the
    following complex type
  • ltelement name"Summary"gt
  • ltcomplexType mixed"true"gt
  • ltsequencegt
  • ltelement name"Name" type"string"/gt
  • ltelement name"Study" type"string"/gt
  • lt/sequencegt
  • lt/complexTypegt
  • lt/elementgt

element contains both text and child elements
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
  • xmlnsxsihttp//www.w3.org/2001/XMLSchema-instanc
    e
  • xsiSchemaLocationpschema.xsdgt

Commonly used for XML Schema instances
Schema instance namespace
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
  • xsinoNamespaceSchemaLocation"schema"

Schema instance namespace
33
Pause and breath
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.

35
UNDERSTANDING DATA TYPES
  • This figure shows the 44 built-in data types

Page XML 168
Premitive types
Dates times
Derived Types
36
UNDERSTANDING DATA TYPES
  • This figure shows a partial description of XML
    string data types

Page XML 169
37
USING DATA TYPES
  • Examples
  • ltxsattribute namepatID typexsID /gt
  • ltxsattribute nameonStudy typexsstring /gt
  • ltxsattribute namescale typexsstring /gt

38
UNDERSTANDING DATA TYPES
  • This figure shows a partial description of XML
    numeric data types

Page XML 170
39
UNDERSTANDING DATA TYPES
  • This figure shows a partial description of XML
    date and time data types

Page XML 171
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,

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.

42
LIST
  • List data type is a list of values separated by
    white space
  • To create a list data type
  • ltxssimpleType namewbcListgt
  • ltxslist itemTypexsdecimal /gt
  • ltxssimpleTypegt
  • To use the data type
  • ltxselement namewbc typewbcList /gt
  • ltwbegt15.1 15.8. 20.0 9.3 7.1 5.2 lt/wbcgt

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.

44
CONSTRAINING FACETS
  • This figure shows the 12 constraining facets

45
CONSTRAINING FACETS EXAMPLE
  • ltxssimpleType nameageTypegt
  • ltxsrestriction basexsintegergt
  • ltxsminInclusive value 21 /gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt

Constrains the data type to be greater than or
equal to the value
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
  • ltxssimpleType name"name"gt
  • ltxsrestriction base"type"gt
  • ltxspattern value"regex"/gt
  • lt/xsrestrictiongt
  • lt/xssimpleTypegt
  • Where regex is a regular expression pattern.

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.

48
PATTERN QUANTIFIERS
  • This figure shows pattern quantifiers

49
EXAMPLE REGULAR EXPRESSIONS
  • ltxspattern value\d3 /gt
  • ltxspattern valueA-Z /gt
  • ltxspattern valueA-Z0,10 /
  • ltxsximpleType namemrTypegt
  • ltsxrestrictiion basexsIDgt
  • ltxspattern valueMR\d3-\d3-d22
    /gt
  • lt/xsrestrictiongt
  • ltxssimpleTypegt

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
  • ltxselement namefullName"gt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement name"firstName"
    type"xsstring"/gt
  • ltxselement name"lastName" type"xsstring"
    /gt
  • lt/xssequencegt
  • lt/xscomplexTypegt
  • lt/xselementgt
  • Use
  • ltxselement nameclient typefullName/gt
  • ltxselement namesalesperson typefullName/gt

51
NAMED MODEL GROUPS
  • A named model group is a collection, or group, of
    elements.
  • Syntax
  • ltxsgroup name"name"gt
  • elements
  • lt/xsgroupgt
  • Where name is the name of the model group, and
    elements is a collection of element declarations

52
NAMED MODEL GROUPS EXAMPLE
  • Definition
  • ltxsgroup namefullNamegt
  • ltxssequencegt
  • ltxselement name"firstName"
    type"xsstring"/gt
  • ltxselement name"lastName" type"xsstring"
    /gt
  • lt/xssequencegt
  • lt/xsgroupgt
  • Use
  • ltxselement nameclient"gt
  • ltxscomplexTypegt
  • ltxsgroup reffullName/gtgt
  • lt/xscomplexTypegt
  • lt/xselementgt

53
WORKING WITH NAMED ATTRIBUTE GROUPS
  • Attributes can be grouped into collections called
    named attribute groups.
  • This is particularly useful for attributes that
    you want to use with several different elements
    in a schema. The syntax for a named attribute
    group is
  • ltxsattributeGroup name"name"gt
  • attributes
  • lt/xsattributeGroupgt
  • Where name is the name of the attribute group and
    attributes is a collection of attributes assigned
    to the group.

54
WORKING WITH NAMED ATTRIBUTE GROUPS
  • Example
  • ltxsattributeGroup nameDRInfogt
  • ltxsattribute nameDRID typexsstring
    userequired /gt
  • ltxsattribute namedept typexsstring
    userequired /gt
  • ltxsattributeGroupgt
  • ltxselement namedoctor typedeptData/gt
  • ltxscomplexType name deptDatagt
  • ltxssimplecontentgt
  • ltxs extension base stringgt
  • ltxsattributegroup
    refDRIinfo/gt
  • lt/xsextensongt
  • lt/xssimplecontentgt
  • lt/xscomplexTypegt

ltdoctor DRIDDR251 deptPediatricsgt curt
Hurley ltdoctorgt
55
SCOPE
  • An item has global scope it is a child of a
    root schema element
  • Can be referenced and reused anywhere in the
    schema
  • An item has local scope it is nested within
    another element
  • Can be referenced and reused only within the item
    in which it is declared

56
STRUCTURING A SCHEMA
  • Schemas can be structured in several ways
  • Flat catalog (also called salami slice)
  • All declarations are made globally
  • Russian doll
  • Has only one global element
  • Everything else is nested inside of that element
  • Venetian Blind Design
  • Similar to flat catalog, except
  • Creates types and references within a single
    global element

57
STRUCTURING A SCHEMA
  • One schema design is a Flat Catalog Design.
  • Sometimes called salami slice design
  • In this design, all element declarations are made
    globally.
  • The structure of the instance document is created
    by referencing the global element declarations.

58
FLAT CATALOG DESIGN
Figure 4-36 page 192
This figure shows a Flat Catalog design
59
STRUCTURING A SCHEMA
  • Schemas can be structured in a number of ways.
    One structure is called a Russian Doll design.
    This design involves sets of nested declarations.
  • While this design makes it easy to associate the
    schema with the instance document, it can be
    confusing and difficult to maintain.

60
RUSSIAN DOLL DESIGN
Figure 4-37 page 193
This figure shows a Russian Doll design
61
VENETIAN BLIND DESIGN
  • A Venetian blind design
  • Compromise between flat catalogs and Russian
    dolls
  • Element and attribute groups and named types are
    declared globally (and can be reused throughout
    the schema)
  • Elements and attributes for the instance document
    are local and nested

62
VENETIAN BLIND DESIGN
Figure 4-38 page 194
63
COMPARING SCHEMA DESIGNS
Figure 4-39 page 195
  • This figure compares the three schema designs

64
PLACING A SCHEMA IN A NAMESPACE TARGETING A
NAMESPACE
  • To associate a schema with a namespace
  • declare the namespace
  • prefixxmlns"uri"
  • make that namespace the target of the schema.
  • targetNamespace"uri"
  • Where
  • prefix prefix of the XML Schema namespace
  • uri URI of the target namespace

65
PLACING A SCHEMA IN A NAMESPACE TARGETING A
NAMESPACE
  • Example (Figure 4-40)
  • ltxsschema xmlnsxshttp//www.ww3.org/2001/XMLSch
    ema
  • xmlnshttp/uhosp.edu/patients/na
  • targetNamespacehttp//uhosp.edu/patien
    ts/nagt
  • ltxs element namepatientsgt
  • ltxscomplexTypegt
  • ltxssequencegt
  • ltxselement namepatient
    typepType
  • Minoccurs 1
    maxoccursunbounded /gt
  • lt/xssequencegt
  • lt/xscomplexTypegt
  • lt/xselementgt

Declare the namespace
Make the new namespace the target of the schema
66
APPLYING A SCHEMA TO A DOCUMENT WITH A NAMESPACE
  • To apply a schema to a document with a namespace,
    add the following attributes to the instance
    documents root element
  • xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
    ce"
  • xsischemaLocation"uri schema"
  • Where uri is the URI of the namespace and schema
    is the location and name of the schema file.
  • All global elements and attributes from the
    schema must be qualified in the instance document.

67
APPLYING A SCHEMA TO A DOCUMENT WITH A NAMESPACE
  • Example
  • ltpatients xmlnsxsihttp//www.W3.org/200/XMLSchem
    a-instance
  • xmlnshttp//uhosp.edu/patients/ns
  • xsischemaLocationhttp//uhosp.edu/patients/
    ns patvb.xsdgt
  • ltpatient patID MR890-041-02
  • .
  • lt/patientgt

68
INCLUDING AND IMPORTING SCHEMAS
  • To include a schema from the same namespace, add
    the following element as a child of the schema
    element
  • ltxsinclude schemaLocation"schema" /gt
  • Where schema is the name and location of the
    schema file.
  • To import a schema from a different namespace,
    use the syntax
  • ltxsimport namespace"uri" schemaLocation"schema"
    /gt
  • Where uri is the URI of the imported schemas
    namespace and schema is the name and location of
    the schema file.

69
REFERENCING OBJECTS FROM OTHER SCHEMAS
  • Once a schema is imported, any objects it
    contains with global scope can be referenced
  • To reference an object from an imported schema,
    you must declare the namespace of the imported
    schema in the schema element
  • You can then reference the object using the ref
    attribute or the type attribute for customized
    simple and complex types

70
Assignment Tutorial 4 Case Problem 1
  • Perform the tasks listed on pages 212 and 213
  • Create a CSS for the resulting jw.xml
  • Post your work, including the CSS on your web
    site.
  • End an e-mail with the subject Tutorial 4 Case
    Problem 1 by ltyour namegt to jim_at_larson-tech.com
    by Wednesday 1150 May 1
Write a Comment
User Comments (0)
About PowerShow.com