Tutorial 13 Validating Documents with Schemas

Slides:



Advertisements
Similar presentations
1 Web Data Management XML Schema. 2 In this lecture XML Schemas Elements v. Types Regular expressions Expressive power Resources W3C Draft:
Advertisements

Tutorial 6 Creating a Web Form
1 XML DTD & XML Schema Monica Farrow G30
Tutorial 9 Working with XHTML
XML Schemas Microsoft XML Schemas W3C XML Schemas.
Lecture 14 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
Tutorial 9 Working with XHTML. XP Objectives Describe the history and theory of XHTML Understand the rules for creating valid XHTML documents Apply a.
Sunday, June 28, 2015 Abdelali ZAHI : FALL 2003 : XML Schemas XML Schemas Presented By : Abdelali ZAHI Instructor : Dr H.Haddouti.
Tutorial 11 Creating XML Document
Document Type Definitions. XML and DTDs A DTD (Document Type Definition) describes the structure of one or more XML documents. Specifically, a DTD describes:
VALIDATING AN XML DOCUMENT
Unit 4 – XML Schema XML - Level I Basic.
Introduction to XML This material is based heavily on the tutorial by the same name at
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
Working with Namespaces Combining XML Vocabularies in a Compound Document.
WORKING WITH NAMESPACES
XP New Perspectives on XML Tutorial 4 1 XML Schema Tutorial – Carey ISBN Working with Namespaces and Schemas.
Tutorial 3: XML Creating a Valid XML Document. 2 Creating a Valid Document You validate documents to make certain necessary elements are never omitted.
XP New Perspectives on XML Tutorial 3 1 DTD Tutorial – Carey ISBN
Lecture 15 XML Validation. a simple element containing text attribute; attributes provide additional information about an element and consist of a name.
Validating DOCUMENTS with DTDs
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation XML Schema 1 Lecturer.
XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML.
1 XML Schemas. 2 Useful Links Schema tutorial links:
Dr. Azeddine Chikh IS446: Internet Software Development.
Neminath Simmachandran
CSE4500 Information Retrieval Systems XML Schema – Part 1.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML.
XML CPSC 315 – Programming Studio Fall 2008 Project 3, Lecture 1.
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
Li Tak Sing COMPS311F. XML Schemas XML Schema is a more powerful alternative to DTD to describe XML document structures. The XML Schema language is also.
XP 1 DECLARING A DTD A DTD can be used to: –Ensure all required elements are present in the document –Prevent undefined elements from being used –Enforce.
1 Tutorial 13 Validating Documents with DTDs Working with Document Type Definitions.
Avoid using attributes? Some of the problems using attributes: Attributes cannot contain multiple values (child elements can) Attributes are not easily.
Of 33 lecture 3: xml and xml schema. of 33 XML, RDF, RDF Schema overview XML – simple introduction and XML Schema RDF – basics, language RDF Schema –
Copyrighted material John Tullis 10/17/2015 page 1 04/15/00 XML Part 3 John Tullis DePaul Instructor
New Perspectives on XML, 2nd Edition
XML Schema. Why Schema? To define a class of XML documents Serve same purpose as DTD “Instance document" used for XML document conforming to schema.
An OO schema language for XML SOX W3C Note 30 July 1999.
XP New Perspectives on XML, 2nd Edition Tutorial 2 1 TUTORIAL 2 WORKING WITH NAMESPACES.
Lecture 16 Introduction to XML Boriana Koleva Room: C54
XML – Part III. The Element … This type of element either has the element content or the mixed content (child element and data) The attributes of the.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 5 XML Schema (Based on Møller and Schwartzbach,
Sheet 1XML Technology in E-Commerce 2001Lecture 2 XML Technology in E-Commerce Lecture 2 Logical and Physical Structure, Validity, DTD, XML Schema.
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.
1 Tutorial 14 Validating Documents with Schemas Exploring the XML Schema Vocabulary.
1 Tutorial 12 Working with Namespaces Combining XML Vocabularies in a Compound Document.
Processing of structured documents Spring 2003, Part 3 Helena Ahonen-Myka.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.2.
Working with XML Schemas ©NIITeXtensible Markup Language/Lesson 3/Slide 1 of 36 Objectives In this lesson, you will learn to: * Declare attributes in an.
XML Validation II Schemas Robin Burke ECT 360. Outline Namespaces Documents  Data types XML Schemas Elements Attributes Derived data types RELAX NG.
XML Module 1 Creating an XML Document. XP Objectives Session 1.1 – Describe the history of XML and the uses of XML documents – Understand XML vocabularies.
XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Deriving Complex Types In XML Schema By: Roy Navon.
XP Tutorial 9New Perspectives on HTML and XHTML, Comprehensive 1 Working with XHTML Creating a Well-Formed Valid Document Tutorial 9.
XML Schema Lecture 3. Indicators There are seven indicators: Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators:
XML Validation II Advanced DTDs + Schemas Robin Burke ECT 360.
Tutorial 9 Working with XHTML. New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition 2 Objectives Describe the history and theory of XHTML.
Tutorial 9 Working with XHTML. XP Objectives Describe the history and theory of XHTML Understand the rules for creating valid XHTML documents Apply a.
Tutorial 6 Creating a Web Form
CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting.
XML SCHEMA 1 CH 20. Objective 2 What’s wrong with DTDs? What is a schema? The W3C XML Schema Language Hello schemas Complex types Simple types Deriving.
4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema.
XML Schemas Dr. Awad Khalil Computer Science Department AUC.
Tutorial 9 Working with XHTML
WORKING WITH NAMESPACES
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
New Perspectives on XML
Working with Namespaces and Schemas
New Perspectives on XML
Presentation transcript:

Tutorial 13 Validating Documents with Schemas

Objectives Compare schemas and DTDs Explore different schema vocabularies Declare simple type elements and attributes Declare complex type elements Apply a schema to an instance document Work with XML Schema data types Derive new data types for text strings, numeric values, and dates Create data types for patterned data using regular expressions New Perspectives on HTML, CSS, and XML 4th edition

The Limits of DTDs DTDs are commonly used for validation largely because of XML’s origins as an offshoot of SGML. One complaint about DTDs is their lack of data types. DTDs also do not recognize namespaces, so they are not well suited to compound documents in which content from several vocabularies needs to be validated. DTDs employ a syntax called Extended Backus–Naur Form (EBNF), which is different from the syntax used for XML. New Perspectives on HTML, CSS, and XML 4th edition

Schemas and DTDs A schema is an XML document that contains validation rules for an XML vocabulary. When applied to a specific XML file, the document to be validated is called the instance document. New Perspectives on HTML, CSS, and XML 4th edition

Schemas and DTDs New Perspectives on HTML, CSS, and XML 4th edition

Schema Vocabularies A single standard doesn’t exist for schemas. A schema vocabulary is simply an XML vocabulary created for the purpose of describing schema content. Support for a particular schema depends solely on the XML parser being used for validation. New Perspectives on HTML, CSS, and XML 4th edition

Schema Vocabularies New Perspectives on HTML, CSS, and XML 4th edition

Starting a Schema File A schema, is always placed in an external XML file. XML Schema filenames end with the .xsd file extension. The root element in any XML Schema document is the schema element. The general structure of an XML Schema file is: <?xml version=”1.0” ?> <schema xmlns=”http://www.w3.org/2001/XMLSchema”> content </schema> New Perspectives on HTML, CSS, and XML 4th edition

Starting a Schema File By convention, the namespace prefix xsd or xs is assigned to the XML Schema namespace to identify elements and attributes that belong to the XML Schema vocabulary. The usual form of an XML Schema document is: <?xml version=”1.0” ?> <xs:schema xmlns=”http://www.w3.org/2001/XMLSchema”> content </xs:schema> New Perspectives on HTML, CSS, and XML 4th edition

Understanding Simple and Complex Types XML Schema supports two types of content—simple and complex. A simple type contains only text and no nested elements. A complex type contains two or more values or elements placed within a defined structure. New Perspectives on HTML, CSS, and XML 4th edition

Understanding Simple and Complex Types New Perspectives on HTML, CSS, and XML 4th edition

Understanding Simple and Complex Types New Perspectives on HTML, CSS, and XML 4th edition

Defining a Simple Type Element An element in the instance document containing only text and no attributes or child elements is defined in XML Schema using the <xs:element> tag: <xs:element name=”name” type=”type” /> Here name is the name of the element in the instance document and type is the type of data stored in the element. If you use a different namespace prefix or declare XML Schema as the default namespace for the document, the prefix will be different. New Perspectives on HTML, CSS, and XML 4th edition

Data Types The data type can be: one of XML Schema’s built-in data types, defined by the schema author, or user data type. The most commonly used data type in XML Schema is string, which allows an element to contain any text string. Example: <xs:element name=”lastName” type=”xs:string” /> Another popular data type in XML Schema is decimal, which allows an element to contain a decimal number. New Perspectives on HTML, CSS, and XML 4th edition

Defining a Simple Type Element New Perspectives on HTML, CSS, and XML 4th edition

Defining an Attribute To define an attribute in XML Schema, you use the <xs:attribute> tag: <xs:attribute name=”name” type=”type” default=”default” fixed=”fixed” /> Here name is the name of the attribute, type is the data type, default is the attribute’s default value, and fixed is a fixed value for the attribute. The default and fixed attributes are optional. New Perspectives on HTML, CSS, and XML 4th edition

Defining an Attribute New Perspectives on HTML, CSS, and XML 4th edition

Defining a Complex Type Element The basic structure for defining a complex type element with XML Schema is <xs:element name=”name”> <xs:complexType> declarations </xs:complexType> </xs:element> Here name is the name of the element and declarations represents declarations of the type of content within the element. New Perspectives on HTML, CSS, and XML 4th edition

Defining a Complex Type Element This content could include nested child elements, basic text, attributes, or any combination of the three: An empty element containing only attributes An element containing text content and attributes but no child elements An element containing child elements but no attributes An element containing both child elements and attributes New Perspectives on HTML, CSS, and XML 4th edition

Defining an Element Containing Attributes and Basic Text The definition needs to indicate that the element contains simple content and a collection of one or more attributes. The structure of the element definition is: <xs:element name=”name”> <xs:complexType> <xs:simpleContent> <xs:extension base=”type”> attributes </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> New Perspectives on HTML, CSS, and XML 4th edition

Defining an Element Containing Attributes and Basic Text Example: <xs:element name=”gpa”> <xs:complexType> <xs:simpleContent> <xs:extension base=”xs:string”> <xs:attribute name=”degree” type=”xs:string” /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> The base attribute in the <xs:extension> element sets the data type for the gpa element. It also sets the data type of the degree attribute to xs:string. New Perspectives on HTML, CSS, and XML 4th edition

Referencing an Element or Attribute Definition XML Schema allows for a great deal of flexibility in writing complex types. Rather than repeating that earlier attribute declaration, you can create a reference to it. A reference to an element definition is <xs:element ref=”elemName” /> where elemName is the name used in the element definition. A reference to an attribute definition is <xs:attribute ref=”attName” /> where attName is the name used in the attribute definition. New Perspectives on HTML, CSS, and XML 4th edition

Defining an Element with Nested Children Complex elements that contain nested child elements but no attributes or text: <xs:element name=”name”> <xs:complexType> <xs:compositor> elements </xs:compositor> </xs:complexType> </xs:element> where name is the name of the element, compositor is a value that defines how the child elements appear in the document, and elements is a list of the nested child elements. New Perspectives on HTML, CSS, and XML 4th edition

Defining an Element with Nested Children The following compositors are supported: sequence - requires the child elements to appear in the order listed in the schema choice - allows any one of the child elements listed to appear in the instance document all - allows any of the child elements to appear in any order in the instance document; however, each may appear only once, or not at all New Perspectives on HTML, CSS, and XML 4th edition

Defining an Element with Nested Children - Examples New Perspectives on HTML, CSS, and XML 4th edition

Defining an Element Containing Nested Elements and Attributes The code for a complex type element that contains both child elements and attributes is: <xs:element name="name"> <xs:complexType> <xs:compositor> elements </xs:compositor> attributes </xs:complexType> </xs:element> where name is the name of the element; compositor is either sequence, choice, or all; elements is a list of nested child elements; and attributes is a list of attribute definitions. New Perspectives on HTML, CSS, and XML 4th edition

Defining an Element Containing Nested Elements and Attributes Example: New Perspectives on HTML, CSS, and XML 4th edition

Specifying Mixed Content An element is said to have mixed content when it contains both a text string and child elements. XML Schema assumes that the element contains both text and child elements. The structure of the child elements can then be defined with the conventional method. New Perspectives on HTML, CSS, and XML 4th edition

Specifying Mixed Content <summary> student <firstName>Cynthia</firstName> <lastName>Berstein</lastName> is enrolled in an IT degree program and has completed <credits>12</credits> credits since 01/01/2012. </summary> The summary element for this document in a schema file can be declared using the following complex type: <element name=”summary”> <complexType mixed=”true”> <sequence> <element name=”firstName” type=”string” /> <element name=”lastName” type=”string” /> <element name=”credits” type=”string” /> </sequence> </complexType> </element> New Perspectives on HTML, CSS, and XML 4th edition

Indicating Required Attributes To indicate whether an attribute is required, the use attribute can be added to the statement that assigns the attribute to an element: <xs:element name=”name”> <xs:complexType> element content <xs:attribute properties use=”use” /> </xs:complexType> </xs:element> New Perspectives on HTML, CSS, and XML 4th edition

Indicating Required Attributes use is one of the following three values: required - The attribute must always appear with the element. optional - The use of the attribute is optional with the element. prohibited - The attribute cannot be used with the element. Example: <xs:attribute name=”degree” type=”xs:string” use=”required” /> New Perspectives on HTML, CSS, and XML 4th edition

Specifying the Number of Child Elements To specify the number of times an element appears in the instance document, you can apply the minOccurs and maxOccurs attributes to the element definition: <xs:element name=”name” type=”type” minOccurs=”value” maxOccurs=”value” /> The value of the minOccurs attribute defines the minimum number of times the element can occur, and the value of the maxOccurs attribute defines the maximum number of times the element can occur. New Perspectives on HTML, CSS, and XML 4th edition

Validating a Schema Document New Perspectives on HTML, CSS, and XML 4th edition

Applying a Schema to an Instance Document To attach a schema to an instance document, you: Declare the XML Schema instance namespace in the instance document. Specify the location of the schema file. To declare the XML Schema instance namespace, you add the following attribute to the root element of the instance document: xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” New Perspectives on HTML, CSS, and XML 4th edition

Applying a Schema to an Instance Document You add a second attribute to the root element to specify the location of the schema file. The attribute you use depends on whether the instance document is associated with a namespace. If the document is not associated with a namespace, you add the attribute: xsi:noNamespaceSchemaLocation=”schema” to the root element, where schema is the location and name of the schema file. New Perspectives on HTML, CSS, and XML 4th edition

Validating with Built-In Data Types XML Schema divides its built-in data types into two classes—primitive and derived. A primitive data type, also called a base type, is one of 19 fundamental data types that are not defined in terms of other types. A derived data type is one of 25 data types that are developed from one of the base types. New Perspectives on HTML, CSS, and XML 4th edition

Validating with Built-In Data Types New Perspectives on HTML, CSS, and XML 4th edition

String Data Types New Perspectives on HTML, CSS, and XML 4th edition

Numeric Data Types New Perspectives on HTML, CSS, and XML 4th edition

Date and Time Data Types New Perspectives on HTML, CSS, and XML 4th edition

Deriving Customized Data Types The code to derive a new data type is: <xs:simpleType name=”name”> rules </xs:simpleType> Here name is the name of the user-defined data type and rules is the list of statements that define the properties of that data type. This structure is also known as a named simple type. You can also create a simple type without a name, which is known as an anonymous simple type. New Perspectives on HTML, CSS, and XML 4th edition

Deriving Customized Data Types The following three components are involved in deriving any new data type: 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 that distinguish one data type from another. New Perspectives on HTML, CSS, and XML 4th edition

Deriving Customized Data Types New data types are created by manipulating the properties of value space, lexical space, and facets. It can be done by: 1. Creating a list based on preexisting data types. 2. Creating a union of one or more of the preexisting data types. 3. Restricting the values of a preexisting data type. New Perspectives on HTML, CSS, and XML 4th edition

Deriving a List Data Type A list data type is a list of values separated by white space, in which each item in the list is derived from an established data type. The syntax for deriving a customized list data type is: <xs:simpleType name=”name”> <xs:list itemType=”type” /> </xs:simpleType> Here name is the name assigned to the list data type and type is the data type from which each item in the list is derived. New Perspectives on HTML, CSS, and XML 4th edition

Deriving a Union Data Type A union data type is based on the value and/or lexical spaces from two or more preexisting data types. Each base data type is known as a member data type. The syntax is: <xs:simpleType name=”name”> <xs:union memberTypes=”type1 type2 type3 ...” /> </xs:simpleType> where type1, type2, type3, etc., are the member types that constitute the union. New Perspectives on HTML, CSS, and XML 4th edition

Deriving a Union Data Type XML Schema also allows unions to be created from nested simple types. The syntax is: <xs:simpleType name=”name”> <xs:union> <xs:simpleType> rules1 </xs:simpleType> <xs:simpleType> rules2 </xs:simpleType> ... </xs:union> </xs:simpleType> where rules1, rules2, etc., are rules for creating different user-derived data types. New Perspectives on HTML, CSS, and XML 4th edition

Deriving a Restricted Data Type New Perspectives on HTML, CSS, and XML 4th edition

Constraining Facets Constraining facets are applied to a base type using the structure: <xs:simpleType name=”name”> <xs:restriction base=”type”> <xs:facet1 value=”value1” /> <xs:facet2 value=”value2” /> ... </xs:restriction> </xs:simpleType> where type is the data type on which the restricted data type is based; facet1, facet2, etc., are constraining facets; and value1, value2, etc., are values for the constraining facets. New Perspectives on HTML, CSS, and XML 4th edition

Deriving Data Types Using Regular Expressions A regular expression is a text string that defines a character pattern. Regular expressions can be created to define patterns for many types of data, including phone numbers, postal address codes, and e-mail addresses. New Perspectives on HTML, CSS, and XML 4th edition

Deriving Data Types Using Regular Expressions To apply a regular expression in a data type, you create the simple type: <xs:simpleType name=”name”> <xs:restriction base=”type”> <xs:pattern value=”regex” /> </xs:restriction> </xs:simpleType> where regex is a regular expression pattern. Example: <xs:pattern value=”ABC” /> New Perspectives on HTML, CSS, and XML 4th edition

Regular Expression Character Types Character types are representations of different kinds of characters. The general form of a character type is: \char New Perspectives on HTML, CSS, and XML 4th edition

Common Regular Expression Character Sets Characters can also be grouped into lists called character sets that specify exactly what characters or ranges of characters are allowed in the pattern. The syntax of a character set is: [chars] New Perspectives on HTML, CSS, and XML 4th edition

Regular Expression Quantifiers To specify the number of occurrences for a particular character or group of characters, a quantifier can be appended to a character type or set. New Perspectives on HTML, CSS, and XML 4th edition

Applying Regular Expression New Perspectives on HTML, CSS, and XML 4th edition