Top Banner
DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of Architecture Wei Peng [email protected] What is XML Schema ? XML Schema is an XML based description of your data’s business rules – alternative to DTD; An XML schema expresses the structure of an XML document as DTD does; The XML Schema language is also referred to as XML Schema Definition (XSD); What XML Schema can while DTD can’t – complex datatype of elements or attributes – think about how to describe an attribute which is an integer ranges from 1 to 100?
19

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

What is XML Schema ?

XML Schema is an XML based description of your data’s business rules – alternative to DTD;

An XML schema expresses the structure of an XML document as DTD does;

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

What XML Schema can while DTD can’t – complex datatype of elements or attributes – think about how to describe an attribute which is an integer ranges from 1 to 100?

Page 2: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Validate your XML documents I:

Page 3: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Validate your XML documents II:

Online Validate (This version is for schema documents with the namespace URI http://www.w3.org/2001/XMLSchema) at http://www.w3.org/2001/03/webdata/xsv

Topologi at http://www.topologi.com/

DTD to Schema Converter at:

LuMriX dtd2xs http://lists.w3.org/Archives/Public/xmlschema-dev/2001Jun/0116.html

Syntext Dtd2Xs http://www.syntext.com/products/index.htm#Dtd2Xs

GUI Oriented:

XML Spy – http://www.xmlspy.com Turbo XML– http://www.extensibility.com

Page 4: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Why DTD is out?

You have to learn different syntax – a syntax different and inconsistent with XML files you are working on;

Limited datatype capability – DTDs support a very limited capability for specifying datatypes. For example, you can not express "I want the <age> element to hold an integer with a range of 1 to 100“;

DTD supports 10 datatypes;

XML Schema supports 44+ datatypes.

Page 5: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Why Schema I?

XML Schema has Support for Data Types – it is easier to describe permissible document content and validate the correctness of data, etc.

XML Schemas use XML Syntax – You don't have to learn another language – you can use your XML editor and parser;

XML Schemas Secure Data Communication – with XML Schemas, the sender can describe the data in a way that the receiver will understand – a “Key”;

XML Schemas are Extensible – just like XML, because they are written in XML –reuse your Schema in other Schemas, create your own data types, etc.

Page 6: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Why Schema II?

Well-Formed is not Enough – a well-formed XML document is a document that conforms to the XML syntax rules;

Even if documents are Well-Formed can have serious consequences because the XML file may violate business rules;

Such as: you order 5 gross of laser printers, and you make a mistake in your XML file – 5 laser printers. With XML Schemas, most of these errors can be caught by your validating software.

Page 7: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Why Schema III – Namespace:

The main reason for using a schema instead of a DTD is the ability to mix namespaces;

An XML namespace is a collection of names, identified by a URI reference [RFC2396], which are used in XML documents as element types and attribute names;

XML document may contain elements and attributes that are defined for and used by multiple software modules – However, such XML files contain multiple markup vocabularies which can pose problems of recognition and collision;

These require components have universal names – XML namespace is the mechanism to solve this problem.

Page 8: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Compare Schema with the Related XML

<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="example.xsd">

<orderperson>John Smith</orderperson>

<shipto>

<name>Ola Nordmann</name>

<address>Langgt 23</address>

<city>4000 Stavanger</city>

<country>Norway</country>

</shipto>

<item>

<title>Empire Burlesque</title>

<note>Special Edition</note>

<quantity>1</quantity>

<price>10.90</price>

</item>

</shiporder>

Page 9: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Compare DTD with the Related Schema

<!ELEMENT BookStore (Book+)>

<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)>

<!ELEMENT Title (#PCDATA)>

<!ELEMENT Author (#PCDATA)>

<!ELEMENT Date (#PCDATA)>

<!ELEMENT ISBN (#PCDATA)>

<!ELEMENT Publisher (#PCDATA)>

Page 10: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

DTD:

Page 11: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Schema:

Page 12: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Schema Tips:

For Tag <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.books.org" xmlns="http://www.books.org" elementFormDefault="qualified">

All Schema has “schema” as root element. And xmlns:xsd =

"http://www.w3.org/2001/XMLSchema" indicates that all XML-Schema elements are to be prefixed with an xsd: tag;

Target nameplace can act as an identification in the data streams when more than one schemas are involved in – it is optional;

The elements and datatypes that are used to construct schemas – schema, element, complexType, sequence, string, etc. are come from the http://www.w3.org/2001/XMLSchema

namespace;

The default namespace is http://www.books.org which is the targetNamespace;

elementFormDefault="qualified“ – This is a directive to any instance documents which conform to this schema: Any elements used by the instance document which were declared in this schema must be namespaced – for example <a:aircraft>Boeing 747</a:aircraft>.

Page 13: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Referencing Schema in XML instance document I:

<?xml version="1.0"?><BookStore xmlns ="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.org BookStore.xsd"> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> ...</BookStore>

The line xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" indicates that we want to use elements defined in the http://www.w3.org/2001/XMLSchema-instance definition;

Page 14: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Referencing Schema in XML instance document II:

First, using a default namespace declaration, tell the schema-validator that all of the elementsused in this instance document come from the http://www.books.org namespace.

Second, with schemaLocation tell the schema-validator that the http://www.books.org namespace is defined by BookStore.xsd,

Note: The xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes can be used in a document to provide hints as to the physical location of schema documents which may be used forassessment.

Third, tell the schema-validator that the schemaLocation attribute we are using is the one inthe XMLSchema-instance namespace.

Page 15: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

No targetNamespace (noNamespaceSchemaLocation

Sometimes you may wish to create a schema but without associating the elements with a namespace;

The targetNamespace attribute is actually an optional attribute of <schema>. Thus, if you don’t want to specify a namespace for your schema then simply don’t use the targetNamespace attribute;

Note that there is no default namespace declaration. So, none of the elements are associated with a namespace;

Note that we do not use xsi:schemaLocation (since it requires a pair of values - a namespace and a URL to the schema for that namespace). Instead, use xsi:noNamespaceSchemaLocation

For Example, in XML instance file: <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="example.xsd">

Page 16: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Assembling an Instance Document from More than one schema

An instance document may be composed of elements from multiple schemas.<?xml version="1.0"?><Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.book.org Book.xsd http://www.employee.org Employee.xsd"> <Books> <Book xmlns="http://www.book.org"> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>1998</Date> <ISBN>1-56592-235-2</ISBN> <Publisher>Macmillan Publishing</Publisher> </Book> </Books> <Employees> <Employee xmlns="http://www.employee.org"> <Name>John Doe</Name> <SSN>123-45-6789</SSN> </Employee> </Employees></Library>

Page 17: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Include method:<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema“ targetNamespace="http://www.library.org"xmlns="http://www.library.org“ elementFormDefault="qualified"> <xsd:include schemaLocation="LibraryBook.xsd"/> <xsd:include schemaLocation="LibraryEmployee.xsd"/> <xsd:element name="Library"> <xsd:complexType> <xsd:sequence> <xsd:element name="Books"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Employees"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Employee" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element></xsd:schema>

Page 18: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

Building a Schema from Multiple Schema Documents with Different Namespaces – use import method

<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.camera.org" xmlns:nikon="http://www.nikon.com" xmlns:olympus="http://www.olympus.com" xmlns:pentax="http://www.pentax.com" elementFormDefault="qualified"> <xsd:import namespace="http://www.nikon.com" schemaLocation="Nikon.xsd"/> <xsd:import namespace="http://www.olympus.com" schemaLocation="Olympus.xsd"/> <xsd:import namespace="http://www.pentax.com" schemaLocation="Pentax.xsd"/> <xsd:element name="camera"> <xsd:complexType> <xsd:sequence> <xsd:element name="body" type="nikon:body_type"/> <xsd:element name="lens" type="olympus:lens_type"/> <xsd:element name="manual_adapter"

type="pentax:manual_adapter_type"/> </xsd:sequence> </xsd:complexType> </xsd:element><xsd:schema>

Page 19: DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema School of Architecture, Design Science and Planning Faculty of.

DECO 3002 Advanced Technology Integrated Design Computing Studio Tutorial 6 – XML Schema

School of Architecture, Design Science and PlanningFaculty of Architecture

Wei [email protected]

References:

http://www.w3schools.com/default.asp http://www.w3.org/XML/Schema http://www.w3.org/TR/xmlschema-0/ http://www.w3.org/TR/xmlschema-1/ http://www.w3.org/TR/xmlschema-2/• Roger L. Costello, XML Technologies Course at http://www.xfront.com/