Upload
godfrey-woods
View
213
Download
0
Embed Size (px)
Citation preview
www.tech.findforinfo.com 1
Schemas
www.tech.findforinfo.com 2
What is a Schema
• a schematic or preliminary plan • Description of a structure, details ...
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 ...
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
www.tech.findforinfo.com 5
Validation
• The Structure of the XML document is Validated against the XML schema
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.
www.tech.findforinfo.com 7
W3C Recommendation
• In 2 May 2001 XML Schema became a W3C recommendation Standard
www.tech.findforinfo.com 8
The XSD document
• written in XML• The file extension is .xsd• The root element is <schema>
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
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
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
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
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>
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
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
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
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>
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
www.tech.findforinfo.com 19
Needed namespaces
• Using System.IO;• Using System.Xml;• Using System.Xml.schema;
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
www.tech.findforinfo.com 21
Provide the Validation Type
vr.ValidationType =ValidationType.Schema;
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
www.tech.findforinfo.com 23
Loop through the file
while (vr.Read())
www.tech.findforinfo.com 24
Create Validation Event handler
public void MyValidateHandler(object sender, ValidationEventArgs e) {
Label1.Text += "Error:" + e.Message + "<br>"; }
www.tech.findforinfo.com 25
Check for exception
while (vr.Read()) { vr.ValidationEventHandler += MyValidateHandler; }