CMP 051 XML Introduction Session IV

Slides:



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

1 XML DTD & XML Schema Monica Farrow G30
A Simple Schema Design. First Schema Design Being a Dog Is a Full-Time Job Charles M. Schulz Snoopy Peppermint Patty extroverted beagle Peppermint.
XML Schema Definition Language
1 Week5 – Schema Why Schema? Schemas vs. DTDs Introduction – W3C vs. Microsoft XDR Schema, How To? Element Types – Simple vs. Complex Attributes Restrictions/Facets.
XML Schemas. “Schemas” is a general term--DTDs are a form of XML schemas –According to the dictionary, a schema is “a structured framework or plan” When.
Enabling Grids for E-sciencE ISSGC’05 XML Schemas (XSD) Richard Hopkins, National e-Science Centre, Edinburgh June 2005.
Sunday, June 28, 2015 Abdelali ZAHI : FALL 2003 : XML Schemas XML Schemas Presented By : Abdelali ZAHI Instructor : Dr H.Haddouti.
1 XML Schema Information Retrieval Systems Maria Indrawan 2003.
Unit 4 – XML Schema XML - Level I Basic.
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
XP New Perspectives on XML Tutorial 3 1 DTD Tutorial – Carey ISBN
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation XML Schema 1 Lecturer.
XML Schema Vinod Kumar Kayartaya. What is XML Schema?  XML Schema is an XML based alternative to DTD  An XML schema describes the structure of an XML.
Dr. Azeddine Chikh IS446: Internet Software Development.
CSE4500 Information Retrieval Systems XML Schema – Part 1.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML.
XP 1 DECLARING A DTD A DTD can be used to: –Ensure all required elements are present in the document –Prevent undefined elements from being used –Enforce.
Session IV Chapter 9 – XML Schemas
1 Tutorial 13 Validating Documents with DTDs Working with Document Type Definitions.
Of 33 lecture 3: xml and xml schema. of 33 XML, RDF, RDF Schema overview XML – simple introduction and XML Schema RDF – basics, language RDF Schema –
Beginning XML 4th Edition. Chapter 5: XML Schemas.
New Perspectives on XML, 2nd Edition
 XML DTD and XML Schema Discussion Sessions 1A and 1B Session 2.
XML Schema. Why Schema? To define a class of XML documents Serve same purpose as DTD “Instance document" used for XML document conforming to schema.
XML – Part III. The Element … This type of element either has the element content or the mixed content (child element and data) The attributes of the.
An Introduction to XML Sandeep Bhattaram
XML Introduction. What is XML? XML stands for eXtensible Markup Language XML stands for eXtensible Markup Language XML is a markup language much like.
Sheet 1XML Technology in E-Commerce 2001Lecture 2 XML Technology in E-Commerce Lecture 2 Logical and Physical Structure, Validity, DTD, XML Schema.
XSD Presented by Kushan Athukorala. 2 Agenda XML Namespaces XML Schema XSD Indicators XSD Data Types XSD Schema References.
XML 2nd EDITION Tutorial 4 Working With Schemas. XP Schemas A schema is an XML document that defines the content and structure of one or more XML documents.
1 Tutorial 14 Validating Documents with Schemas Exploring the XML Schema Vocabulary.
Tutorial 13 Validating Documents with Schemas
XML Schema. Why Validate XML? XML documents can generally have any structure XML grammars define specific document structures Validation is the act of.
Processing of structured documents Spring 2003, Part 3 Helena Ahonen-Myka.
Introduction to XML Schema John Arnett, MSc Standards Modeller Information and Statistics Division NHSScotland Tel: (x2073)
XSD: XML Schema Language Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
Deriving Complex Types In XML Schema By: Roy Navon.
XML Schema Lecture 3. Indicators There are seven indicators: Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators:
XML Validation II Advanced DTDs + Schemas Robin Burke ECT 360.
XML Schema – Simple Type Web site:
CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting.
4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema.
Extensible Markup Language (XML) Pat Morin COMP 2405.
Session III Chapter 10 – Defining Simple Types
XML Schema.
ACG 4401 XML Schemas XML Namespaces XLink.
ACG 4401 XML Schemas XML Namespaces XLink.
Lecture 9 XML & its applications
Eugenia Fernandez IUPUI
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
Discussion on XSD open issues
XML Validation III Schemas
Session III Chapter 6 – Creating DTDs
ISSGC’05 XML Schemas (XSD)
Design and Implementation of Software for the Web
XML Data DTDs, IDs & IDREFs.
CMP 051 XML Introduction Session IV Chapter 10 – Defining Simple Types
DTD and XML Schema.
New Perspectives on XML
CMP 051 XML Introduction Session III
XML Technologies X-Schema.
CMP 051 XML Introduction Session IV Chapter 12 – XML Namespaces
Optimising XML Schema for IODEF Data model
Lecture 9 XML & its applications
Session II Chapter 6 – Creating DTDs
CMP 051 XML Introduction Session IV Chapter 12 – XML Namespaces
XML Schema Diyar A. Abdulqder
New Perspectives on XML
Presentation transcript:

CMP 051 XML Introduction Session IV Chapter 11 – Defining Complex Types http://www.profburnett.com

Outline Why XML Schemas? Complex XML Element Types Basics Complex XML Indicators Controlling How Many Defining Empty Elements Defining Elements to Contain Only Text Defining Complex Types that Contain Child Elements Requiring Child Elements to Appear in Sequence Allowing Child Element to Appear in Any Order Defining Anonymous Complex Types Deriving Named Complex Types Defining Element with Mixed Content Deriving Complex Types from Existing Complex Types Creating a Set of Choices Defining Named Model Groups Reference a Named Model Group Defining Attributes Requiring an Attribute Predefining an Attribute’s Content Defining Attribute Groups Referencing Attribute Groups Local and Global Definitions Referencing Globally Defined Elements 8/1/2014 Copyright © Carl M. Burnett

Why XML Schemas? XML-based alternative to DTD. More powerful than DTDs. The XML Schema language = XML Schema Definition (XSD). Support Data Types Use XML Syntax Secure and Accurate Data Communication Extensible Reusable Individual Data Types based n Standard Type Multiple schemas Well-Formed is Not Enough Must have XML Declaration Unique Root Element Matching Start and End Tags Case-Sensitive Elements Must be Closed Elements Must be Properly Nested Attribute Values Must be Quoted Entities Must be used for Special Characters 8/1/2014 Copyright © Carl M. Burnett

Complex XML Element Types Basics Four Types of Complex Elements Empty Text Element Only – Child Elements Mixed Content Empty Complex Element <source sectionid="101“ newspaperid="21"/> Text Complex Element <year_built era="BC">282</year_built> Element Only Complex Element <ancient_wonders> <wonder> ... </wonder> </ancient_wonders> Mixed Content Complex Element <description> It happened on <date lang="norwegian">03.03.99</date> .... </description> 8/1/2014 Copyright © Carl M. Burnett

Complex XML Indicators There are Seven Indicators: Order Indicators: All Choice Sequence Occurrence Indicators: maxOccurs minOccurs Group Indicators: Group name attributeGroup name 8/1/2014 Copyright © Carl M. Burnett

Controlling How Many Occurrence Indicators: maxOccurs minOccurs <xs:complexType name="wonderType"> <xs:sequence> ... <xs:element name="contributor"> <xs:complexType> <xs:element ref="name" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

Defining Empty Elements Type <xs:complexType. Type name="complex_type_name">, where complex_type_name identifies your new complex type. Declare the attributes that should appear in this complex type element, if any. Type </xs:complexType> to complete the complex type definition. <xs:complexType name="sourceType"> <xs:attribute name="sectionid“ type="xs:positiveInteger"/> <xs:attribute name="newspaperid“ </xs:complexType> 8/1/2014 Copyright © Carl M. Burnett

Defining Elements to Contain Only Text Type <xs:complexType. 2. Then, type name="complex_type_name">, where complex_type_name identifies your new complex type. Type <xs:simpleContent>. Type <xs:extension to use a simple type for the text value of the element. Or type <xs:restriction to limit the base simple type with additional facets. Type base="foundation">, where foundation indicates the simple type on which you’re basing the new complex type element. If you chose xs:restriction in Step 4, declare the additional facets that should limit the simple content in this complex type definition. Declare the attributes that should appear in this complex type element, if any. Type </xs:extension> or </xs:restriction> to match Step 4. Type </xs:simpleContent>. Type </xs:complexType> to complete the complex type definition. <xs:complexType name="yearType"> <xs:simpleContent> <xs:extension base= "xs:positiveInteger"> <xs:attribute name="era" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> 8/1/2014 Copyright © Carl M. Burnett

Defining Complex Types that Contain Child Elements Type <xs:complexType. Type name="complex_type_name">, where complex_type_name identifies your new complex type. Next, you’ll define the structure and order of the child elements of this parent element. You’ll declare a sequence, an unordered list, or a choice. Then, declare the attributes that should appear in this complex type element, if any. Type </xs:complexType> to complete the “element only” complex type definition. <xs:element name="ancient_wonders"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:anyType"> <xs:sequence> <xs:element name="wonder“ type="wonderType"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

Requiring Child Elements to Appear in Sequence Type <xs:sequence. If desired, specify how many times the sequence of elements itself can appear by setting the minOccurs and maxOccurs attributes. Type > to complete the opening tag. Declare the simple type elements and/or complex type elements you want in the sequence, in the order in which they should appear. Finally, type </xs:sequence> to complete the model group. <xs:complexType name="wonderType"> <xs:sequence> <xs:element name="name“ type="nameType"/> <xs:element name="location“ type="xs:string"/> <xs:element name="height“ type="heightType"/> <xs:element name="history“ type="historyType"/> <xs:element name="main_image“ type="imageType"/> <xs:element name="source“ type="sourceType"/> </xs:sequence> </xs:complexType> Tips ■ A sequence defines the order in which its child elements must appear in an XML document. ■ Since an XML element may only have one child, it’s perfectly legitimate for a sequence to contain only one element. ■ A sequence can also contain other sequences, choices, or references to named groups. ■ A sequence may be contained in a complex type definition, other sequences, a set of choices, or in named group definitions. ■ The <xs:sequence> element is basically equivalent to the comma (,) in DTDs. 8/1/2014 Copyright © Carl M. Burnett

Allowing Child Element to Appear in Any Order Type <xs:all to begin the unordered list of elements. Optionally, you can specify how many times the unordered list itself can appear by setting the minOccurs and maxOccurs attributes. Then, type > to complete the opening tag. Declare the simple type elements and/or complex type elements that you want in the unordered list. Finally, type </xs:all> to complete the model group. <xs:complexType name="historyType"> <xs:all> <xs:element name="year_built" type="yearType"/> <xs:element name="year_destroyed" <xs:element name="how_destroyed" type="destrType"/> <xs:element name="story" type="storyType"/> </xs:all> </xs:complexType> Tips ■ The members of an xs:all element (despite its name) may appear once or not at all (depending on their individual minOccurs and maxOccurs attributes), in any order. ■ The minOccurs attribute may only be set to 0 or 1. The maxOccurs attribute may only be set to 1. ■ An xs:all element can only contain individual element declarations or references, not other groups. In addition, no element may appear more than once. ■ An xs:all element can only be contained in, and must be the sole child of, an element only complex type definition. 8/1/2014 Copyright © Carl M. Burnett

Defining Anonymous Complex Types Begin the definition of the element by typing <xs:element name="label">, where label is the name of the XML element that you are defining. Type <xs:complexType> to begin the anonymous complex type. Within the xs:complexType element: Declare the content type to be either simple content or complex content. Create the guts of the element . Define the attributes that should appear, if any. Type </xs:complexType> to complete the anonymous complex type definition. Type </xs:element> to complete the definition of the complex type element. <xs:element name="year_built"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:positiveInteger"> <xs:attribute name="era“ type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

Deriving Named Complex Types Type <xs:complexType to begin the named complex type. Type name="complex_type_name">, where complex_type_name identifies your new complex type. Within the xs:complexType element: Declare the content type to be either simple content or complex content . Create the guts of the element . Define the attributes that should appear, if any. Type </xs:complexType> to complete the named complex type definition. Then, to use the named complex type for the definition of the XML element, you type <xs:element name="label“ type="complex_type_name">, where complex_type_name is the name you gave the new complex type in Step 2 above. <xs:complexType name="yearType”> <xs:simpleContent> <xs:extension base="xs:positiveInteger"> <xs:attribute name="era“ type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> <xs:complexType name="historyType"> <xs:sequence> <xs:element name="year_built“ type="yearType”/> <xs:element name="year_destroyed“ 8/1/2014 Copyright © Carl M. Burnett

Defining Element with Mixed Content Type <xs:complexType. Type name="complex_type_name", where complex_type_name identifies your new complex type. Type mixed="true"> to indicate that the element can contain elements and text, (and may even contain attributes as well). Declare a sequence, an unordered list, or a choice to specify the child elements and structure within the complex type. Declare the attributes that should appear in this complex type element, if any. Type </xs:complexType> to complete the complex type definition. <xs:complexType name="story" mixed="true"> <xs:sequence> <xs:element name="para" maxOccurs="unbounded"> <xs:complexType/> </xs:element> </xs:sequence> </xs:complexType> 8/1/2014 Copyright © Carl M. Burnett

Deriving Complex Types from Existing Complex Types Type <xs:complexType. Type name="complex_type_name", where complex_type_name identifies your new complex type. Type <xs:complexContent>. Type <xs:extension to indicate that features will be added to the existing complex type. Or type <xs:restriction to indicate that features will be removed from the existing complex type. Type base="existing_type">, where existing_type identifies the name of the existing type from which the new complex type will be derived. Declare the attributes that should be part of the new complex type. Type a matching closing tag for Step 4. Type </xs:complexContent>. Type </xs:complexType> to complete the complex type definition. <xs:complexType name="historyType"> <xs:sequence> <xs:element name="year_built" type="yearType"/> <xs:element name="year_destroyed" type="yearType" minOccurs="0"/> <xs:element name="how_destroyed" type="destrType" minOccurs="0"/> <xs:element name="story" type="storyType"/> </xs:sequence> </xs:complexType> 8/1/2014 Copyright © Carl M. Burnett

Deriving Complex Types from Existing Complex Types <xs:complexType name="newHistoryType"> <xs:complexContent> <xs:extension base="historyType"> <xs:sequence> <xs:element name="who_built" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> extension <xs:complexType name="historyType"> <xs:sequence> <xs:element name="year_built" type="yearType"/> <xs:element name="year_destroyed" type="yearType" minOccurs="0"/> <xs:element name="how_destroyed" type="destrType" minOccurs="0"/> <xs:element name="story" type="storyType"/> </xs:sequence> </xs:complexType> <xs:complexType name="newHistoryType"> <xs:complexContent> <xs:restriction base="historyType"> <xs:sequence> <xs:element name="year_built" type="yearType"/> <xs:element name="year_destroyed" <xs:element name="how_destroyed" type="destrType" fixed="fire"/> <xs:element name="story" type="storyType"/> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> restriction 8/1/2014 Copyright © Carl M. Burnett

Creating a Set of Choices Type <xs:choice. Optionally, you can specify how many times the set of choices itself can appear by setting the minOccurs and maxOccurs attributes. Type > to complete the opening tag. Declare the simple type elements and/or complex type elements that you want to make up the set of choices. Type </xs:choice> to complete the model group. <xs:complexType name="wonderType"> <xs:sequence> <xs:element name="name" type="nameType"/> <xs:choice> <xs:element name="location" type="xs:string"/> <xs:element name="city" <xs:element name="country" </xs:sequence> </xs:choice> ... </xs:complexType> 8/1/2014 Copyright © Carl M. Burnett

Defining Named Model Groups Type <xs:group. Type name="model_group_name", where model_group_name identifies your group of elements. Type > to complete the opening group tag. Declare sequences, unordered lists, and/or sets of choices that will make up the named model group. Type </xs:group> to complete the definition of the group. <xs:group name="image_element"> <xs:sequence> <xs:element name="image"> <xs:complexType> <xs:attribute name="file" type="xs:anyURI"/> <xs:attribute name="w" type="xs:positiveInteger"/> <xs:attribute name="h" </xs:complexType> </xs:element> <xs:element name="source" type="xs:string"/> </xs:sequence> </xs:group> 8/1/2014 Copyright © Carl M. Burnett

Defining Attributes To use an anonymous simple type: Within the definition of the complex type, type <xs:attribute. Type name="attribute_name", where attribute_name is the name of the XML attribute that you are defining. To use an anonymous simple type: Type > to complete the opening tag. Then, type <xs:simpleType>. Add any restrictions (or facets) you like. Type </xs:simpleType> to close the simple type element. Finally, type </xs:attribute> to close the opening tag. To use a base or named simple type: Type type="simple_type"/>, where simple_type is the named or base type of the attribute that you are defining. To use a globally defined attribute: Type ref="label"/>, where label identifies an attribute definition that you’ve already globally defined. 8/1/2014 Copyright © Carl M. Burnett

Defining Attributes <xs:complexType name="sourceType"> <xs:attribute name="sectionid" type="xs:positiveInteger"/> <xs:attribute name="newspaperid"> <xs:simpleType> <xs:restriction base="xs:positiveInteger"> <xs:pattern value="\d{4}"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> 8/1/2014 Copyright © Carl M. Burnett

Requiring an Attribute To require that an attribute be present: <xs:complexType name="sourceType"> <xs:attribute name="sectionid" type="xs:positiveInteger" use="required"/> <xs:attribute name="newspaperid"> <xs:simpleType> <xs:restriction base="xs:positiveInteger"> <xs:pattern value="\d{4}"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:complexType> Within an attribute definition, type use="required" to indicate that the attribute must appear for an XML document to be considered valid. You may also add value="must_be", where must_be is the only acceptable value for the attribute. To require that an attribute not be present: Within an attribute definition, type use=“prohibited" so that the XML document will only be considered valid if the attribute is not present. 8/1/2014 Copyright © Carl M. Burnett

Predefining an Attribute’s Content To dictate an attribute’s content: Within an attribute definition, type fixed="content", where content determines what the value of the attribute should be for the document to be considered valid <xs:attribute name="sectionid" type="xs:positiveInteger"/> <xs:attribute name="newspaperid" type="xs:positiveInteger" fixed="21"/> To set an attribute’s initial value: Within an attribute definition, type default="content", where content determines the value the that attribute should be set to if it is omitted from the XML document. <xs:attribute name="sectionid" type="xs:positiveInteger"/> <xs:attribute name="newspaperid" type="xs:positiveInteger" default="21"/> 8/1/2014 Copyright © Carl M. Burnett

Defining Attribute Groups Type <xs:attributeGroup. Type name="attribute_group_ name">, where attribute_group_name identifies your attribute group. Define or reference each attribute that belongs to the group. Type </xs:attributeGroup> to complete the attribute group definition. <xs:attributeGroup name="imageAttrs"> <xs:attribute name="file" type="xs:anyURI" use="required"/> <xs:attribute name="w" type="xs:positiveInteger" use="required"/> <xs:attribute name="h" </xs:attributeGroup> 8/1/2014 Copyright © Carl M. Burnett

Referencing Attribute Groups Within a complex type definition, after declaring any elements that should be contained, type <xs:attributeGroup. Type ref="attribute_group_name"/>, to identify the attribute group that you created in Step 2. <xs:complexType name="imageType"> <xs:attributeGroup ref="imageAttrs"/> </xs:complexType> <xs:complexType name="videoType"> <xs:attribute name="format" type="xs:string"/> 8/1/2014 Copyright © Carl M. Burnett

Local and Global Definitions A globally defined element is defined as a child of the xs:schema element. A globally defined element scope allows it to be used anywhere in the entire schema. A locally defined element is defined as the child of some other element. A locally defined element scope is within its parent element only. Globally defined elements, like named custom types, do not automatically become part of an XML Schema. Global elements must be explicitly referenced in order to actually appear in a valid XML document. Locally defined elements, like anonymous custom types, automatically become part of an XML document. Where Locally defined elements are defined determines where in the XML document the element must appear. One of the benefits of using locally defined elements is that the element’s scope is isolated. An isolated scope means that the element’s name and definition cannot conflict with other elements in the same XML Schema using the same name. Which one to choose is dependent on your need for reusability, versus your need to isolate an element and its definition. 8/1/2014 Copyright © Carl M. Burnett

Referencing Globally Defined Elements Global elements are defined as children of the xs:schema root element. Named complex types are an example of an element that are globally defined. You can also globally define an individual element. To use globally defined elements in the XML Schema document, they must be called or referenced. To Reference a Globally Defined Element In the sequence unordered list or set of choices in which the element should appear, type <xs:element. Type ref="label", where label is the name of the globally defined element. If desired, specify how many times the element can appear at this point using minOccurs or maxOccurs. Type /> to complete the global element reference . <xs:complexType name="wonderType"> <xs:sequence> <xs:element ref="name"/> <xs:element name="location“ type="xs:string"/> <xs:element name="height" type="heightType"/> 8/1/2014 Copyright © Carl M. Burnett

Review Complex XML Element Types Basics Complex XML Indicators Defining Empty Elements Defining Elements to Contain Only Text Defining Complex Types that Contain Child Elements Requiring Child Elements to Appear in Sequence Allowing Child Element to Appear in Any Order Defining Anonymous Complex Types Deriving Named Complex Types Defining Element with Mixed Content Deriving Complex Types from Existing Complex Types Creating a Set of Choices Defining Named Model Groups Reference a Named Model Group Controlling How Many Defining Attributes Requiring an Attribute Predefining an Attribute’s Content Defining Attribute Groups Referencing Attribute Groups Local and Global Definitions Referencing Globally Defined Elements 8/1/2014 Copyright © Carl M. Burnett