Языки описания информации Лекция 3. Кафедра «ОСУ» Весенний семестр 2010/2011 уч. года XML Schema и XLink.

Презентация:



Advertisements
Похожие презентации
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
Advertisements

© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
REFERENCE ELEMENTS 64. If your REFERENCE ELEMENTS toolbar is not in view and not hidden, you can retrieve it from the toolbars menu seen here. 65.
Unit II Constructor Cont… Destructor Default constructor.
Here are multiplication tables written in a code. The tables are not in the correct order. Find the digit, represented by each letter.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Using Dreamweaver MX Slide 1 Window menu Manage Sites… Window menu Manage Sites… 2 2 Open Dreamweaver 1 1 Set up a website folder (1). Click New…
© 2006 Cisco Systems, Inc. All rights reserved. CVOICE v Configuring Voice Networks Configuring Dial Peers.
Ideal Family Were prepared by Iryna Molokova and Ilona Synytsia.
DRAFTING and DIMENSIONING 98. A properly dimensioned drawing of a part is very important to the manufacturing outcome. With CATIA, it can be a very simple.
CONSTRAINTS 52. You do your CONSTRAINING in Sketcher mode to create your part to exacting dimensions. This is the opposite of free-form creating we have.
Lesson 2. How to say hello & goodbye ?. When we first meet someone whether it is a person we know or someone we are meeting for the first time, we will.
1/27 Chapter 9: Template Functions And Template Classes.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Employing AS-Path Filters.
S4-1 PAT328, Section 4, September 2004 Copyright 2004 MSC.Software Corporation SECTION 4 FIELD IMPORT AND EXPORT.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Applying Route-Maps as BGP Filters.
Simple Past vs. Present Perfect When do we use each tense in English?
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Customer-to-Provider Connectivity with BGP Connecting a Multihomed Customer to Multiple Service.
PAT312, Section 21, December 2006 S21-1 Copyright 2007 MSC.Software Corporation SECTION 21 GROUPS.
Транксрипт:

Языки описания информации Лекция 3. Кафедра «ОСУ» Весенний семестр 2010/2011 уч. года XML Schema и XLink

XSD (XML Schema Definition Language) XML Schema: альтернатива использованию DTD

Стандарт XML Schema XML Schema это альтернатива языку DTD, основанная на синтаксисе языка XML. XML 1.0 поддерживает DTD для определения структуры документов. Язык XML Schema именет лучшие возможности поддержки для приложений, структуры документов, атрибутов т типов данных. Будущие версии XML будут основываться на XML Schema для определения типов XML документов. Стандарт XML Schema включает – XML Schema Structures specifies the XML Schema definition language. – XML Schema Data types specifies extensible data types for XML.

Спецификации XML Shema и временной график их принятия SpecificationDraft / ProposalRecommendation XML Schema (XSD) 02. May 2001 XML Schema 1.0: Structures 02. May 2001 XML Schema 1.0: Datatypes 02. May 2001 XML Schema (2.Ed) 28. Oct 2004 XML Schema Structures (2.Ed) 28. Oct 2004 XML Schema Datatypes (2.Ed) 28. Oct 2004 XML Schema Component Designators 17. Nov 2008 XML Schema 1.1: Structures30. Apr 2009 XML Schema 1.1: Datatypes30. Apr 2009

Язык XML Schema позволяет описывать структуру XML документа. Применр XML Schema:

Введение в XML Schema Язык XML Schema предназначен для описания структуры XML документов. – Язык XML Schema также называется XML Schema Definition (XSD). Целью создания языка XML Schema является описание правильного синтаксиса конкретного языка разметки (также, как и язык DTD). XML Schema определяет: – элементы, которые могут появляться в документах; – атрибуты, которые могут использоваться с элементами; – как элементы могут вкладываться друг в друга; – порядок следования дочерних элементов; – может ли элемент быть пустым или может ли он включать текст; – типы данных для элементов и атрибутов; – значения по умолчанию и фиксированные значения для элементов и атрибутов.

XML Schema является правопреемником (заменителем) языка DTD. Считается, что скоро XML Schema будет использоваться в большинстве Web приложений в качестве замены для DTDs. Причинами этого является: – XML Schema является расширяемым для будущих дополнений; – XML Schema более выразительный и более мощный, чем DTD; – XML Schema использует семантику метаязыка XML; – XML Schema поддерживает типы данных; – XML Schema поддерживает пространства имен (namespaces).

Почему нужно использовать XML Schema Язык XML Schemas является более мощным по сравнению с языком DTD. – XML Schemas Support Data Types. – XML Schemas use XML Syntax. – XML Schemas Secure Data Communication. – XML Schemas are Extensible. – Well-Formed is not Enough.

XML Schemas поддерживает типы данных One of the greatest strength of XML Schemas is the support for data types. With support for data types: – It is easier to describe allowable document content – It is easier to validate the correctness of data – It is easier to work with data from a database – It is easier to define data facets (restrictions on data) – It is easier to define data patterns (data formats) – It is easier to convert data between different data types

Рассмотрим применр простого XML- документа (note.xml) Луценко Фролов Напоминание Не забудь о собрании лаборатории в субботу!

Описание схемы на языке XML Schema На языке XML Schema язык разметки использованный для составления XML- документа note.xml может быть описан следующим образом (файл note.xsd):

Связывание XML-документ note.xml с описанием его синтаксиса на языке XML Schema можно выполнить следующим образом: < сообщение xmlns=" xmlns:xsl=" xsl:schemaLocation=" note.xsd"> Луценко Фролов Напоминание Не забудь о собрании лаборатории в субботу!

Здесь все используемые элементы, с помощью первого атрибута объявлены в пространстве именн " Во втором атрибуте (xmlns:xsl=" создается префикс для пространства именн «XML Schema Instance», которое используется для связывания с экземплярами документов описания схем на языке XSD. Из данного пространства именн можно использовать атрибут schemaLocation, которому задается два значения, разделенные пробелом. – первое значение ( – это используемое пространство именн, – второе значение (note.xsd) – местоположение документа с описанием XML схемы для данного пространства именн. Наприменр: xsl:schemaLocation=" note.xsd"

Синтаксис языка XSD Корневым элементом описаний на языке XML Schema является элемент :... Элемент может включать некоторые атрибуты. Наприменр:

Первый атрибут xmlns:xs=" указывает, что в данном документе используется пространство именн " Второй атрибут targetNamespace= " указывает на то, что для описываемых в документе новых элементов (сообщение, для_кого, от_кого, заголовок, содержание) задается пространство именн " Третий атрибут xmlns=" w3schools.com" указывает, что пространством именн по умолчанию является " Четвертый атрибут elementFormDefault= "qualified" указывает, что для всех элементов данного XML-документа должно быть указано пространство именн. Описания всех элементов и атрибутов конкретного языка могут использовать атрибут type.

Встроенные типы языка XML Schema В языке XML Schema определен набор из 19 встроенных (предопределенных) типов данных, которые могут использоваться при описании элементов и атрибутов.

Применры встроенных типов xs:string – произвольные строки текста; xs:byte, xs:float, xs:long – числовые форматы: – xs:byte – любое 8-разрядное число со знаком; – xs:long – любое 32-разрядное число со знаком; – xs:float – число с плавающей точкой, наприменр, 15,6 или 5.032E-6. xs:time, xs:date, xs:timeinstant, xs:timeduration – шаблоны для обозначения времени, даты и длительности;

xs:boolean – значение true или false (допускаются также числовые эквиваленты: 0 или 1); xs:binary – шаблон для двоичных чисел, наприменр, ; xs:language – код языка, наприменр, en-US или RUS ; xs:uri-reference – шаблон для любого URI, наприменр, ; xs:ID, xs:IDREF, xs:IDREFS, xs:NMTOKEN, xs:NMTOKENS – типы атрибутов, действующие так же, как соответствующие типы в DTD.

Использование такого большого количества встроенных типов делает язык XSD очень полезным для документов, которые связаны со специальными приложениями для обработки данных, наприменр, базами данных и бланками ввода заказов. При его использовании не требуется писать программу, проверяющую типы данных, т.к. эту работу выполняет сам XML-процессор.

Описание простых элементов В состав описываемого конкретного языка могут входить простые элементы и составные элементы. – Простые элементы могут включать только текстовое содержание и не могут включать другие элементы или атрибуты. – Составные элементы могут содержать текст, атрибуты и дочерние элементы. Для описания простого элемента используется следующий синтаксис:, где "имя" это имя элемента, а "тип" – тип данных, которые описываемый элемент может именть.

Применры простых XML элементов XML-документ: Refsnes Эти элементы могут быть следующим образом описаны с помощью языка XSD:

Значения по умолчанию и фиксированные значения Простые элементы могут именть значения по умолчанию или фиксированные значения. Значение по умолчанию автоматически назначается элементу, когда никакого значения ему не задано. Наприменр: Если для простого элемента задано фиксированное значение, то данному элементу не могут быть заданы другие значения. Наприменр:

Описание атрибутов Простые элементы не могут именть атрибуты. Если элемент именет атрибуты, то он считается составным типом. Однако, сами атрибуты описываются, как простые типы, следующим образом:, – "имя" – это имя атрибуты, – "тип" – тип данных, который может быть задан данному атрибуту. Наприменр: Атрибуты, также, как и простые типы могут именть значения по умолчанию или фиксированные значения. Наприменр:

По умолчанию атрибуты считаются не обязательными. Для задания обязательности описываемых атрибутов используется атрибут "use". Наприменр:

Ограничения на содержание элементов Если простому элементу или атрибуту задан тип, то он налагает ограничения на их содержание. Наприменр, если элемент типа xs:date будет содержать строку типа "Hello World", то он не будет считаться верным. Кроме использования типов на значения простых элементов и атрибутов можно также накладывать дополнительные ограничения, которые еще называются «гранями» (facets). Ограничения можно задать с помощью элементов xs:restriction, в который вкладываются разные типы ограничений,

Типы ограничений на элементы Ограничение Описание enumeration Определение допустимых значений. fractionDigits Задание максимальное количество знаков после запятой (равно или больше чем 0). length Задание точного количества разрешенных символов или значений (равно или больше 0). maxExclusive Задание верхней границы для числового значения (значение должно быть меньше чем заданная граница). maxInclusive Задание верхней границы для числового значения (значение должно быть меньше или рано заданной границе). maxLength Задание максимального количества символов или элементов списка. (равно или больше чем 0).

Типы ограничений на элементы (2) Ограничение Описание minExclusive Задание нижней границы для числового значения (значение должно быть больше чем заданная граница). minInclusive Задание нижней границы для числового значения (значение должно быть больше или равно заданной границе). minLength Задание минимального количества символов или элементов списка. (равно или больше чем 0). pattern Описание точной последовательности символов, которые считаются верными. totalDigits Задание точного количества цифр (больше чем 0). whiteSpace Описание того, как обрабатываются пробельные символы в тексте (white space) (перевод строки, табуляция, пробел и возврат каретки).

Применр ограничения Для элемента age (возраст) возможный интервал значений от 0 до 120 можно задать следующим образом:

Применр ограничения (2) C помощью элемента xs:enumeration можно ограничить содержание элемента списком возможных значений. Наприменр, с помощью элемента xs:enumeration можно установит условие, что элементу car могут задаваться только значения из списка (Audi, Golf, BMW):

Применр с типами автомобилей можно описать и по- другому: – вначале создать новый тип carType, – затем использовать его для описания элемента car: Новый тип carType может использоваться и для описания и других элементов, т.к. не является частью элемента car.

Ограничения содержания текстового элемента Для ограничения содержания элемента, именющего тип xs:string, можно использовать шаблоны, которые задаются с помощью элемента xs:pattern и атрибута value. Шаблоны используют язык регулярных выражений и задают наборы правил формирования последовательности допустимых символов. Они могут задаваться в виде перечня разрешенных строк разделенных знаком "|". – Наприменр: "male | female".

В каждой строке могут быть заданы конкретные символы или не определенные символы, описываемые в квадратных скобках […], в которых перечисляются возможные их значения, – наприменр, [xyz] – символ может получать значение x или y или z. Вместо перечисления значений символа можно задавать интервалы возможных значений: a-z (только маленькие буквы); A-Z (только большие буквы); 0-9 (только цифры). Интервалы можно комбинировать, наприменр, a- zA-Z – возможна маленькая или большая буква.

Кроме задания символов в квадратных скобках можно использовать следующие указатели: – \d – одна цифра; – \s – пробельный символ; – \w – буква или цифра. С помощью круглых скобок возможные символы можно объединять в группы, после которой можно указать сколько раз данная группа может повториться. Для этого после закрывающейся скобки группы можно указать символы: – * – ноль или более; – + – 1 или более; – {n} – точно n раз.

Применр 1 разрешенными значениями являются одна или несколько пар символов, каждая пара состоит из маленькой и большой буквы – верно: "sToP"; – неверно: "Stop" или "STOP".

Применр 2 должно задаваться точно 8 символов, каждый из которых может быть – маленькой (a-z) или большой буквой (A-Z), или – цифрой от 0 до 9 (0-9).

Применр 3 может задаваться – любой символ алфавита ([A-Z]), – за которым следует дефис (-) и – три цифры (\d{3}).

Описание составных элементов Составные элементы могут содержать текст, другие элементы и/или атрибуты. Имеются 4 вида составных элементов: – пустые элементы пустые элементы, которые не могут именть содержание, но могут именть атрибуты; – элементы, содержащие другие элементы; – элементы, содержащие только текст; – элементы, которые содержат другие элементы и текст (смешанное содержание). Каждый из этих видов элементов также может содержать и атрибуты.

Применры составных элементов Применр пустого составного элемента "product": Применр составного элемента "employee", содержащего только дочерние элементы : John Smith

Описание пустых элементов Пустые элементы не могут именть содержания, но могут именть атрибуты. Их нужно описывать, как составные элементы, но не включать в них никакие дочерние элементы, а определять только их атрибуты. Наприменр: элемент, использующий атрибут pid, которому могут задаваться только целые положительные значения (наприменр, ), может описываться на языке XML Schema следующими способами:

Создание нового составного типа и указание его в описании элемента product: Непосредственное описание элемента без содержания и с одним атрибутом с помощью элементов xs:complexType и xs:restriction:

Описание составных элементов, содержащих только дочерние элементы Составные элементы, содержащие только дочерние элементы, как наприменр элемент person: John Smith могут определяться на языке XSD следующим образом: Элемент xs:sequence указывает, что дочерние элементы (firstname и lastname) должны в XML-документе появляться в заданном порядке.

Также можно задать создать новый тип составного элемента persontype и использовать его при описании элемента person:

Описание элементов, именющих простое содержание При описании элемента, включающего только простое содержимое (текст и атрибуты), нужно использовать элемент simpleContent. В элементе simpleContent можно описать расширение (xs:extension) или ограничение (xs:restriction):...

Описание элемента simpleContent с использованием ограничение (xs:restriction):...

Применр описания простого элемента Наприменр, нужно описать элемент "shoesize" (размер обуви), которые записываются следующим образом: 35 Данный элемент может быть описан, как составной элемент, содержащий только целые значения и использующий атрибут с именнем country: Также можно вначале создать новый составной тип shoetype, а затем использовать его при описании элемента shoesize.

Описание составных типов со смешанным содержанием Составные типы со смешанным содержанием могут включать атрибуты, дочерние элементы и текст. Наприменр, рассмотрим элемент letter, который может содержать, как текст, так и другие элементы: Уважаемый господин Луценко Александр. Ваш заказ 1032 Был отправлен

Данный элемент "letter" можно описать следующим образом: Отметим, чтобы разрешить записывать символьные данные между дочерними элементами, требуется задать атрибуту mixed значение true.

Индикаторы Для задания правил использования в XML- документе дочерних элементов применняются индикаторы. Имеется три вида индикаторов: – Индикаторы порядка – используются для определения порядка элементов (all, choice, sequence). – Индикаторы кратности – используется для определения того, сколько раз могут записываться элементы: (maxOccurs, minOccurs). – Индикаторы группирования – используются для задания взаимосвязанных наборов элементов (Group, attributeGroup).

Индикатор Индикатор указывает, что дочерние элементы могут появляться в любом порядке и каждый элемент должен быть появится только один раз. Наприменр: Отметим, что при использовании индикатора можно – индикатору задавать значение 0 или 1, – индикатору может задаваться только значение 1.

Индикатор Индикатор указывает, что в составном элементе должен быть задан один из перечисленных дочерних элементов. Наприменр:

Индикатор Индикатор указывает, что дочерние элементы должны записываться в том порядке, как они описаны. Наприменр:

Индикаторы и Индикатор указывает на максимальное количество, а индикатор указывает на минимальное количество дочерних элементов. Наприменр, для задания ограничения детей от 0 до 10 можно записать

По умолчанию для всех элементов задаются индикаторы maxOccurs=1 и minOccurs=1. Для того, что не было ограничения на максимальное количество дочернего элемента нужно задать: maxOccurs="unbounded".

С помощью индикаторов группирования элементов можно описывать группы элементов, задавать им именна и использовать их в других описаниях. При создании группы элементов нужно указать индикатор порядка элементов внутри данной группы (all, choice или sequence). Наприменр: описание группы элементов с именнем persongroup, которые должны появляться в заданном порядке:

После определения группы с именнем persongroup, на нее можно ссылаться в других описаниях с помощью атрибута ref. Наприменр, при описании составного элемента:

С помощью индикаторов группирования атрибутов можно описывать группы атрибутов, задавать им именна и использовать их в других описаниях. Наприменр: описание группы атрибутов с именнем personattrgroup: После создания данной группы атрибутов на нее можно ссылаться в других описаниях. Наприменр:

Элемент Элемент позволяет включать в XML-документ элементы, которые не указаны в описании схемы конкретного языка разметки. Применр, показанный ниже, является фрагментом из схемы конкретного языка разметки, хранящегося в файле family.xsd. В нем содержится описание элемента person. С помощью элемента выполняется расширение содержания типа элементов person. В этом случае в элементе person после элемента lastname могут записываться любые не описанные в схеме элементы:

Теперь в элементе person включать, наприменр, элемент children, хотя в самой схеме возможность использования данного элемента не была описана. Рассмотрим файл, называемый "children.xsd", который содержит описание схему (описание синтаксиса конкретного языка разметки):

Ниже показан XML-документ (называемый "Myfamily.xml"), использующий элементы из двух разных схем "family.xsd" и "children.xsd": Hege Refsnes Cecilie Stale Refsnes

Данный XML-документ является действительным (valid), так как схема "family.xsd" позволяет расширять элемент "person" необязательным элементом после элемента "lastname". Элементы и используются для того, чтобы сделать конкретный язык разметки расширяемым! Они позволяют документу содержать дополнительные элементы, которые не объявлены в основной XML схеме.

Спасибо за внимание!

XML Schemas использует XML- синтаксис Another great strength about XML Schema is that they are written in XML. Some benefits of that XML Schemas are written in XML: – You don't have to learn a new language – You can use your XML editor to edit your Schema files – You can use your XML parser to parse your Schema files – You can manipulate your Schema with the XML DOM – You can transform your Schema with XSLT

XML Schemas Secure Data Communication When sending data from a sender to a receiver, it is essential that both parts have the same "expectations" about the content. With XML Schemas, the sender can describe the data in a way that the receiver will understand. – A date like: " " will, in some countries, be interpreted as 3. November and in other countries as 11.March. – However, an XML element with a data type like this: ensures a mutual understanding of the content, because the XML data type "date" requires the format "YYYY-MM- DD".

XML Schemas являются расширяемым XML Schemas are extensible, because they are written in XML. With an extensible Schema definition you can: – Reuse your Schema in other Schemas – Create your own data types derived from the standard types – Reference multiple schemas in the same document

Well-Formed is not Enough A well-formed XML document is a document that conforms to the XML syntax rules, like: – it must begin with the XML declaration – it must have one unique root element – start-tags must have matching end-tags – elements are case sensitive – all elements must be closed – all elements must be properly nested – all attribute values must be quoted – entities must be used for special characters Even if documents are well-formed they can still contain errors, and those errors can have serious consequences. Think of the following situation: – you order 5 gross of laser printers, instead of 5 laser printers (???). – With XML Schemas, most of these errors can be caught by your validating software.

XML документы могут ссылаться на DTD или XML Schema Look at this simple XML document called "note.xml": Tove Jani Reminder Don't forget me this weekend! DTD файл. The following example is a DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"): The first line defines the note element to have four child elements: "to, from, heading, body". Line 2-5 defines the to, from, heading, body elements to be of type "#PCDATA".

Описание на языке XML Schema The following example is an XML Schema file called "note.xsd" that defines the elements of the XML document above ("note.xml"): The note element is a complex type because it contains other elements. The other elements (to, from, heading, body) are simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.

Ссылки на схему Ссылка на DTD. This XML document has a reference to a DTD: Tove Jani Reminder Don't forget me this weekend! Ссылка на XML Schema. This XML document has a reference to an XML Schema: Tove Jani Reminder Don't forget me this weekend!

Элемент XSD - The element is the root element of every XML Schema The element may contain some attributes. A schema declaration often looks something like this: …

The following fragment: xmlns:xs= indicates that the elements and data types used in the schema come from the " namespace. It also specifies that the elements and data types that come from the " namespace should be prefixed with xs: This fragment: targetNamespace=" that the elements defined by this schema (note, to, from, heading, body.) come from the " namespace. This fragment: xmlns=" that the default namespace is " This fragment: elementFormDefault="qualified"indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.

Ссылка на схему в XML документе This XML document has a reference to an XML Schema:

Простые (simple) XSD элементы Простыми XML элементами называются элементы, которые могут содержать только текст. Они не могут включать другие элементы или атрибуты. Текст может представлять собой данные разных типов: – типов включенных в определение XML Schema (boolean, string, date, etc.), – пользовательский тип, который он описывает сам. You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern. The syntax for defining a simple element is:, где – xxx - name of the element and – yyy - data type of the element. 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

Применр простых элементов Here are some XML elements: Refsnes And here are the corresponding simple element definitions:

Значение по умолчанию и фиксированное значение для простых типов 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. In the following example the default value is "red": A fixed value is also automatically assigned to the element, and you cannot specify another value. In the following example the fixed value is "red":

Атрибуты XSD All attributes are declared as simple types. 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. The syntax for defining an attribute is:, где – xxx is the name of the attribute – yyy specifies the data type of the attribute. 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 Применр XML element with an attribute: Smith And here is the corresponding attribute definition:

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. Наприменр, default value is "EN": A fixed value is also automatically assigned to the attribute, and you cannot specify another value. Наприменр, fixed value is "EN": Optional and Required Attributes Attributes are optional by default. To specify that the attribute is required, use the "use" attribute: 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. If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate. With XML Schemas, you can also add your own restrictions to your XML elements and attributes. Такие ограничения называются гранями (facets).

XSD ограничения/грани Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets. 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:

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. The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW: The example above could also have been written like this: Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.

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

Restrictions on a Series of Values (2) 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: 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:

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

The next example defines an element called "gender" with a restriction. The only acceptable value is male OR female: 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:

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:

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

This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "collapse", which means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space):

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: This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters:

Restrictions for Datatypes ConstraintDescription enumerationDefines a list of acceptable values fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zero lengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zero maxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value) maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value) maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zero minExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value) minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value) minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zero patternDefines the exact sequence of characters that are acceptable totalDigitsSpecifies the exact number of digits allowed. Must be greater than zero whiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

XSD Complex Elements A complex element is an XML element that contains other elements and/or attributes. There are four kinds of complex elements: – empty elements – elements that contain only other elements – elements that contain only text – elements that contain both other elements and text Note: Each of these elements may contain attributes as well! Examples of Complex Elements A complex XML element, "product", which is empty: A complex XML element, "employee", which contains only other elements: John Smith A complex XML element, "food", which contains only text: Ice cream A complex XML element, "description", which contains both elements and text: It happened on

Описание сложных элементов Look at this complex XML element, "employee", which contains only other elements: John Smith We can define a complex element in an XML Schema two different ways: 1. The "employee" element can be declared directly by naming the element, like this: If you use the method described above, only the "employee" element can use the specified complex type. Note that the child elements, "firstname" and "lastname", are surrounded by the indicator. This means that the child elements must appear in the same order as they are declared. You will learn more about indicators in the XSD Indicators chapter. 2. The "employee" element can have a type attribute that refers to the name of the complex type to use:

If you use the method described above, several elements can refer to the same complex type, like this: You can also base a complex element on an existing complex element and add some elements, like this:

Пустые элементы An empty complex element cannot have contents, only attributes. Complex Empty Elements An empty XML element: The "product" element above has no content at all. To define a type with no content, we must define a type that allows elements in its content, but we do not actually declare any elements, like this: In the example above, we define a complex type with a complex content. The complexContent element signals that we intend to restrict or extend the content model of a complex type, and the restriction of integer declares one attribute but does not introduce any element content.

However, it is possible to declare the "product" element more compactly, like this: Or you can give the complexType element a name, and let the "product" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):

Составные элементы, содержащие только подэлементы ("elements-only") An "elements-only" complex type contains an element that contains only other elements. An XML element, "person", that contains only other elements: John Smith В схеме элемент "person" может быть описан следующим образом: Notice the tag. It means that the elements defined ("firstname" and "lastname") must appear in that order inside a "person" element.

Другой способ описания Or you can give the complexType element a name, and let the "person" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):

XSD Text-Only Elements A complex text-only element can contain text and attributes. Complex Text-Only Elements. 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: или Tip: Use the extension/restriction element to expand or to limit the base simple type for the element.

Here is an example of an XML element, "shoesize", that contains text-only: 35 The following example declares a complexType, "shoesize". The content is defined as an integer value, and the "shoesize" element also contains an attribute named "country": We could also give the complexType element a name, and let the "shoesize" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):

Элементы со смешанным содержанием A mixed complex type element can contain attributes, elements, and text. Complex Types with Mixed Content An XML element, "letter", that contains both text and other elements: Dear Mr. John Smith. Your order 1032 will be shipped on The following schema declares the "letter" element: Note: To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true". The tag means that the elements defined (name, orderid and shipdate) must appear in that order inside a "letter" element.

We could also give the complexType element a name, and let the "letter" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):

Индикаторы XSD We can control HOW elements are to be used in documents with indicators. Indicators There are seven indicators: Order indicators: – All – Choice – Sequence Occurrence indicators: – maxOccurs – minOccurs Group indicators: – Group name – attributeGroup name

Order Indicators Order indicators are used to define the order of the elements. All Indicator The indicator specifies that the child elements can appear in any order, and that each child element must occur only once: Note: When using the indicator you can set the indicator to 0 or 1 and the indicator can only be set to 1 (the and are described later).

Choice Indicator The indicator specifies that either one child element or another can occur: Sequence Indicator The indicator specifies that the child elements must appear in a specific order:

Occurrence Indicators Occurrence indicators are used to define how often an element can occur. Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1. maxOccurs Indicator The indicator specifies the maximum number of times an element can occur: The example above 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. minOccurs Indicator The indicator specifies the minimum number of times an element can occur: 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. Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:

Применр XML-документа Myfamily.xml Hege Refsnes Cecilie Tove Refsnes Hege Stale Jim Borge Stale Refsnes

Применр описания схемы family.xsd The XML file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements.

Индикаторы группы Group indicators are used to define related sets of elements. Element Groups Element groups are defined with the group declaration, like this:... You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:

After you have defined a group, you can reference it in another definition, like this:

Attribute Groups Attribute groups are defined with the attributeGroup declaration, like this:... The following example defines an attribute group named "personattrgroup": After you have defined an attribute group, you can reference it in another definition, like this:

XSD The Element The element enables us to extend the XML document with elements not specified by the schema! The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the element we can extend (after ) the content of "person" with any element: Now we want to extend the "person" element with a "children" element. In this case we can do so, even if the author of the schema above never declared any "children" element. Look at this schema file, called "children.xsd":

The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "children.xsd": Hege Refsnes Cecilie Stale Refsnes The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element. The and elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.

XSD The Element The element enables us to extend the XML document with attributes not specified by the schema! The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the element we can add any number of attributes to the "person" element: Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the author of the schema above never declared any "gender" attribute. Look at this schema file, called "attribute.xsd":

XSD Element Substitution With XML Schemas, one element can substitute another element. Let's say that we have users from two different countries: England and Norway. We would like the ability to let the user choose whether he or she would like to use the Norwegian element names or the English element names in the XML document. To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element. In the example above, the "name" element is the head element and the "navn" element is substitutable for "name". Look at this fragment of an XML schema:

A valid XML document (according to the schema above) could look like this: John Smith or like this: John Smith

Blocking Element Substitution To prevent other elements from substituting with a specified element, use the block attribute: Look at this fragment of an XML schema: A valid XML document (according to the schema above) looks like this: John Smith BUT THIS IS NO LONGER VALID: John Smith

Using substitutionGroup The type of the substitutable elements must be the same as, or derived from, the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element. Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work! What are Global Elements? Global elements are elements that are immediate children of the "schema" element! Local elements are elements nested within other elements.

XML document Let's have a look at this XML document called "shiporder.xml": John Smith Ola Nordmann Langgt Stavanger Norway Empire Burlesque Special Edition Hide your heart The XML document above consists of a root element, "shiporder", that contains a required attribute called "orderid". The "shiporder" element contains three different child elements: "orderperson", "shipto" and "item". The "item" element appears twice, and it contains a "title", an optional "note" element, a "quantity", and a "price" element. The line above: xmlns:xsi=" tells the XML parser that this document should be validated against a schema. The line: xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder.xml").

Create an XML Schema Now we want to create a schema for the XML document above. We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow the structure in the XML document and define each element as we find it. We will start with the standard XML declaration followed by the xs:schema element that defines a schema:... In the schema above we use the standard namespace (xs), and the URI associated with this namespace is the Schema language definition, which has the standard value of

Create an XML Schema Next, we have to define the "shiporder" element. This element has an attribute and it contains other elements, therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a xs:sequence element that defines an ordered sequence of sub elements:...

Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type (xs:string) is prefixed with the namespace prefix associated with XML Schema that indicates a predefined schema data type: Next, we have to define two elements that are of the complex type: "shipto" and "item".

We start by defining the "shipto" element: With schemas we can define the number of possible occurrences for an element with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and minOccurs specifies the minimum number of occurrences for an element. The default value for both maxOccurs and minOccurs is 1!

Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element. This is specified by setting the maxOccurs attribute of the "item" element to "unbounded" which means that there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element is optional.

We have specified this by setting the minOccurs attribute to zero: We can now declare the attribute of the "shiporder" element. Since this is a required attribute we specify use="required". Note: The attribute declarations must always come last:

Полное описание схемы (shiporder.xsd)

Разделение схемы The previous design method is very simple, but can be difficult to read and maintain when documents are complex. The next design method is based on defining all elements and attributes first, and then referring to them using the ref attribute. Here is the new design of the schema file ("shiporder.xsd"):

Использование именнованных типов (Named Types) The third design method defines classes or types, that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of the element. Here is the third design of the schema file ("shiporder.xsd"):

The restriction element indicates that the datatype is derived from a W3C XML Schema namespace datatype. So, the following fragment means that the value of the element or attribute must be a string value: The restriction element is more often used to apply restrictions to elements. Look at the following lines from the schema above: This indicates that the value of the element or attribute must be a string, it must be exactly six characters in a row, and those characters must be a number from 0 to 9.

Типы данных XML Schema

XSD String Data Types String data types are used for values that contains character strings. The string data type can contain characters, line feeds, carriage returns, and tab characters. The following is an example of a string declaration in a schema: An element in your document might look like this: John Smith Or it might look like this: John Smith Note: The XML processor will not modify the value if you use the string data type.

NormalizedString Data Type The normalizedString data type is derived from the String data type. The normalizedString data type also contains characters, but the XML processor will remove line feeds, carriage returns, and tab characters. The following is an example of a normalizedString declaration in a schema: An element in your document might look like this: John Smith Or it might look like this: John Smith Note: In the example above the XML processor will replace the tabs with spaces.

Token Data Type The token data type is also derived from the String data type. The token data type also contains characters, but the XML processor will remove line feeds, carriage returns, tabs, leading and trailing spaces, and multiple spaces. The following is an example of a token declaration in a schema: An element in your document might look like this: John Smith Or it might look like this: John Smith Note: In the example above the XML processor will remove the tabs.

String Data Types Note that all of the data types below derive from the String data type (except for string itself)! NameDescription ENTITIES ENTITY IDA string that represents the ID attribute in XML (only used with schema attributes) IDREFA string that represents the IDREF attribute in XML (only used with schema attributes) IDREFS languageA string that contains a valid language id NameA string that contains a valid XML name NCName NMTOKENA string that represents the NMTOKEN attribute in XML (only used with schema attributes) NMTOKENS normalizedStringA string that does not contain line feeds, carriage returns, or tabs QName stringA string tokenA string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces

Restrictions on String Data Types Restrictions that can be used with String data types: – enumeration – length – maxLength – minLength – pattern (NMTOKENS, IDREFS, and ENTITIES cannot use this constraint) – whiteSpace

XSD Date and Time Data Types Date and time data types are used for values that contain date and time. The date data type is used to specify a date. The date is specified in the following form "YYYY-MM-DD" where: – YYYY indicates the year – MM indicates the month – DD indicates the day Note: All components are required! The following is an example of a date declaration in a schema: An element in your document might look like this: Time Zones To specify a time zone, you can either enter a date in UTC time by adding a "Z" behind the date - like this: Z or you can specify an offset from the UTC time by adding a positive or negative time behind the date - like this: :00 Or :00

Time Data Type The time data type is used to specify a time. The time is specified in the following form "hh:mm:ss" where: – hh indicates the hour – mm indicates the minute – ss indicates the second Note: All components are required! The following is an example of a time declaration in a schema: An element in your document might look like this: 09:00:00 Or it might look like this: 09:30:10.5

Time Zones To specify a time zone, you can either enter a time in UTC time by adding a "Z" behind the time - like this: 09:30:10Z or you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this: 09:30:10-06:00 or 09:30:10+06:00

DateTime Data Type The dateTime data type is used to specify a date and a time. The dateTime is specified in the following form "YYYY-MM- DDThh:mm:ss" where: – YYYY indicates the year – MM indicates the month – DD indicates the day – T indicates the start of the required time section – hh indicates the hour – mm indicates the minute – ss indicates the second Note: All components are required! The following is an example of a dateTime declaration in a schema: An element in your document might look like this: T09:00:00 Or it might look like this: T09:30:10.5

Time Zones To specify a time zone, you can either enter a dateTime in UTC time by adding a "Z" behind the time - like this: T09:30:10Z or you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this: T09:30:10-06:00 or T09:30:10+06:00

Duration Data Type The duration data type is used to specify a time interval. The time interval is specified in the following form "PnYnMnDTnHnMnS" where: – P indicates the period (required) – nY indicates the number of years – nM indicates the number of months – nD indicates the number of days – T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds) – nH indicates the number of hours – nM indicates the number of minutes – nS indicates the number of seconds The following is an example of a duration declaration in a schema: An element in your document might look like this (period of five years): P5Y Or it might look like this (period of five years, two months, and 10 days): P5Y2M10D Or it might look like this (period of five years, two months, 10 days, and 15 hours): P5Y2M10DT15H Or it might look like this (period of 15 hours): PT15H

Negative Duration To specify a negative duration, enter a minus sign before the P: -P10D The example above indicates a period of minus 10 days.

Date and Time Data Types … NameDescription dateDefines a date value dateTimeDefines a date and time value durationDefines a time interval gDayDefines a part of a date - the day (DD) gMonthDefines a part of a date - the month (MM) gMonthDayDefines a part of a date - the month and day (MM- DD) gYearDefines a part of a date - the year (YYYY) gYearMonthDefines a part of a date - the year and month (YYYY- MM) timeDefines a time value

Restrictions on Date Data Types Restrictions that can be used with Date data types: – enumeration – maxExclusive – maxInclusive – minExclusive – minInclusive – pattern – whiteSpace

XSD Numeric Data Types - Decimal Data Type Decimal data types are used for numeric values. The decimal data type is used to specify a numeric value. The following is an example of a decimal declaration in a schema: An element in your document might look like this: Or it might look like this: Or it might look like this: Or it might look like this: 0 Or it might look like this: 14 Note: The maximum number of decimal digits you can specify is 18.

Integer Data Type The integer data type is used to specify a numeric value without a fractional component. The following is an example of an integer declaration in a schema: An element in your document might look like this: 999 Or it might look like this: +999 Or it might look like this: -999 Или it might look like this: 0

Numeric Data Types Note that all of the data types below derive from the Decimal data type (except for decimal itself)! NameDescription byteA signed 8-bit integer decimalA decimal value intA signed 32-bit integer integerAn integer value longA signed 64-bit integer negativeIntegerAn integer containing only negative values (..,-2,-1) nonNegativeIntegerAn integer containing only non-negative values (0,1,2,..) nonPositiveIntegerAn integer containing only non-positive values (..,-2,-1,0) positiveIntegerAn integer containing only positive values (1,2,..) shortA signed 16-bit integer unsignedLongAn unsigned 64-bit integer unsignedIntAn unsigned 32-bit integer unsignedShortAn unsigned 16-bit integer unsignedByteAn unsigned 8-bit integer

Restrictions on Numeric Data Types Restrictions that can be used with Numeric data types: – enumeration – fractionDigits – maxExclusive – maxInclusive – minExclusive – minInclusive – pattern – totalDigits – whiteSpace

XSD Miscellaneous Data Types Other miscellaneous data types are boolean, base64Binary, hexBinary, float, double, anyURI, QName, and NOTATION. Boolean Data Type The boolean data type is used to specify a true or false value. The following is an example of a boolean declaration in a schema: An element in your document might look like this: 999 Note: Legal values for boolean are true, false, 1 (which indicates true), and 0 (which indicates false). Binary Data Types Binary data types are used to express binary-formatted data. We have two binary data types: – base64Binary (Base64-encoded binary data) – hexBinary (hexadecimal-encoded binary data) The following is an example of a hexBinary declaration in a schema:

AnyURI Data Type The anyURI data type is used to specify a URI. The following is an example of an anyURI declaration in a schema: An element in your document might look like this: Note: If a URI has spaces, replace them with %20.

Miscellaneous Data Types … NameDescription anyURI base64Binary boolean double float hexBinary NOTATION QName

Restrictions on Miscellaneous Data Types Restrictions that can be used with the other data types: – enumeration (a Boolean data type cannot use this constraint) – length (a Boolean data type cannot use this constraint) – maxLength (a Boolean data type cannot use this constraint) – minLength (a Boolean data type cannot use this constraint) – pattern – whiteSpace

XSD Elements ElementExplanation allSpecifies that the child elements can appear in any order. Each child element can occur 0 or 1 time annotationSpecifies the top-level element for schema comments anyEnables the author to extend the XML document with elements not specified by the schema anyAttributeEnables the author to extend the XML document with attributes not specified by the schema appInfoSpecifies information to be used by the application (must go inside annotation) attributeDefines an attribute attributeGroupDefines an attribute group to be used in complex type definitions choiceAllows only one of the elements contained in the declaration to be present within the containing element complexContentDefines extensions or restrictions on a complex type that contains mixed content or elements only complexTypeDefines a complex type element documentationDefines text comments in a schema (must go inside annotation)

XSD Elements (2) ElementExplanation elementDefines an element extensionExtends an existing simpleType or complexType element fieldSpecifies an XPath expression that specifies the value used to define an identity constraint groupDefines a group of elements to be used in complex type definitions importAdds multiple schemas with different target namespace to a document includeAdds multiple schemas with the same target namespace to a document keySpecifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document keyrefSpecifies that an attribute or element value correspond to those of the specified key or unique element listDefines a simple type element as a list of values notationDescribes the format of non-XML data within an XML document redefineRedefines simple and complex types, groups, and attribute groups from an external schema

XSD Elements (3) ElementExplanation restrictionDefines restrictions on a simpleType, simpleContent, or a complexContent schemaDefines the root element of a schema selectorSpecifies an XPath expression that selects a set of elements for an identity constraint sequenceSpecifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times simpleContentContains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements simpleTypeDefines a simple type and specifies the constraints and information about the values of attributes or text-only elements unionDefines a simple type as a collection (union) of values from specified simple data types uniqueDefines that an element or an attribute value must be unique within the scope

XSD ограничения/грани для типов данных ConstraintDescription enumerationDefines a list of acceptable values fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zero lengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zero maxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value) maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value) maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zero minExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value) minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value) minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zero patternDefines the exact sequence of characters that are acceptable totalDigitsSpecifies the maximum number of digits allowed. Must be greater than zero whiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled

Синтаксис DTD устарел и недостаточно гибок, из- за чего для некоторых задач DTD недостаточно выразительны. В документах применняется один синтаксис, а в DTD – другой. Модели содержимого и объявления списков атрибутов тяжело читать и понимать, невозможно задать шаблонов данных в элементах и атрибутах. В связи с этим в 2001 году был разработан стандарт XML Schema (XSchema). Вторая версия XSchema был опубликован в 2004 (

В отличие от синтаксиса DTD, синтаксис XML Schema является корректным (well-formed) XML, что позволяет использовать при ее редактировании излюбленные инструменты XML. Она также предоставляет значительно больший контроль над типами данных и шаблонами, образуя более привлекательный язык для соблюдения строгих требований ввода данных. Применр описания схемы бланк для переписи населения. Данная схема должна способствовать поддержке организации данных путем контроля типов данных в случае, если что-то будет введено в неверное поле. Вот как может выглядеть в XML экземпляр документа.

Применр XML документа переписи Yellowbrick Road Munchkinville Negbo Floyd Fleegle 61 Phylis Fleegle 52 Filbert Fleegle 22

Описание схемы документа переписи Census form for the Republic of Oz Department of Paperwork, Emerald City

Объявление элементов и атрибутов В первой строке документ идентифицируется как схема и связывается с пространством именн XML Schema. Для удобства в дальнейшем обсуждении мы будем отбрасывать префикс пространства именн xsd:. Следующая структура,, служит местом для документирования назначения схемы и других деталей. Следующим в нашем применре идет первое объявление типа элемента. Атрибут name присваивает родовой идентификатор, а атрибут type устанавливает тип элемента. Есть два типа элементов: простые и сложные. – Объявление простого (simple) элемента не содержит атрибутов или элементов в содержимом. Поскольку данный конкретный элемент является корневым, он должен именть иной тип – сложный. В данном случае сложному типу в действительности дается имя, CensusType, которое нам позднее пригодится для его описания. Хотя именна не являются обязательными, все же лучше использовать их для вашего же собственного блага.

Сложные и простые типы элементов В следующей части схемы CensusType определяется как элемент. В нем содержатся объявления еще трех элементов и объявление атрибута. Здесь не только объявляются три элемента: (счетчик), (адрес) и (жильцы), но и определяется модель содержимого для CensusType. Поэтому элемент должен содержать все три элемента в указанном порядке и может именть необязательный атрибут date. Это значительно отличается от стиля DTD, в котором модель содержимого состоит из строки внутри объявления элемента, а атрибуты объявляются отдельно в объявлении списка атрибутов. Ограничения модели содержимого – Если последовательность отдельных элементов не предоставляет достаточной информации, в XML Schema есть другие возможности. – Атрибуты minOccurs и maxOccurs устанавливают (минимальное и максимальное) число появлений чеголибо в документе. minOccurs="0" отменяет значение по умолчанию 1 и делает элемент необязательным. maxOccurs="*" снимает верхнюю границу, и элемент может появляться любое число раз.

Ограничение модели содержимого Элементы составных типов могут именть разные модели содержимого. Модели содержимого элементов, которые могут включать дочерние узлы, могут быть одного из трех видов: –, – или –. При определении нового составного типа путем наложения ограничения на другой тип необходимо гарантировать, что модели содержимого обоих типов удовлетворяют ряду условий. Эти условия необходимы для гарантии того, что любое корректное содержимое производного типа является корректным по отношению и к базовому типу.

Типы данных В каждом объявлении элемента и атрибута есть атрибут type, как мы видели в объявлении первого элемента. Некоторые типы являются предопределенными в XML Schema, наприменр, string и decimal. Тип string представляет обычные символьные данные, как тип CDATA в языке DTD. Тип decimal представляет собой число. Ниже мы объявляем элемент как именющий тип positive-integer и ограничиваем сверху его величину значением 200. В DTD это сделать нельзя, в нем нет способа наложить на символьные данные шаблон ограничений, а в XML Schema таких способов несколько.

Предопределенные типы данных В следующем списке перечислены еще некоторые предопределенные типы: – byte, float, long - Числовые форматы. Формат byte является любым 8- разрядным числом со знаком, а long - любым 32-разрядным числом со знаком. Тип float представляет число с плавающей точкой, наприменр, 5.032E-6. Другие числовые величины представляют скорее абстракции, а не числа, наприменр, INF (бесконечность), –INF (отрицательная бесконечность) и NaN (not a number – не число, категория, определенная IEEE для операций с плавающей точкой). – time, date, timeinstant, timeduration - Шаблоны для обозначения времени, даты и длительности. – boolean - Значение true или false. Допускаются также числовые эквиваленты: 0 или 1. – binary - Шаблон для двоичных чисел, наприменр, – language - Код языка, скажем, en-US. – uri-reference - Шаблон для любого URI, наприменр, – ID, IDREF, IDREFS, NMTOKEN, NMTOKENS - Типы атрибутов, действующие так же, как соответствующие типы в DTD.

Грани Грани (facets) представляют собой свойства, используемые для задания типов данных, устанавливая пределы и ограничения на величину данных. Наприменр, элементу, тип данных которого positive-integer, было разрешено максимальное значение 200, называемое гранью max-inclusive. В XSchema есть еще 13 граней, в том числе precision, scale, encoding, pattern, enumeration, max-length (точность, масштаб, кодировка, шаблон, перечисление, максимальная длина) и другие.

Шаблоны Объявление сложного типа Address знакомит нас с другим типом ограничения по шаблону. Он именет атрибут postalcode с типом PCode, который определен посредством объявления. Если среди предопределенных типов нет нужного шаблона, с помощью этого элемента можно создать свой собственный. В рассматриваемой схеме PCode определен строкой шаблона [A-Z]-\d{3}, которую нужно читать следующим образом: «любой символ алфавита, за которым следует дефис и три цифры».

XML документ Abiteboul Buneman Suciu Data on the Web A fine book. Buneman XML in Scotland The best ever!

DTD (Document Type Definition)

XML Schema