100 likes | 303 Views
XML Schema. Describing the structure of XML documents. A very brief introduction. 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
E N D
XML Schema Describing the structure of XML documents. A very brief introduction XML Schema
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 XML Schema
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 XML Schema
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> XML Schema
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 • Right click the project node • Chose” Add Existing Item…” • You can validate XML documents as you write them • With your XML document, go to the ”Properties” window • Select ”Schemas” and press the small ”…” button • In Visual Studio it’s not enough to make a link from the XML document to the XML schema file (next slide) XML Schema
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> XML Schema
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> XML Schema
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"/> XML Schema