xml schema describing the structure of xml documents. a very brief introduction 1xml schema

8
XML Schema Describing the structure of XML documents. A very brief introduction 1 XML Schema

Upload: vanessa-cunningham

Post on 22-Dec-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 1

XML Schema

Describing the structure of XML documents.

A very brief introduction

Page 2: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 2

What is XML Schema?

• XML schema is used to describe the structure of XML documents– Name of elements + attributes– Sub-elements– Sequence of elements, etc.

• XML Schema is itself an XML application• XSD = XML Schema Definition– Filename.xsd holds an XML Schema

• XML Schema is a W3C recommendation, 2001

Page 3: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 3

Validation

• An XML documents can be validated against its XML schema– Check that all element, etc. Of the XML document

conforms with the XML schema

Page 4: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 4

XML schema, example<?xml version="1.0" encoding="utf-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="students"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" name="student"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string" /> <xs:element name="address" type="xs:string" /> </xs:sequence> <xs:attribute name="id" type="xs:unsignedShort" use="required" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>

Page 5: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 5

XML schema support in Visual Studio Ultimate

• You can generate an XML schema from an XML document– Menu: XML → Create Schema

• The schema is saved in your home directory.• If you want it in your Visual Studio you must

1. Right click the project node2. Chose” Add Existing Item…”

• You can validate XML documents as you write them1. With your XML document, go to the ”Properties” window2. Select ”Schemas” and press the small ”…” button3. In Visual Studio it’s not enough to make a link from the

XML document to the XML schema file (next slide)

Page 6: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 6

Reference to an XML schemafrom an XML document

<?xml version="1.0" encoding="utf-8" ?><students xmlns="http://www.rhs.dk/andersb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.rhs.dk/andersb students.xsd"> <student id="14593"> <name>Anders</name> <address>Roskilde</address> </student> <student id="1111"> <name>Bill</name> <address>London</address> </student></students>

Page 7: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 7

XSD simple elements• Simple element has no child elements

– <xs:element name="xxx" type="yyy"/>– Some common types

• xs:string, • xs:decimal, xs:integer• xs:boolean• xs:date, xs:time• User defines types

– Restrictions on data types, example: interval• <xs:element name="age">

<xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType></xs:element>

Page 8: XML Schema Describing the structure of XML documents. A very brief introduction 1XML Schema

XML Schema 8

XSD complex elements

• Four kinds of complex elements– empty elements

• may have attributes

– elements that contain only other elements• <xs:sequence>

– elements that contain only text• <xs:simpleContent>

– elements that contain both other elements and text• <xs:complexType mixed="true">

• Attribute– Child element of <xs:complexType …>– <xs:attribute name="xxx" type="yyy"/>