Upload
daniel-haynes
View
216
Download
0
Embed Size (px)
Citation preview
CMP 051XML Introduction
Session IV Chapter 9 – XML Schemas
http://www.profburnett.com
Copyright © Carl M. Burnett 2
OutlineXML Schema BasicsXML Schema Data TypesXML Schema SyntaxXML Schema ExtensibilityCreating an XML SchemaAssociate an XML Schema to an XML DocumentAnnotating an XML Schema
8/1/2014
Copyright © Carl M. Burnett 3
XML Schema BasicsXML Schema defines: Elements AttributesChild ElementsOrder of Child ElementsNumber of Child ElementsWhether an Element is Empty or can Include TextData Types for Elements and AttributesDefault and Fixed Values for Elements and Attributes
8/1/2014
Copyright © Carl M. Burnett 4
Supports Data TypesDescribe allowable document contentValidate the correctness of dataWork with data from a databaseDefine data facets (restrictions on data)Define data patterns (data formats)Convert data between different data types
8/1/2014
Copyright © Carl M. Burnett 5
Uses XML SyntaxDon't have to learn a new languageUse your XML editor to edit your Schema filesUse your XML parser to parse your Schema filesManipulate your Schema with the XML DOMTransform your Schema with XSLT
8/1/2014
Copyright © Carl M. Burnett 6
Syntax RulesMust begin with the XML declarationMust have one unique root elementStart-tags must have matching end-tagsElements are case sensitiveElements must be closedElements must be properly nestedAttribute values must be quotedEntities must be used for special characters
8/1/2014
Copyright © Carl M. Burnett 7
XML Schemas are ExtensibleReuse your Schema in other SchemasCreate your own data types derived from the standard
typesReference multiple schemas in the same document
8/1/2014
Copyright © Carl M. Burnett 8
Creating an XML Schema
8/1/2014
1. At top of document type <?xml version=“1.0”?> 2. Type <xs:schema to define root element.3. Type Xmlns:xs=“http://www.w3c.org/2001/
XMLSchema” to declare the XML schema namespace. 4. Type > to complete the root element tag. 5. Type </xs:schema> to complete the root element.
<?xml version="1.0"?><xs:schema Xmlns:xs="http://www.w3.org/2001/XMLSchema">
. . .
</xs:schema>
Copyright © Carl M. Burnett 9
Associate an XML Schema to an XML Document
8/1/2014
1. Inside the root element your XML document type: xmlns:xsi=“http://www.w3c.org/2001/XMLSchema”
2. Type xsi:noNamespaceSchemaLocation=3. Type ”xsd:uri” where xsd.uri is the location of the
XML Schema file.
<?xml version="1.0"?><wonder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="09-06.xsd"> <name>Colossus of Rhodes</name> <location>Greece</location> <height>107</height></wonder>
Copyright © Carl M. Burnett 10
Annotating an XML SchemaYou can include standard XML
comments in a XML schema document.
XML Schema comments are annotations.
Annotations can be parsed.
8/1/2014
<?xml version="1.0"?><xs:schema xmlns= "http://www.w3.org/2001/XMLSchema">
<xs:annotation> <xs:documentation>This XML Schema will be used to validate the set of XML documents for the Wonders of the World project. </xs:documentation></xs:annotation>
. . .
Copyright © Carl M. Burnett 11
ReviewXML Schema BasicsXML Schema Data TypesXML Schema SyntaxXML Schema ExtensibilityCreating an XML SchemaAssociate an XML Schema to an XML DocumentAnnotating an XML Schema
8/1/2014
Next: Chapter 10 – Defining Simple
XML Element Types