xml 2nd edition tutorial 4 working with schemas. xp schemas a schema is an xml document that defines...

67
XML 2nd EDITION Tutorial 4 Working With Schemas

Upload: grant-casey

Post on 05-Jan-2016

222 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XML2nd EDITION

Tutorial 4Working With Schemas

Page 2: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSchemas• 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.

Page 3: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPComparing Schemas and DTDs

This figure compares schemas and DTDs

Page 4: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSchema 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.

Page 5: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSchema Vocabularies

This figure shows a few schema vocabularies

Page 6: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPStarting A Schema File• A schema is always placed in a separate XML

document that is referenced by the instance document.

Page 7: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPExample 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>

Page 8: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPElements And Attributes Of The Patients Document

This figure shows the elements and attributes of the patients.xml document

Page 9: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSchema 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

Page 10: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSchema TypesThis figure shows types of elements

Page 11: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSimple 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 isan XML language

Page 12: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUnderstanding 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.

Page 13: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPDeclaring 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 attributetype = the data typedefault = the attribute’s default valuefixed = a fixed value for the attribute

• For examle:<xs:attribute name=“Gender” type=“xs:string”

default=“female” />

Page 14: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPAssociating 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.

Page 15: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPAssociating 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.

Page 16: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPEmpty 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

Page 17: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPEmpty 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:

<subject name=“Cynthia Dibbs” age=“62” />

Page 18: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSimple 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>

Page 19: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSimple Content And Attributes• 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• <performance scale=“Karnofskyk”>0.81</performance>

Page 20: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSpecifying 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”/>

Page 21: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPReferencing 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" />

whereelemName = the name used in an element declarationattName = the name used in an attribute declaration

Page 22: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPReferencing 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>

Page 23: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPWorking 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>

whereelements =the list of simple type element declarations for

each child elementcompositor = defines how the child elements are organized.

Page 24: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUsing 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.

Page 25: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUsing 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

Page 26: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUsing 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

Page 27: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSpecifying The Occurrences Of An Item• <xs:element name=“patient” type=“xs:string”

minOccurs=“1” maxOccurs=“3”/>

Page 28: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPWorking 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>

Page 29: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPChild 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>

Page 30: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPSpecifying Mixed Content<Summary>

Patient <Name>Cynthia Davis</Name> was enrolled inthe <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

Page 31: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPApplying 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-

instancexsi=SchemaLocation=“pschema.xsd”>

Commonly used for XML Schema

instances

Schema instance namespace

Page 32: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUnderstanding 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.

Page 33: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUnderstanding Data Types

This figure shows the 44 built-in data types

Page XML 168

Premitivetypes

DerivedTypes

Dates & times

Page 34: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUnderstanding Data Types

This figure shows a partial description of XML string data types

Page 35: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUsing Data TypesExamples:<xs:attribute name=“patID” type=“xs:ID” /><xs:attribute name=“onStudy” type=“xs:string” /><xs:attribute name=“scale” type=“xs:string” />

Page 36: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUnderstanding Data Types

This figure shows a partial description of XML numeric data types

Page 37: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUnderstanding Data Types

This figure shows a partial description of XML date and time data types

Page 38: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPDeriving 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,

Page 39: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPUser 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.

Page 40: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPList• List data type is a list of values separated by

white space• To create a list data type:

<xs:simpleType name=wbcList”> <xs:list itemType=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>

Page 41: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPDeriving 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.

Page 42: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPConstraining FacetsThis figure shows the 12 constraining facets

Page 43: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPConstraining 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

Page 44: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPThe 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.

Page 45: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPRegular 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] match any digit form 0-9– Etc.

Page 46: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPPattern Quantifiers

This figure shows pattern quantifiers

Page 47: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPExample 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>

Page 48: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPWorking 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”/>

Page 49: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPNamed Model Groups• A named model group is a collection, or group, of

elements. • Syntax

<xs:group name="name">elements

</xs:group>• Where name is the name of the model group, and

elements is a collection of element declarations

Page 50: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPNamed Model Groups Example• Definition

<xs:group name=“fullName”><xs:sequence>

<xs:element name="firstName" type="xs:string"/>

<xs:element name="lastName" type="xs:string" />

</xs:sequence></xs:group>

• Use<xs:element name=“client">

<xs:complexType><xs:group ref=“fullName”/>>

</xs:complexType></xs:element>

Page 51: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPWorking 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

<xs:attributeGroup name="name">attributes

</xs:attributeGroup>• Where name is the name of the attribute group and

attributes is a collection of attributes assigned to the group.

Page 52: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPWorking With Named Attribute Groups• Example:<xs:attributeGroup name=“DRInfo”> <xs:attribute name=“DRID” type=“xs:string” use=“required” /> <xs:attribute name=“dept” type=“xs:string” use=“required” /><xs:attributeGroup>

<xs:element name=“doctor” type=“deptData”/><xs:complexType name = “deptData”> <xs:simplecontent> <xs: extension base = “string”> <xs:attributegroup ref=“DRIinfo”/> </xs:extenson> </xs:simplecontent></xs:complexType>

<doctor DRID=“DR251” dept=“Pediatrics”> curt Hurley<doctor>

Page 53: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPScope• 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

Page 54: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPStructuring 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

Page 55: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPStructuring 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.

Page 56: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPFlat Catalog DesignThis figure shows a Flat Catalog design

Page 57: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPStructuring 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.

Page 58: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPRussian Doll DesignThis figure shows a Russian Doll design

Page 59: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPVENETIAN 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

Page 60: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPVenetian Blind Design

Page 61: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPComparing Schema DesignsThis figure compares the three schema designs

Page 62: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPPlacing A Schema In A Namespace: Targeting A Namespace• To associate a schema with a namespace– declare the namespace

prefix:xmlns="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

Page 63: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPPlacing A Schema In A Namespace: Targeting A Namespace• Example (Figure 4-40):<xs:schema xmlns:xs=http://www.ww3.org/2001/XMLSchema xmlns=“http:/uhosp.edu/patients/na” targetNamespace=“http://uhosp.edu/patients/na>

<xs: element name=“patients”> <xs:complexType> <xs:sequence> <xs:element name=“patient” type=“pType” Minoccurs = “1” maxoccurs=“unbounded” /> </xs:sequence> </xs:complexType></xs:element>

Declare the namespace

Make the new namespace the target

of the schema

Page 64: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPApplying 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 document’s root element:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="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.

Page 65: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPApplying A Schema To A Document With A Namespace<patients

xmlns:xsi=http://www.W3.org/200/XMLSchema-instance

xmlns=“http://uhosp.edu/patients/ns xsi:schemaLocation=http://uhosp.edu/patients/ns

patvb.xsd>

<patient patID = “MR890-041-02” … ….</patient>

Page 66: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPIncluding And Importing Schemas• To include a schema from the same namespace, add the

following element as a child of the schema element:<xs:include schemaLocation="schema" />

Where schema is the name and location of the schema file.

• To import a schema from a different namespace, use the syntax

<xs:import namespace="uri" schemaLocation="schema" />• Where uri is the URI of the imported schema’s

namespace and schema is the name and location of the schema file.

Page 67: XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents

XPXPXPXPXPReferencing 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