Download presentation
Presentation is loading. Please wait.
Published byGodfrey Woods Modified over 9 years ago
1
Schemas 1www.tech.findforinfo.com
2
What is a Schema a schematic or preliminary plan Description of a structure, details... 2www.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... 3www.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 4www.tech.findforinfo.com
5
Validation The Structure of the XML document is Validated against the XML schema 5www.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. 6www.tech.findforinfo.com
7
W3C Recommendation In 2 May 2001 XML Schema became a W3C recommendation Standard 7www.tech.findforinfo.com
8
The XSD document written in XML The file extension is.xsd The root element is 8www.tech.findforinfo.com
9
The 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 9www.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 10www.tech.findforinfo.com
11
Defining a simple element A simple element is defined as where: –name is the name of the element –the most common values for type are xs:booleanxs:integer xs:datexs:string xs:decimalxs:time 11www.tech.findforinfo.com
12
Defining an attribute Attributes themselves are always declared as simple types An attribute is defined as where: –name and type are the same as for xs:element 12www.tech.findforinfo.com
13
Restrictions The general form for putting a restriction on a text value is – (or xs:attribute)... the restrictions... 13www.tech.findforinfo.com
14
Restriction Example </xs:element Element name Base data type Restriction applied 14www.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 15www.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 16www.tech.findforinfo.com
17
Enumeration An enumeration restricts the value to be one of a fixed set of values Example: – 17www.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 18www.tech.findforinfo.com
19
Needed namespaces Using System.IO; Using System.Xml; Using System.Xml.schema; 19www.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 20www.tech.findforinfo.com
21
Provide the Validation Type vr.ValidationType =ValidationType.Schema; 21www.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 22www.tech.findforinfo.com
23
Loop through the file while (vr.Read()) 23www.tech.findforinfo.com
24
Create Validation Event handler public void MyValidateHandler(object sender, ValidationEventArgs e) { Label1.Text += "Error:" + e.Message + " "; } 24www.tech.findforinfo.com
25
Check for exception while (vr.Read()) { vr.ValidationEventHandler += MyValidateHandler; } 25www.tech.findforinfo.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.