xml 6.5 xml schema (xsd) 6. what is xml schema? the origin of schema xml schema documents are...
Post on 14-Jan-2016
Embed Size (px)
XML6.5 XML Schema (XSD)6
What is XML Schema?The origin of schemaXML Schema documents are used to define and validate the content and structure of XML dataXML Schema was originally proposed by Microsoft, but became an official W3C recommendation in May 2001http://www.w3.org/XML/Schema
Why Schema? InformationStructureFormatTraditional Document: Everything is clumped togetherInformationStructureFormatFashionable Document: A document is broken into discrete parts, which can be treated separatelySeparating Information from Structure and Format
Why Schema? Schema Workflow
DTD vs. Schema Limitations of DTDNo constraints on character dataNot using XML syntaxNo support for namespaceVery limited for reusability and extensibilityAdvantages of SchemaSyntax in XML StyleSupporting Namespace and import/includeMore data typesAble to create complex data type by inheritanceInheritance by extension or restrictionMore
Problems of XML SchemaGeneral ProblemSeveral-hundred-page spec in a very technical languagePractical Limitations of expressibilitycontent and attribute declarations cannot depend on attributes or element contextTechnical ProblemThe notion of type adds an extra layer of confusing complexity
XML.org Registry The XML.org Registry offers a central clearinghouse for developers and standards bodies to publicly submit, publish and exchange XML schemas, vocabularies and related documents
An XML Document Example
The Examples Schema
Referring to a SchemaTo refer to a DTD in an XML document, the reference goes before the root element: ... To refer to an XML Schema in an XML document, the reference goes in the root element: ...
The XSD DocumentSince the XSD is written in XML, it can get confusing which we are talking aboutThe file extension is .xsdThe root element is The XSD starts like this:
The 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
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 isnt simple, its complexA complex element may have attributesA complex element may be empty, or it may contain text, other elements, or both text and other elements
Defining a Simple ElementA simple element is defined as where:name is the name of the elementthe most common values for type are xs:booleanxs:integer xs:datexs:string xs:decimalxs:timeOther attributes a simple element may have:default="default value" if no other value is specifiedfixed="value" no other value may be specified
Defining an AttributeAttributes themselves are always declared as simple typesAn attribute is defined as 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 present
Restrictions, or FacetsThe general form for putting a restriction on a text value is: (or xs:attribute) ... the restrictions ... For example:
Restrictions, or FacetsThe age" element is a simple type with a restriction. The acceptable values are: 20 to 100The example above could also have been written like this:
Restrictions on numbersminInclusivenumber must be the given valueminExclusive number must be > the given valuemaxInclusive number must be the given valuemaxExclusive number must be < the given valuetotalDigits number must have exactly value digitsfractionDigitsnumber must have no more than value digits after the decimal point
Restrictions on stringslength the string must contain exactly value characters minLengththe string must contain at least value charactersmaxLengththe 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 space
EnumerationAn enumeration restricts the value to be one of a fixed set of valuesExample:
Complex ElementsA complex element is defined as ... information about the complex type... Example:
Complex ElementsAnother example using a type attribute
xs:sequenceWeve already seen an example of a complex type whose elements must occur in a specific order:
- xs:allxs:all allows elements to appear in any order
Text Element with AttributesIf a text element has attributes, it is no longer a simple type
Empty ElementsEmpty elements are (ridiculously) complex
Mixed ElementsMixed elements may contain both text and elementsWe add mixed="true" to the xs:complexType elementThe text itself is not mentioned in the element, and may go anywhere (it is basically ignored)
Dear Mr.John Smith. Your order 1032 will be shipped on 2001-07-13.
ExtensionsYou can base a complex type on another complex type ...new stuff...
Predefined String TypesRecall that a simple element is defined as: Here are a few of the possible string types:xs:string - a stringxs:normalizedString - a string that doesnt contain tabs, newlines, or carriage returnsxs:token - a string that doesnt contain any whitespace other than single spacesAllowable restrictions on strings:enumeration, length, maxLength, minLength, pattern, whiteSpace
Predefined Date and Time Typesxs:date - A date in the format CCYY-MM-DD, for example, 2003-11-05xs:time - A date in the format hh:mm:ss (hours, minutes, seconds)xs:dateTime - Format is CCYY-MM-DDThh:mm:ssAllowable restrictions on dates and times:enumeration, minInclusive, maxExclusive, maxInclusive, maxExclusive, pattern, whiteSpace
Predefined Numeric TypesHere are some of the predefined numeric types:Allowable restrictions on numeric types: enumeration, minInclusive, maxExclusive, maxInclusive, maxExclusive, fractionDigits, totalDigits, pattern, whiteSpacexs:decimalxs:positiveIntegerxs:bytexs:negativeIntegerxs:shortxs:nonPositiveIntegerxs:intxs:nonNegativeIntegerxs:long
XML ParsersEvery XML application is based on a parserTwo types of XML documents:Well-formed:if it obeys the syntax of XML Valid:if it conforms to a proper definition of legal structure and elements of an XML documentTwo types of XML Parsers:Non-validatingValidating
Interfacing XML Documents with XML ApplicationsTwo WaysObject-based: DOMEvent-based: SAX
Available XML Schema-supported ParsersApache Xerces 2 Java/C++ freeValidating/Non-validating DOM and SAXMicrosoft XML Parser 4.0 freeDOM and SAXTIBCO XML Validate commercialSAX-based implementationSuitable in a streaming runtime environmentSourceForge.net JBind 1.0 freeA data binding framework linking Java and XMLIts Schema Compiler generates Java classes/interfaces for types contained in XML Schema. The runtime environment is used to read/write XML documents for validation, accessing and manipulating XML dataAnd many many more
Schema FeaturesObject-Oriented FeaturesDistinction between types and instances. Schema type definitions are independent of instance declarationsInheritanceRelational information FeaturesLike tree structure; having parents and childrenStrongly-typed: strong typing available in the specification
Xml schema enable translations from XML documents to databases.
What is XML Software Development process? Begin with developing content model using XML Schema or DTD2. Edit and validate XML documents according to the content model3. Finally, the XML document is ready to be used or processed by an XML enabled framework
What is XML Software Development process?
ReferencesW3School XSD Tutorialhttp://www.w3schools.com/schema/default.aspMSXML 4.0 SDKSeveral online presentations
Reading ListW3School XSD Tutorialhttp://www.w3schools.com/schema/default.asp
Non-validating XML parser ensures an XML document meets the general rules of XML (e.g. non-empty tags must be properly nested, each non-empty start tag must correspond to an end tag. Besides enforcing the syntactic rules, validating XML parser does more complicated work to ensure an instance XML document obeys the