XML Schema Definition LanguageXML SchemasWhy XML Schemas?Why not XML schemas?Referring to a schemaThe XSD document<schema>“Simple” and “complex” elementsDefining a simple elementDefining an attributeRestrictions, or “facets”Restrictions on numbersRestrictions on stringsEnumerationComplex elementsGlobal and local definitionsDeclaration and usexs:sequencexs:allReferencingText element with attributesEmpty elementsMixed elementsExtensionsPredefined string typesPredefined date and time typesPredefined numeric typesOpinionThe EndJan 14, 2019XML Schema Definition Language(XSD)2XML Schemas“Schemas” is a general term--DTDs are a form of XML schemasAccording to the dictionary, a schema is “a structured framework or plan”When we say “XML Schemas,” we usually mean the W3C XML Schema LanguageThis is also known as “XML Schema Definition” language, or XSDI’ll use “XSD” frequently, because it’s shortDTDs, XML Schemas, and RELAX NG are all XML schema languages3Why XML Schemas?DTDs provide a very weak specification languageYou can’t put any restrictions on text contentYou have very little control over mixed content (text plus elements)You have little control over ordering of elementsDTDs are written in a strange (non-XML) formatYou need separate parsers for DTDs and XMLThe XML Schema Definition language solves these problemsXSD gives you much more control over structure and contentXSD is written in XML4Why not XML schemas?DTDs have been around longer than XSD Therefore they are more widely usedAlso, more tools support themXSD is very verbose, even by XML standardsMore advanced XML Schema instructions can be non-intuitive and confusingNevertheless, XSD is not likely to go away quickly5Referring to a schemaTo refer to a DTD in an XML document, the reference goes before the root element:<?xml version="1.0"?><!DOCTYPE rootElement SYSTEM "url"><rootElement> ... </rootElement>To refer to an XML Schema in an XML document, the reference goes in the root element:<?xml version="1.0"?><rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" (The XML Schema Instance reference is required) xsi:noNamespaceSchemaLocation="url.xsd"> (This is where your XML Schema definition can be found) ...</rootElement>6The XSD documentSince the XSD is written in XML, it can get confusing which we are talking aboutExcept for the additions to the root element of our XML data document, the rest of this lecture is about the XSD schema documentThe file extension is .xsdThe root element is <schema>The XSD starts like this:<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.rg/2001/XMLSchema">7<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 definedelementFormDefault="qualified"This means that all XML elements must be qualified (use a namespace)It is highly desirable to qualify all elements, or problems will arise when another schema is added8“Simple” and “complex” elementsA “simple” element is one that contains text and nothing elseA simple element cannot have attributesA simple element cannot contain other elementsA simple element cannot be emptyHowever, the text can be of many different types, and may have various restrictions applied to itIf an element isn’t simple, it’s “complex”A complex element may have attributesA complex element may be empty, or it may contain text, other elements, or both text and other elements9Defining a simple elementA simple element is defined as <xs:element name="name" type="type" />where:name is the name of the elementthe most common values for type are xs:boolean xs:integer xs:date xs:string xs:decimal xs:timeOther attributes a simple element may have:default="default value" if no other value is specifiedfixed="value" no other value may be specified10Defining an attributeAttributes themselves are always declared as simple typesAn attribute is defined as <xs:attribute name="name" type="type" />where:name and type are the same as for xs:elementOther attributes a simple element may have:default="default value" if no other value is specifiedfixed="value" no other value may be specifieduse="optional" the attribute is not required (default)use="required" the attribute must be present11Restrictions, or “facets”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>For example:<xs:element name="age"> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"> <xs:maxInclusive value="140"> </xs:restriction></xs:element>12Restrictions on numbersminInclusive -- 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 digitsfractionDigits -- number must have no more than value digits after the decimal point13Restrictions on stringslength -- the string must contain exactly value characters minLength -- the string must contain at least value charactersmaxLength -- the string must contain no more than value characterspattern -- the value is a regular expression that the string must matchwhiteSpace -- not really a “restriction”--tells what to do with whitespacevalue="preserve" Keep all whitespacevalue="replace" Change all whitespace characters to spacesvalue="collapse" Remove leading and trailing whitespace, and replace all sequences of whitespace with a single space14EnumerationAn enumeration restricts the value to be one of a fixed set of valuesExample:<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>15Complex elementsA complex element is defined as <xs:element name="name">
View Full Document