Markup Languages. Lecture 4. XML Schema

Similar documents
Software Engineering Methods, XML extensible Markup Language. Tutorial Outline. An Example File: Note.xml XML 1

XML extensible Markup Language

Introduction to XML Schema. The XML Schema language is also referred to as XML Schema Definition (XSD).

Agenda. XML document Structure. XML and HTML were designed with different goals: XML was designed to describe data and to focus on what

Semantic Web. XML and XML Schema. Morteza Amini. Sharif University of Technology Fall 94-95

The <schema> Element. <?xml version="1.0"?> <xs:schema>... </xs:schema>

X(ml)S(chema)D(definition) Complex Types Indicators

So far, we've discussed the use of XML in creating web services. How does this work? What other things can we do with it?

extensible Markup Language (XML)

Ontologies and Schema Languages on the Web. Kaveh Shahbaz

XML (Extensible Markup Language)

XML Schema. Mario Alviano A.Y. 2017/2018. University of Calabria, Italy 1 / 28

27/02/2013. XML Schema. Nội dung. 1. Giới thiệu. XML Schema hỗ trợ các kiểu dữ liệu. Giới thiệu Các kiểu ñơn giản Các kiểu phức tạp Kiểu dữ liệu

Restricting complextypes that have mixed content

XML: extensible Markup Language

Tecnologie per XML. Sara Comai Politecnico di Milano. Tecnologie legate a XML

Syntax XML Schema XML Techniques for E-Commerce, Budapest 2004

Introduction Syntax and Usage XML Databases Java Tutorial XML. November 5, 2008 XML

2006 Martin v. Löwis. Data-centric XML. XML Schema (Part 1)

Last week we saw how to use the DOM parser to read an XML document. The DOM parser can also be used to create and modify nodes.

The main problem of DTD s...

Introducing our First Schema

PESC Compliant JSON Version /19/2018. A publication of the Technical Advisory Board Postsecondary Electronic Standards Council

MANAGING INFORMATION (CSCU9T4) LECTURE 2: XML STRUCTURE

XML Format Plug-in User s Guide. Version 10g Release 3 (10.3)

XMLSchema2ShEx: Converting XML validation to RDF validation

XML. Document Type Definitions XML Schema. Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

XML Schema Element and Attribute Reference

PESC Compliant JSON Version /04/2019

Chapter 11 XML Data Modeling. Recent Development for Data Models 2016 Stefan Deßloch

Author: Irena Holubová Lecturer: Martin Svoboda

Schema schema-for-json.xsd

Jeff Offutt. SWE 642 Software Engineering for the World Wide Web

ՕՐԻՆԱԿ. <xs:schema targetnamespace=" xmlns:tax="

TED schemas. Governance and latest updates

Public Education Information Management System (PEIMS)

Public Education Information Management System (PEIMS)

/// Rapport. / Testdocumentatie nieuwe versie Register producten en dienstverlening (IPDC)

extensible Markup Language

Appendix H XML Quick Reference

Modelling XML Applications (part 2)

XML Technologies. Doc. RNDr. Irena Holubova, Ph.D. Web pages:

XML and Web Services

<xs:element name="record" type="recordtype" maxoccurs="unbounded" /> </xs:sequence> </xs:complextype>

extensible Markup Language

Internet Technologies 11-XML. F. Ricci 2010/2011

WA2217 Programming Java EE 6 SOAP Web Services with JAX-WS - JBoss / Eclipse EVALUATION ONLY

COMP9321 Web Application Engineering. Extensible Markup Language (XML)

Overview. Introduction to XML Schemas. Tutorial XML Europe , Berlin. 1 Introduction. 2 Concepts. 3 Schema Languages.

XML-Schema Quick Start * Draft

No Trade Secrets. Microsoft does not claim any trade secret rights in this documentation.

Big Data Exercises. Fall 2018 Week 8 ETH Zurich. XML validation

Messages are securely encrypted using HTTPS. HTTPS is the most commonly used secure method of exchanging data among web browsers.

! "# # $ % & ' ( ' )* ) & %*+ *

Using and defining new simple types. Basic structuring of definitions. Copyright , Sosnoski Software Solutions, Inc. All rights reserved.

EVALUATION COPY. Contents

Java EE 7: Back-end Server Application Development 4-2

Notes. Any feedback/suggestions? IS 651: Distributed Systems

Introduction to XML DTD

7.1. Redovna datoteka sa slogovima koji se odnose na kupnje i prodaje valuta na tržištu stranih sredstava plaćanja

Intellectual Property Rights Notice for Open Specifications Documentation

Big Data 9. Data Models

AlwaysUp Web Service API Version 11.0

Automatic Configurator for Application Code

Technical requirements

4. XML Schema 4-2. Objectives. After completing this chapter, you should be able to: explain why DTDs are not sufficient for many applications.

Document erratum applies to QosDevice:1. List other Erratum s or Documents that this change may apply to or have associated changes with

COMP60411 Modelling Data on the Web XPath, XML Schema, and XQuery, week 3

No Trade Secrets. Microsoft does not claim any trade secret rights in this documentation.

XML and Content Management

[MS-MSL]: Mapping Specification Language File Format. Intellectual Property Rights Notice for Open Specifications Documentation

[MS-SSISPARAMS-Diff]: Integration Services Project Parameter File Format. Intellectual Property Rights Notice for Open Specifications Documentation

Área d Organització i Sistemes d Informació Centre de Sistemes d Informació Servei de desenvolupament SARA 600 MISSATGES XML

COMP60411 Modelling Data on the Web XPath, XML Schema, and XQuery. Week 3

[MS-OXWSSYNC]: Mailbox Contents Synchronization Web Service Protocol Specification

QosPolicyHolder:1 Erratum

Big Data Fall Data Models

Combining graph and tree: writing SHAX, obtaining SHACL, XSD and more

Languages of the WEB. Jukka K. Nurminen

HVDC LINK DOCUMENT UML MODEL AND SCHEMA

No Trade Secrets. Microsoft does not claim any trade secret rights in this documentation.

G16-02SE-A02.1-NUE112_SchedaContatto-Rev5 <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="

UNAVAILABILITY DOCUMENT UML MODEL AND SCHEMA

XML DTDs and Namespaces. CS174 Chris Pollett Oct 3, 2007.

SEMANTIC AND TECHNICAL STRUCTURE OF ELECTRONIC MESSAGES STORAGE AND RETRIEVAL OF MODELS

PLANNED RESOURCE SCHEDULE DOCUMENT UML MODEL AND SCHEMA

Altova XMLSpy 2007 Tutorial

Altova XMLSpy 2013 Tutorial

X3D Unit Specification Updates Myeong Won Lee The University of Suwon

Web Data Management. Typing Semistructured Data. Philippe Rigaux CNAM Paris & INRIA Saclay

RESOURCE SCHEDULE CONFIRMATION DOCUMENT UML MODEL AND SCHEMA

ASSISTments Interactive Qualifying Project

Apache UIMA Regular Expression Annotator Documentation

file://c:\michael\prog\mast\docs\tmp0000.xsd

Chapter 3 Brief Overview of XML

<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:types=" xmlns:xs="

Brief guide for XML, XML Schema, XQuery for YAWL data perspective

CountryData Technologies for Data Exchange. Introduction to XML

INFORMATION TECHNOLOGY. Automated Railcar Release, Switch and Resequence XML and WSDL documentation

VDS Service Broker APIs

Transcription:

Markup Languages Lecture 4. XML Schema

Introduction to XML Schema XML Schema is an XML-based alternative to DTD. An XML schema describes the structure of an XML document. The XML Schema language is also referred to as XML Schema Definition (XSD). XML Schema became a W3C Recommendation 02. May 2001. 2

XML Schemas are the Successors of DTDs XML Schemas are extensible to future additions XML Schemas are richer and more powerful than DTDs XML Schemas are written in XML XML Schemas support data types XML Schemas support namespaces 3

XSD How To? XML: <?xml version="1.0"?> <?xml-stylesheet...?> <catalog> <cd> <title>empire Burlesgue </title> <artist>bob Dylan </artist> </cd> <cd> <title>live A Paris</title> <artist>celin Dion </artist> </cd> </catalog> DTD: <!ELEMENT catalog (cd+)> <!ELEMENT cd (title, artist)> <!ELEMENT title (#PCDATA)> <!ELEMENT artist (#PCDATA)> XML Schema: <xs:element name="catalog"> <xs:complextype> <xs:sequence> <xs:element name="cd maxoccurs="unbounded"> <xs:complextype> <xs:sequence> <xs:element name= title" type= xs:string /> <xs:element name= artist" type= xs:string /> <xs:sequence> </xs:complextype> </xs:sequence> </xs:complextype> 4

Compare References to DTD and XML Schema A Reference to a DTD: <?xml version="1.0"?> <!DOCTYPE catalog SYSTEM "cd.xsd"> <catalog> <cd> <title>empire Burlesgue</title> <artist>bob Dylan</artist> </catalog> A Reference to an XML Schema: <?xml version="1.0"?> <catalog xmlns:xsi="http:// www.w3.org/2001/xmlschemainstance" xsi:schemalocation="cd.xsd"> <cd> <title>empire Burlesgue</title> <artist>bob Dylan</artist> </catalog> 5

XML Schema XML-file: cd.xml <?xml version="1.0"> <catalog xmlns:xsi="http://www.w3.org/ 2001/XMLSchema-instance" xsi:schemalocation="cd.xsd"> <cd> <title>empire burlesque</title> <artist>bob dylan</artist> </cd>... </catalog> XSD-file: cd.xsd <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/... </xs:schema> 2001/XMLSchema"> 6

XSD Elements Simple Element A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. 7

XSD Simple Elements The text can be of many different types. It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself. The syntax for defining a simple element is: <xs:element name="name_element" type="name_type"/> XML Schema has a lot of built-in data types. The most common types are: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time 8

Simple Elements Example <lastname>refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn> <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> 9

Default and Fixed Values for Simple Elements Simple elements may have a default value OR a fixed value specified. A default value is automatically assigned to the element when no other value is specified. Example: <xs:element name="color" type="xs:string" default="red"/> A fixed value is also automatically assigned to the element, and you cannot specify another value. Example: <xs:element name="color" type="xs:string" fixed="red"/> 10

Task Write XSD rules for next bold elements: <person> <surname>ivanova</surname> <name>elena</name> <email>elene@gmail.com</email> <gender>female</gender> <date_birth>04.06.2013</date_birth> <marital_status>married</marital_status> <cell>89123456789</cell> </person> <objective> <post>programmer</post> <salary>100000</salary> </objective> 11

XSD Attributes Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type. <xs:attribute name="name_attr" type="name_type"/> Example: <lastname lang="en">smith</lastname> <xs:attribute name="lang" type="xs:string"/> 12

Default and Fixed Values for Attributes Attributes may have a default value OR a fixed value specified. A default value is automatically assigned to the attribute when no other value is specified. Example: <xs:attribute name="lang" type="xs:string" default="en"/> A fixed value is also automatically assigned to the attribute, and you cannot specify another value. Example: <xs:attribute name="lang" type="xs:string" fixed="en"/> 13

Optional and Required Attributes Attributes are optional by default. To specify that the attribute is required, use the "use" attribute: <xs:attribute name="lang" type="xs:string" use="required"/> 14

Restrictions on Content When an XML element or attribute has a data type defined, it puts restrictions on the element's or attribute's content. Example: If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate. 15

XSD Restrictions/Facets With XML Schemas, you can also add your own restrictions to your XML elements and attributes. These restrictions are called facets. 16

Restrictions on Values The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120: <xs:element name="age"> <xs:simpletype> <xs:restriction base="xs:integer"> <xs:mininclusive value="0"/> <xs:maxinclusive value="120"/> </xs:restriction> </xs:simpletype> 17

Restrictions on a Set of Values To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint. <xs:element name="car"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:enumeration value="audi"/> <xs:enumeration value="golf"/> <xs:enumeration value="bmw"/> </xs:restriction> </xs:simpletype> 18

Restrictions on a Series of Values To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint. The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z: <xs:element name="letter"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpletype> 19

Restrictions on a Series of Values The next example defines an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z: <xs:element name="initials"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-z][a-z][a-z]"/> </xs:restriction> </xs:simpletype> 20

Restrictions on a Series of Values The next example also defines an element called "initials" with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z: <xs:element name="initials"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-z][a-z][a-z]"/> </xs:restriction> </xs:simpletype> 21

Restrictions on a Series of Values The next example defines an element called "choice" with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z: <xs:element name="choice"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[xyz]"/> </xs:restriction> </xs:simpletype> 22

Restrictions on a Series of Values The next example defines an element called "prodid" with a restriction. The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9: <xs:element name="prodid"> <xs:simpletype> <xs:restriction base="xs:integer"> <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> </xs:restriction> </xs:simpletype> 23

Other Restrictions on a Series of Values The example below defines an element called "letter" with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z: <xs:element name="letter"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction> </xs:simpletype> 24

Other Restrictions on a Series of Values The next example also defines an element called "letter" with a restriction. The acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an upper case letter. For example, "stop" will be validated by this pattern, but not "Stop" or "STOP" or "stop": <xs:element name="letter"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][a-z])+"/> </xs:restriction> </xs:simpletype> 25

Other Restrictions on a Series of Values The next example defines an element called "gender" with a restriction. The only acceptable value is male OR female: <xs:element name="gender"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="male female"/> </xs:restriction> </xs:simpletype> 26

Other Restrictions on a Series of Values The next example defines an element called "password" with a restriction. There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9: <xs:element name="password"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-za-z0-9]{8}"/> </xs:restriction> </xs:simpletype> > 27

Restrictions on Length To limit the length of a value in an element, we would use the length, maxlength, and minlength constraints. This example defines an element called "password" with a restriction. The value must be exactly eight characters: <xs:element name="password"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpletype> 28

Restrictions on Length This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters: <xs:element name="password"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:minlength value="5"/> <xs:maxlength value="8"/> </xs:restriction> </xs:simpletype> 29

Restrictions on Whitespace Characters To specify how whitespace characters should be handled, we would use the whitespace constraint. This example defines an element called "address" with a restriction. The whitespace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters: <xs:element name="address"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:whitespace value="preserve"/> </xs:restriction> </xs:simpletype> 30

Restrictions on Whitespace Characters This example also defines an element called "address" with a restriction. The whitespace constraint is set to "replace", which means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces: <xs:element name="address"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:whitespace value="replace"/> </xs:restriction> </xs:simpletype> 31

Restrictions for Datatypes Constraint enumeration fractiondigits length maxexclusive maxinclusive maxlength minexclusive mininclusive minlength pattern totaldigits whitespace Description Defines a list of acceptable values Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero Specifies the upper bounds for numeric values (the value must be less than this value) Specifies the upper bounds for numeric values (the value must be less than or equal to this value) Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero Specifies the lower bounds for numeric values (the value must be greater than this value) Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero Defines the exact sequence of characters that are acceptable Specifies the exact number of digits allowed. Must be greater than zero Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled 32

Task Write XSD rules for next bold elements: <person> <surname>ivanova</surname> <name>elena</name> <email>elene@gmail.com</email> <gender>female</gender> <date_birth>04.06.2013</date_birth> <marital_status>married</marital_status> <cell>8(912)345-67-89</cell> </person> <objective> <post>programmer</post> <salary>100000</salary> 5 000 <= salary < 500 000 </objective> 33

XSD Complex Elements A complex element is an XML element that contains other elements and/or attributes. Examples: <product pid="1345"/> <employee> <firstname>john</firstname> <lastname>smith</lastname> </employee <food type="dessert">ice cream</food> <description> It happened on <date lang="norwegian">03.03.99</date> </description> 34

How to Define a Complex Element <employee> <firstname>john</firstname> <lastname>smith</lastname> </employee> <xs:element name="employee"> <xs:complextype> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> <xs:element name="employee" type="personinfo"/> <xs:complextype name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> 35

XSD Elements Only <person> <firstname>john</firstname> <lastname>smith</lastname> </person> <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> 36

XSD Order Indicators: Sequence The <sequence> indicator specifies that the child elements must appear in a specific order: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complextype> 37

XSD Order Indicators: all The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once: <xs:element name="person"> <xs:complextype> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complextype> 38

XSD Order Indicators: choice The <choice> indicator specifies that either one child element or another can occur: <xs:element name="person"> <xs:complextype> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complextype> 39

Task <contacts> <tel>123-45-67</tel> <address>lenina, 76</address> <email>123@mail.ru</email> </contacts> Require only one element <info> <surname>ivanova</surname> <name>elena</name> </info> Require all elements 40

Occurrence Indicators maxoccurs Indicator The <maxoccurs> indicator specifies the maximum number of times an element can occur: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxoccurs="10"/> </xs:sequence> </xs:complextype> The example indicates that the "child_name" element can occur a minimum of one time (the default value for minoccurs is 1) and a maximum of ten times in the "person" element. 41

Occurrence Indicators minoccurs Indicator The <minoccurs> indicator specifies the minimum number of times an element can occur: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxoccurs="10" minoccurs="0"/> </xs:sequence> </xs:complextype> The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element. 42

Occurrence Indicators To allow an element to appear an unlimited number of times, use the maxoccurs="unbounded" statement: <xs:element name="person"> <xs:complextype> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxoccurs="10" maxoccurs="unbounded" /> </xs:sequence> </xs:complextype> 43

XSD Empty Elements <product prodid="1345" /> <xs:element name="product"> <xs:complextype> <xs:attribute name="prodid" type="xs:positiveinteger"/> </xs:complextype> 44

XSD Text-Only Elements A complex text-only element can contain text and attributes. This type contains only simple content (text and attributes), therefore we add a simplecontent element around the content. When using simple content, you must define an extension OR a restriction within the simplecontent element, like this: <xs:element name="somename"> <xs:complextype> <xs:simplecontent> <xs:extension base="basetype">...... </xs:extension> </xs:simplecontent> </xs:complextype> <xs:element name="somename"> <xs:complextype> <xs:simplecontent> <xs:restriction base="basetype">...... </xs:restriction> </xs:simplecontent> </xs:complextype> 45

Example Text-Only Elements <shoesize country="france">35</shoesize> <xs:element name="shoesize"> <xs:complextype> <xs:simplecontent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simplecontent> </xs:complextype> 46

XSD Mixed Content A mixed complex type element can contain attributes, elements, and text. <letter> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>. </letter> <xs:element name="letter"> <xs:complextype mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveinteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence> </xs:complextype> To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true. 47

Task <contacts> <tel>123-45-67</tel> <address>lenina, 76</address> <email>123@mail.ru</email> <email>123@mail.ru</email> </contacts> Require all elements <photo url= image.jpg /> 48