Instructor: Prof. Young-guk HaDept. of Computer Science & Engineering
웹 기술 및 응용
XML Schema
2019년 2학기
2웹기술응용설계
Outline
q History
q Comparison with DTD
q Syntax
Ø Definitions and declaration
Ø Simple types
Ø Namespace
Ø Complex types
Ø Content models
Ø Secondary components
3웹기술응용설계
Birth of XML Schema
q XML-Data: Jan 1998
Ø Basic concepts of XML Schema
Ø For W3C internal discussion only
q XDR (XML-Data Reduced): Jul 1998
Ø Refines and subsets XML-Data down to manageable size
q Other Schema Languages
Ø DCD for XML (Document Content Description)
Ø DDML (Document Definition Markup Language)
Ø SOX (Schema for Object-oriented XML)
q XML Schema (W3C Recommendation)
Ø May 2001: XML Schema 1.0
Ø Oct. 2004: XML Schema 1.0 2nd Edition
Ø Apr. 2012: XML Schema 1.1
v XML Schema is also called XML Schema Definition Language (XSD)
v W3C XSD Recommendation- Part 0: Primer- Part 1: Structures- Part 2: Data types
4웹기술응용설계
Outline
q History
q Comparison with DTD
q Syntax
Ø Definitions and declaration
Ø Simple types
Ø Namespace
Ø Complex types
Ø Content models
Ø Secondary components
5웹기술응용설계
DTD vs. XML Schema
6웹기술응용설계
Outline
q History
q Comparison with DTD
q Syntax
Ø Definitions and declaration
o Building blocks
o Element & attribute declaration
Ø Simple types
Ø Namespace
Ø Complex types
Ø Content models
Ø Secondary components
7웹기술응용설계
Building Blocks
q Schema declaration
Ø Root of the document
Ø Namespace declarations
q Declarations
Ø Elements
Ø Attributes
q Type definitions
Ø Simple typesfor elements and attributes
Ø Complex typesfor elements
8웹기술응용설계
q Element declaration
Declarations
<product><name>Bolt-001</name><amount>1000</amount>
</product>
<product><name>Bolt-002</name>
</product>
<product><name>Bolt-003</name><amount>1000</amount><amount>2000</amount><amount>3000</amount>
</product>
Example) XML document with element amount
à DTD
decimal
• type := simpleType | complexType• Cardinality constraints
- minOccurs := 0, 1, 2, …- maxOccurs := 0 ~ unbounded
9웹기술응용설계
q Cardinality constraints
Declarations (cont’d)
XML Schema DTD
minOccurece = 1maxOccurence = 1 e
minOccurece = 0maxOccurence = 1 e?
minOccurece = 1maxOccurence = unbounded e+
minOccurece = 0maxOccurence = unbounded e*
minOccurece = nmaxOccurence = m N/A
10웹기술응용설계
q Attribute declaration
Declarations (cont’d)
à DTD#REQUIRED >
<quantity partNum=“Bolt-001” unit=“ea” value=“1000”/>
<quantity unit=“ea” value=“2000” partNum=“Bolt-002”/>
<quantity unit=“ea” value=“3000”/> à invalid
Example) XML document with attribute partNum
• type := simpleType• use := required |
optional |prohibited
11웹기술응용설계
Outline
q History
q Comparison with DTD
q SyntaxØ Definitions and declaration
Ø Simple types
o Built-in, user-derived types
o List, union types
o Facets
Ø Namespace
Ø Complex types
Ø Content models
Ø Secondary components
12웹기술응용설계
XML Schema Datatype Hierarchy
DTD 호환성
user-defined simple types
user-defined simple types derived by list, union or restriction
13웹기술응용설계
Simple Types
q Built-in typesØ Derived from anySimpleType
o Primitive types
• Types in their own right
• Atomic: not defined in terms of other data-types
o Derived types
• List or restriction of existing primitive types
q User-defined types
Ø Defined by the author of the schema and particular to the schema
o List, union or restriction is allowed for user-defined simple types
o Extension is NOT allowed for simple typesv Note that complex types can be derived by either extension or restriction
14웹기술응용설계
Simple Types: Primitive Types
double
15웹기술응용설계
Simple Types: Derived Types
-128~127
16웹기술응용설계
Simple Types: User-Defined Types
q Derived by restriction
Ø Users can derive a simple type by restriction of another
simple base type
o Extension is NOT allowed
o I.e., expansion of range or addition of attributes is not
allowed
E.g.) Defining myInteger of range 1000~9999
17웹기술응용설계
Facets (1/2)
q lengthØ Determines the length of data type
o For string, it means the number of characters it contains
o For binary data such as base64Binary, it means the number of octets
q minLength
Ø Sets the minimum length of the data type
Ø Less than or equal to maxLength
q maxLengthØ Sets the maximum length of the data type
q patternØ Specifies a regular expression which the element content should match
q enumeration
Ø Contains a set of values that element should choose
18웹기술응용설계
Facets (2/2)
q whiteSpaceØ Controls how the processor should treat whitespace chars
o preserve (default), replace, or collapse
q maxInclusive, maxExclusiveØ Sets an upper bound for values using the data type
q minInclusive, minExclusiveØ Sets a lower bound for values using the data type
v minInclusive ≤ value ≤ maxInclusivev minExclusive < value < maxExclusive
q precision, scaleØ precision determines the maximum number of digits the data
type may have (including comma ‘.’)
Ø scale sets the maximum number of digits appearing in the
fractional part of a decimal based data type
19웹기술응용설계
Facets Examples
/> à 10 chars long, e.g., “123-456-78”
e.g., 1234.5 , 3425.1, 9182.5, …
e.g., one of “10”, “20”, or “part Code”
à e.g., “123-AB”, “327-XY”, “617-ST”, …
scale value=“1”/> precision value=“6”/>
20웹기술응용설계
Simple Types: List types
q List types
Ø Derived from existing primitive types, NOT another list or
complex type
Ø Built-in: NMTOKENS, IDREFS, ENTITIES
Ø Using whitespace as a delimiter
E.g.) User-defined list type
<element name=“listOfInteger” type=“listOfIntegerType”>
XML Schema of listOfIntegerType XML Document
21웹기술응용설계
Simple Types: Union types
q Union types
Ø Enables an element or attribute value to be one or more
instances of one type drawn from the union of multiple
primitive and list types
E.g.) <element name=“zips” type=“zipUnion”>
XML Schema of zipUnion type XML Document
22웹기술응용설계
Outline
q History
q Comparison with DTD
q Syntax
Ø Definitions and declaration
Ø Simple types
Ø Namespace
Ø Complex types
Ø Content models
Ø Secondary components
23웹기술응용설계
Namespace
q XML NamespaceØ W3C Recommendation
o First edition on Jan. 1999
o Currently, v1.0 3rd Ed. (Dec. 2009) and v1.1 2nd Ed. (Aug. 2006)
Ø Resolves name conflict between elements or attributes in XML documents
o Qualified names of elements or attributes are required to be globally uniqueand persistent
o Namespace can be used in XML docs and XSD docs as well
Ø Identified by URI (URL or URN, commonly URL)o However, URI is quite long, so a short prefix is used instead
o In case of using URL, it needs NOT to point to actual documents or locations
Ø Default namespaceo Unprefixed elements or attributes are associated with the default namespace of
themselves or parent element’s (block scope)* URL: Uniform Resource Locator (e.g., http://www.w3.org/2001/XMLSchema)* URN: Uniform Resource Name (e.g., urn:isbn:0451450523)* URI: Uniform Resource Identifier (= URL + URN)
24웹기술응용설계
Namespace (cont’d)
ü 세 개의 <title> element가
서로 다른 의미로 사용되고 있음(또한 같은 scope 내에 동음이의element가 사용될 수도 있음)
ü 또한 타 Markup 언어의 <title> element와도 구별할 필요가 있음(예: XHTML의 <title>)
ü 각각의 <title> element가
parser에 의해 식별되지 않는다면well-formed이지만 valid하지 않은XML 문서로 해석될 가능성이 있음
25웹기술응용설계
q Namespace declaration
q Default namespace declaration
q Target namespace declaration (used in XSD docs only)
Namespace (cont’d)
<element_name xmlns:edi=“http://ecommerce.org/schema”>
Prefix Namespace URI
<element_name xmlns=“http://ecommerce.org/schema”>
E.g.) <schema xmlns=“http://www.w3.org/2001/XMLSchema” ...>
<schema targetNamespace=“http://ecommerce.org/schema”>
* No prefix is defined for default namespace
26웹기술응용설계
Namespace (cont’d)
q 네임스페이스 사용 예
Ø Namespace prefix = edi
Ø Namespace URI = “http://ecommerce.org/schema”
<edi:x xmlns:edi=“http://ecommerce.org/schema”><edi:price edi:units=“Euro”>32.18</edi:/price>
</edi:x>
q 디폴트 네임스페이스를 사용하는 경우
<x xmlns=“http://ecommerce.org/schema”><price unit=“Euro”>32.18</price>
</x>
27웹기술응용설계
Namespace (cont’d)
q Well-known namespaces
Ø xsd=“http://www.w3.org/2001/XMLSchema”
Ø xsi=“http://www.w3.org/2001/XMLSchema-instance”
Ø xhtml=“http://www.w3.org/1999/xhtml”
Ø xsl=“http://www.w3.org/1999/XSL/Transform”
Ø wsdl=“http://www.w3.org/ns/wsdl”
Ø soapenv=“http://www.w3.org/2003/05/soap-envelope”
Ø soaprpc=“http://www.w3.org/2003/05/soap-rpc”
Ø xmldsig=“http://www.w3.org/2000/09/xmldsig”
Ø xmlenc=“http://www.w3.org/2001/04/xmlenc”
Ø …
28웹기술응용설계
Namespace (cont’d)
q 네임스페이스의 적용 범위
Ø XML 문서는 하나 이상의 네임스페이스를 가질 수 있음
Ø 네임스페이스는 네임스페이스가 선언된 element와 그 element의 내
부에 nested된 모든 element 및 attribute들에 적용됨 (block scope)
29웹기술응용설계
Namespace in XML Schema
q Target NamespaceØ Using XML Schema, we can
create new elements and
attributes, and keep them in a
specific namespace
Ø This specific namespace is
target namespace where the
newly created elements and
attributes will reside
Ø During validation, an XML
parser verifies whether the
elements and attributes used
in the document exist in the
declared target namespace/
30웹기술응용설계
<schema xmlns="http://www.w3.org/2001/XMLSchema"xmlns:po="http://www.example.com/PO"targetNamespace="http://www.example.com/PO">
<complexType name="USAddress"><sequence>
<element name="name" type="string"/><element name="street" type="string"/>
</sequence></complexType><complexType name="PurchaseOrderType">
<sequence><element name="shipTo" type="po:USAddress"/><element name="billTo" type="po:USAddress"/>
</sequence></complexType>
...<element name="purchaseOrder" type="po:PurchaseOrderType"/>
</schema>
Namespace in XML Schema (cont’d)
31웹기술응용설계
<purchaseOrder xmlns=“http://www.example.com/PO” ... >
<shipTo><name>Micheal</name><street>Main street</street>
</shipTo>
<billTo><name>Tom</name><street>Fremont street</street>
</billTo>
...
</purchaseOrder>
Namespace in XML Schema (cont’d)
q Valid XML document example
à targetNamespace of the schema document
32웹기술응용설계
q Including schema definitions from another external documents
Namespace in XML Schema (cont’d)
<schema xmlns="http://www.w3.org/2001/XMLSchema"xmlns:po="http://www.example.com/PO"targetNamespace="http://www.example.com/PO"… >
<include schemaLocation=“http://www.example.com/PO2.xsd”><element name="purchaseOrder" type="po:PurchaseOrderType"/>
…</schema>
<schema xmlns="http://www.w3.org/2001/XMLSchema"targetNamespace="http://www.example.com/PO"… >
<complexType name="PurchaseOrderType">…
</complexType></schema>
http://www.example.com/PO1.xsd
http://www.example.com/PO2.xsd
* 반드시 동일한target namespace
33웹기술응용설계
Outline
q History
q Comparison with DTD
q SyntaxØ Definitions and declaration
Ø Simple types
Ø Namespace
Ø Complex types
o Extension, restriction
o Adding attribute
o Global & local element
Ø Content models
Ø Secondary components
34웹기술응용설계
Complex Types
q Features of complex types
Ø Carrying attributes and elements
o I.e., contains other types (simple or complex)
Ø Can be derived by both restriction and extension
o Cf., only restriction allowed for simple types
Ø Various and flexible content models
o Any, empty, element-only, mixed
o Model groups: sequence, all, choice
o Cardinality constraints: minOccurs, maxOccurs
o Combinations of the above
35웹기술응용설계
Complex Types: Complex Type Definition
<schema xmlns="http://www.w3.org/2001/XMLSchema"xmlns:po="http://www.example.com/PO"targetNamespace="http://www.example.com/PO">
<element name="purchaseOrder" type="po:PurchaseOrderType"/>
<complexType name="PurchaseOrderType">
<sequence><element name="shipTo" type="po:USAddress"/><element name="billTo" type="po:USAddress"/><element ref="po:comment"
minOccurs="0“ maxOccurs=“10”/>
</sequence><attribute name="orderDate" type="date"/>
</complexType>
<complexType name="USAddress"><sequence>
<element name="name" type="string"/><element name="street" type="string"/>
</sequence></complexType>… …
Attributes
Elements(simple or
complex types)
Specificcardinality
Model group(can be nested)
36웹기술응용설계
Complex Types: Attribute Declaration
q Only simple types can be used for attribute types
Ø Examples
<attribute name=“orderDate”/>
<attribute name=“orderDate” type=“date”/>
<attribute name=“orderDate” type=“date”
use=“required” fixed=“2003-09-19”/>
<attribute name=“orderDate” type=“date”
use=“optional” default=“2003-09-19”/>
<attribute name=“orderDate” type=“date”use=“prohibited”/>
* prohibited: usually used to restrict the base element
37웹기술응용설계
Complex Types: Extension & Restriction (1)
q Deriving types by extension
Ø Base type ⊆ Derived type
Ø Extension from a complex type
o Adding new elements or attributes to a base type
Ø Extension from a simple type
o Adding new attributes to a base type
o Expanding range, length, …
q Deriving types by restriction
Ø Base type ⊇ Derived type
Ø Restriction from a complex type
o Limited cardinality of element, use of attributes, …
o Prohibiting attributes(e.g., use=“prohibited”)
Ø Restriction from a simple type
o Limiting range, length, …
[ Derivation of Complex Types ]
<complexType>
<complexContent> <simpleContent>
<restriction> <extension> <restriction> <extension>
38웹기술응용설계
Complex Types: Extension & Restriction (2)
[ Extension from a simple typeby adding an attribute ]
[ Extension and restriction from a complex type ]
Additionalelements
à Limited cardinality
39웹기술응용설계
Complex Types: Scope of Elements
q Global elements
Ø Direct children of the
“schema” element
Ø Can be referenced using
attribute “ref”
q Local elements
Ø Child elements of another
element
Ø Local elements with the
same name can be
declared in different
scopes Global Element Local Element
40웹기술응용설계
Outline
q History
q Comparison with DTD
q SyntaxØ Definitions and declaration
Ø Simple types
Ø Namespace
Ø Complex types
Ø Content modelso Any, empty, mixed
o Sequence, choice, all
o minOccurs, maxOccurs
Ø Secondary components
41웹기술응용설계
Basic Content Models
q Any typeØ Default content model when you declare an element in XML
Schemao <element name=“anything”/>
== <element name=“anything” type=“anyType”/>
Ø Can contain any contents (completely unconstrained)o E.g., string, integer, elements, …, or mixture of them
q Empty contentØ Can carry attributes, but prevent any content appearing as text or
child elementso <element name=“empty” xsi:nil=“true”/>
v xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
q Mixed content and element onlyØ Mixture of text contents and elements
o <xsd:element name="letterBody"><xsd:complexType mixed="true"> ...
v Default: mixed=“false” (element-only)
42웹기술응용설계
Mixed Content Model
[ XML Schema ]
[ Valid XML Document ]
43웹기술응용설계
Model Groups
q sequence group
Ø Only allows the specified order in an instance
q choice group
Ø Allows only one of its children to appear in an instance
q all group
Ø Unordered subset of individual elements
o Children may appear in any order
o Children may appear once or not (no more than once)
o Children must all be individual elements (no groups)
o Children are restricted to contain local elements or refs to top-level elements declarations only
Ø Must appear
o as the sole child
o at the top of content model
44웹기술응용설계
Cardinality Constraints
q minOccursØ Value: nonnegative integer (Default = 1)
q maxOccursØ Value: nonnegative integer or “unbounded” (Default = 1)
q Examples<element name=“e1” /> ==<element name=“e1” minOccurs=“1” maxOccurs=“1”/>
à DTD: <!ELEMENT e0 (e1)>
<element name=“e1” minOccurs=“0” maxOccurs=“1”/>à DTD: <!ELEMENT e0 (e1?)>
<element name=“e1” minOccurs=“0” maxOccurs=“unbounded”/>à DTD: <!ELEMENT e0 (e1*)>
<element name=“e1” minOccurs=“1” maxOccurs=“unbounded”/>à DTD: <!ELEMENT e0 (e1+)>
<element name=“e1” minOccurs=“3” maxOccurs=“10”/>
à can not be described with DTD
45웹기술응용설계
Content Model Example: Sequence & Choice
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:choice>
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
</xsd:sequence>
<xsd:element name="singleUSAddress" type="USAddress"/>
</xsd:choice>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
46웹기술응용설계
Content Model Example: Sequence & Choice
<PurchaseOrder orderDate=“2012-03-29”>
<shipTo> ... </shipTo><billTo> ... </billTo><comment> ... </comment><items> ... </items>
</PurchaseOrder>
<PurchaseOrder orderDate=“2012-03-29”>
<singleUSAddress> ... </singleUSAddress><comment> ... </comment><items> ... </items>
</PurchaseOrder>
<PurchaseOrder orderDate=“2012-03-29”>
<singleUSAddress> ... </singleUSAddress><items> ... </items>
</PurchaseOrder>
[ Valid XML documents ]
47웹기술응용설계
Content Model Example: All
<xsd:complexType name="PurchaseOrderType"><xsd:all><xsd:element name="shipTo" type="USAddress"/><xsd:element name="billTo" type="USAddress"/><xsd:element ref="comment" minOccurs="0"/><xsd:element name="items" type="Items"/>
</xsd:all><xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
<xsd:complexType name="PurchaseOrderType"><xsd:sequence><xsd:all><xsd:element name="shipTo" type="USAddress"/><xsd:element name="billTo" type="USAddress"/><xsd:element name="items" type="Items"/>
</xsd:all><xsd:sequence><xsd:element ref="comment" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence></xsd:sequence><xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
*** Illegal: used in a sequence group ***
48웹기술응용설계
Outline
q History
q Comparison with DTD
q SyntaxØ Definitions and declaration
Ø Simple types
Ø Namespace
Ø Complex types
Ø Content models
Ø Secondary componentso Attribute groups
o Annotation
o Redefinition
o Abstract element
o Null element
49웹기술응용설계
Attribute Groups & Annotation
q Attribute groups
Ø Improves readability of
schemas and facilitates
updating schemas
Ø Must appear at the end of
complex type definitions
q Annotation
Ø Annotating schemas for
benefit of human readers
and applications as well
Ø Use “xml:lang” attribute to
indicate the language of the
information
<attributeGroup name="myAttrGroup"><attribute . . . />. . .
</attributeGroup>
<complexType name="myelement">. . .
<attributeGroup ref="myAttrGroup"/></complexType>
<annotation><documentation xml:lang=“en”>
… Some documentation …</documentation>
</annotation>
50웹기술응용설계
Redefinition
q To provide some support for evolution or versioningof XML schema files
Ø The redefine mechanism enables you to redefine simple
and complex types, groups, and attribute groups that are
obtained from external schema files (i.e., restriction or
extension from the original version)
o Cf., the include mechanism enables you to use externally
created schema components “as-is” (i.e., without any
modification)
Ø Redefinition requires the external components to be in the
same target namespace as the redefined schema
51웹기술응용설계
Redefinition Example
… targetNamespace="http://mycom.com/IPO" …
<complexType name="personName"><sequence>
<element name="title" minOccurs="0"/><element name="forename" minOccurs="0“ maxOccurs="unbounded"/>
</sequence></complexType>
… targetNamespace="http://mycom.com/IPO” xmlns:p="http://mycom.com/IPO" …
<redefine schemaLocation=“v1.xsd"><complexType name="personName">
<complexContent><extension base=“p:personName">
<sequence><element name="generation" minOccurs="0"/>
</sequence></extension>
</complexContent></complexType>
</redefine>
<element name="author" type="personName"/>
v1.xsd :
v2.xsd :
52웹기술응용설계
Substitution Groups
q Substitution groups allow elements to be substituted for other elements
Ø Head element
o a particular named element to be substituted
o E.g., ipo:comment
Ø Substitutableelements
o E.g., shipComment, customerComment
v Both types of elements must be declared as
global elements
<element name="ipo:comment" type="string"/><element name="shipComment" type="string"
substitutionGroup="ipo:comment"/><element name="customerComment" type="string"
substitutionGroup="ipo:comment"/>
<element name="item" minOccurs="0" maxOccurs="unbounded"><complexType>
<sequence><element name="productName" type="string"/><element ref="ipo:comment" maxOccurs=“unbounded"/><element name="shipDate" type="date" minOccurs="0"/>
</sequence></complexType>
</element>
<item partNum="833-AA"><productName>Lapis necklace</productName><ipo:comment>Order #1</ipo:comment><ipo:shipComment>Use gold wrap</ipo:shipComment><ipo:customerComment>Want ASAP!</ipo:customerComment><shipDate>1999-12-05</shipDate>
</item>
53웹기술응용설계
Abstract Elements and Abstract Types
q Abstract elements can NOT
be used in an instance
document
Ø To make a substitutable
element MUST appear in
the instance document
o From the previous example
<element name=“ipo:comment" type="string” abstract="true"/>
q Abstract type elements
Ø Must be used with a
specific derived type in
instance documents
o E.g., <transport> element
<!-- Abstract type example --><complexType name="Vehicle" abstract="true"/>
<complexType name="Car"> <complexContent>
<extension base="ns:Vehicle"/></complexContent>
</complexType>
<complexType name="Plane"> <complexContent>
<extension base="ns:Vehicle"/></complexContent>
</complexType>
<element name="transport" type="ns:Vehicle"/>
<transport/> à Schema invalid
<transport xsi:type="ns:Car"/> à Schema valid
* xsi="http://www.w3.org/2001/XMLSchema-instance"
54웹기술응용설계
Null Element
q An element that can has a null value as its content
Ø Meaning of null value
o Unknown
o Not applicable
o The element may be absent
Ø XML has no actual null value that appears as element
contento E.g.) <shipDate>null</shipDate> à “null” string value
q Using attribute “xsi:nil”Ø In the schema: nillable=“true”Ø In the document: xsi:nil=“true”
/>
55웹기술응용설계
References
q XML Schema 1.0 (2nd Ed) Part 0: Primer
Ø http://www.w3.org/TR/xmlschema-0/
q XML Schema 1.0 (2nd Ed) Part 1: Structures
Ø http://www.w3.org/TR/xmlschema-1/
q XML Schema 1.0 (2nd Ed) Part 2: Data types
Ø http://www.w3.org/TR/xmlschema-2/
q Namespaces 1.0 (3rd Ed) in XML
Ø http://www.w3.org/TR/xml-names/