25
Schemas 1 www.tech.findforinfo.com

Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

Embed Size (px)

Citation preview

Page 1: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 1

Schemas

Page 2: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 2

What is a Schema

• a schematic or preliminary plan • Description of a structure, details ...

Page 3: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 3

XMLSchema

• An XML schema defines the structure of an XML document. An XML schema defines things such as which data elements and attributes can appear in a ...

Page 4: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 4

What do Schemas provide to XML?

• Validation• Documentation• Querying support (Xpath,Xquery will

process for some functions sort,equality)• Data Binding

Page 5: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 5

Validation

• The Structure of the XML document is Validated against the XML schema

Page 6: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 6

Schema languages

• RDF (Resource Description Framework)• XML – Data• XML - DR (XML – Data Reduced)• DCD (Document Content Description)• SOX (Schema for Object-Oriented XML) Definition• DDML (Document Definition Markup Language)• BizTalk Schema• Schematron• ! XML Schema (or W3C Schema) is a W3C proposed• recommendation.

Page 7: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 7

W3C Recommendation

• In 2 May 2001 XML Schema became a W3C recommendation Standard

Page 8: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 8

The XSD document

• written in XML• The file extension is .xsd• The root element is <schema>

Page 9: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 9

<schema>

• The <schema> element may have attributes:– xmlns:xs="http://www.w3.org/2001/XMLSchema"

• This is necessary to specify where all our XSD tags are defined

– elementFormDefault="qualified"• This means that all XML elements must be qualified

Page 10: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 10

“Simple” and “complex” elements

• A “simple” element is one that contains text and nothing else

• cannot have attributes– cannot contain other elements– cannot be empty

• A complex element may have attributes

Page 11: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 11

Defining a simple element• A simple element is defined as

<xs:element name="name" type="type" />where:– name is the name of the element– the most common values for type are

xs:boolean xs:integer xs:date xs:string xs:decimal xs:time

Page 12: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 12

Defining an attribute

• Attributes themselves are always declared as simple types• An attribute is defined as

<xs:attribute name="name" type="type" />where:– name and type are the same as for xs:element

Page 13: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 13

Restrictions

• The general form for putting a restriction on a text value is

–<xs:element name="name"> (or xs:attribute) <xs:restriction base="type"> ... the restrictions ... </xs:restriction></xs:element>

Page 14: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 14

Restriction Example

<xs:element name="age"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"> <xs:maxInclusive value="140"> </xs:restriction></xs:element

Element nameBase data type

Restriction applied

Page 15: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 15

Restrictions on numbers

• minInclusive -- number must be ≥ the given value

• minExclusive -- number must be > the given value

• maxInclusive -- number must be ≤ the given value

• maxExclusive -- number must be < the given value

• totalDigits -- number must have exactly value digits

• fractionDigits -- number must have no more than

value digits after the decimal point

Page 16: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 16

Restrictions on strings

• length -- the string must contain exactly value characters

• minLength -- the string must contain at least value characters

• maxLength -- the string must contain no more than value

characters

• pattern -- the value is a regular expression that the string must

match

• whiteSpace -- not really a “restriction”--tells what to do with

whitespace

– value="preserve" Keep all whitespace

– value="replace" Change all whitespace characters to spaces– value="collapse" Remove leading and trailing whitespace, and

replace all sequences of whitespace with a single space

Page 17: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 17

Enumeration• An enumeration restricts the value to be one of a

fixed set of values• Example:

– <xs:element name="season"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Spring"/> <xs:enumeration value="Summer"/> <xs:enumeration value="Autumn"/> <xs:enumeration value="Fall"/> <xs:enumeration value="Winter"/> </xs:restriction> </xs:simpleType></xs:element>

Page 18: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 18

Validating XML against the XSD schema

• Create an XML file• Create XSD schema (from the menu)• Save the XSD in the Current application• Check out the Schema

Page 19: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 19

Needed namespaces

• Using System.IO;• Using System.Xml;• Using System.Xml.schema;

Page 20: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 20

Create the objects

FileStream fs=new FileStream(Server.MapPath("SuperProductList.xml"),FileMode.Open,FileAccess.Read);

XmlTextReader r=new XmlTextReader(fs);

XmlValidatingReader vr=new XmlValidatingReader(r);

To read the XML file

Validation class object

Page 21: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 21

Provide the Validation Type

vr.ValidationType =ValidationType.Schema;

Page 22: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 22

Schema collection

XmlSchemaCollection schemas=new XmlSchemaCollection(); schemas.Add("SuperProductList",Server.MapPath("SuperProductList.xsd"));

Retrieve the .XSD file and add it to the Schema collection

vr.Schemas.Add(schemas);

Add schema to the Validating object

Page 23: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 23

Loop through the file

while (vr.Read())

Page 24: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 24

Create Validation Event handler

public void MyValidateHandler(object sender, ValidationEventArgs e) {

Label1.Text += "Error:" + e.Message + "<br>"; }

Page 25: Schemas 1. What is a Schema a schematic or preliminary plan Description of a structure, details... 2

www.tech.findforinfo.com 25

Check for exception

while (vr.Read()) { vr.ValidationEventHandler += MyValidateHandler; }