TEAMFLY Team-Fly ® XML Schema Essentials John Wiley & Sons, Inc. Wiley Computer Publishing R. Allen Wyke Andrew Watt XML Schema Essentials John Wiley & Sons, Inc. Wiley Computer Publishing R. Allen Wyke Andrew Watt Publisher: Robert Ipsen Editor: Cary Sullivan Developmental Editor: Scott Amerman Associate Managing Editor: Penny Linskey Associate New Media Editor: Brian Snapp Text Design & Composition: D&G Limited, LLC Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where John Wiley & Sons, Inc., is aware of a claim, the product names appear in initial capital or ALL CAPITAL LETTERS. Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration. This book is printed on acid-free paper. Copyright © 2002 by R. Allen Wyke and Andrew Watt. All rights reserved. Published by John Wiley & Sons, Inc. Published simultaneously in Canada. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copy- right Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4744. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 605 Third Avenue, New York, NY 10158-0012, (212) 850-6011, fax (212) 850- 6008, E-Mail: PERMREQ @ WILEY.COM. This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold with the understanding that the publisher is not engaged in professional services. If professional advice or other expert assistance is required, the services of a competent professional person should be sought. Library of Congress Cataloging-in-Publication Data: ISBN: 0-471-412597 Printed in the United States of America. 10987654321 Introduction ix Acknowledgments xi About the Authors xiii Part One Getting Started 1 Chapter 1 Elementary XML Schema 3 What Is XML Schema? 5 How Does an XML Schema Processor Work? 6 What Is XML Schema for? 7 XSD Schema Schema Components 7 Other Schema Languages for XML 8 The DTD Descended from SGML 8 XSD Schema Tools 9 XML Schema Document 14 CHAPTER Contents iii Root of an XML Schema Document 16 Declaring the Location of Your XML Schema Document 16 Declaring Elements and Defining Types 17 Defining Simple Types 17 Defining Complex Types 25 Anonymous Complex Types 25 Named Complex Types 26 Using Anonymous or Named Complex Types 29 Declarations 34 Annotations in Schema 42 Standard XML Comments 42 The <annotation> Element 43 Empty Element Declaration 45 The anyType Type 50 Occurrence Constraints 51 Cardinality in DTDs 51 minOccurs and maxOccurs 51 Defining Your Own Simple Type 56 Model Groups in Schema 57 Sequence Group 57 Choice Group 57 All Group 58 Attribute Groups 61 More about the XML 1.0 DTD Content Model 62 Validation in XSD Schema 63 Validation versus Assessment 64 XML Information Set 67 Post-Schema Validation Infoset 69 Summary 69 Chapter 2 XSD Elements 71 XML Elements 72 Defining within a DTD 72 Limitations 76 iv Contents Moving On to XSD Elements 77 <xsd:element>: A Closer Examination 81 Default Values 83 Substitution Groups 85 Null Values 92 Attributes 93 Complex Content 99 Importing Elements from Other Locations 107 Redefining Elements 111 More on <xsd:complexType> 112 Using a Sequence 114 Grouping 114 Summary 119 Chapter 3 Adding Attributes 121 What Are Attributes? 122 Additional Metadata 122 Application Uses 125 Storing Data 126 Hybrid Approaches 128 Considerations for Using Attributes 130 XML Attributes Foundation 130 Syntax 130 Capabilities 131 XSD Attributes: The Next Generation 132 Syntax Changes 132 Further Capabilities 134 Using Attributes 136 Scope 136 Qualification 138 Defaults 140 Grouping 141 Inclusion of Other Attributes 142 Summary 142 Contents v Part Two Going Beyond DTDs 145 Chapter 4 Applying Datatypes 147 What Are Datatypes? 147 Primitive Datatypes 149 Derived Datatypes 152 Defining Our Own Datatypes 152 More on Simple Types 157 Defining Lists 160 Creating a Union 162 Constraining Type Definitions 167 Controlling Digits 171 Handling White Space 174 Pattern Matching 180 Applicability of Facets 181 Summary 183 Chapter 5 Data Facets 185 Fundamental and Constraining Facets 186 Constraining Facets in XSD Schema 186 The length Element 187 The minLength Element 195 The maxLength Element 198 The pattern Element 200 Parts Catalog Example 202 Postal Code Examples 206 The enumeration Element 211 Simple Enumeration Example 212 U.S. States Example 213 The whiteSpace Element 216 Summary 217 Chapter 6 More about Data Facets 219 The maxExclusive Element 220 The maxInclusive Element 223 The minExclusive Element 225 vi Contents The minInclusive Element 228 The totalDigits and fractionDigits Facets 233 Summary 238 Chapter 7 Grouping Elements and Attributes 239 Reusing Definitions with Groups 240 Nesting Sequence Groups 245 Nesting Choice Groups 246 Substitution Groups 250 Attribute Groups 257 Summary 258 Chapter 8 Deriving Types 259 Deriving Types by Extension 260 Deriving Types by Restriction 271 The enumeration Element 279 The pattern Element 282 The xsi:type Attribute 284 Summary 285 Part Three Next Steps 287 Chapter 9 Uniqueness and Keys in XSD Schema 289 Identity-Constraint Definitions 289 The <xsd:unique> Element 290 The <xsd:key> and <xsd:keyref> Elements 298 Summary 303 Chapter 10 Bringing the Parts Together 305 Modularizing Schemas 305 How to Use Schema Modules 306 Creating the Example 325 Planning the Example 325 Defining the Information Needs 325 Documenting the Schema 326 Basic Schema Templates 327 Modularizing the Schemas 328 Contents vii [...]... a schema language as simply XML Schema In reality, a number of other XML schema languages existed for some time before W3C completed the development of XML Schema So, to avoid ambiguity, when we refer to the specification for the W3C flavor of XML Schema, we will use the terms W3C XML Schema or XSD Schema to refer to W3C’s type of XML Schema, because an earlier name for the W3C XML Schema was XML Schema. .. the XSD Schema namespace: N OT E If you see an XSD schema where the namespace declaration refers to www.w3.org/2000/10/XMLSchema, that indicates that the schema was created by using a non-final version of the specification Elementary XML Schema This is a sample XML Schema for Chapter 1 of XML Schema Essentials. .. Watt John Wiley Listing 1.1 Simple XML instance document (Book .xml) Elementary XML Schema < ?xml version="1.0" encoding="UTF-8"?> This is a sample XML Schema for Chapter 1 of XML Schema Essentials ... problems just mentioned, both Turbo XML and XML Spy are powerful and useful tools for XSD Schema development Each is capable of validating an instance document against a schema as well as validating an XSD schema for conformity to the W3C XML Schema Recommendation XML Schema Document In this section, we will look briefly at the general structure of an XML Schema schema such as the one that you saw... A first attempt at an instance document (SimpleBook01 .xml) < ?xml version="1.0" encoding="UTF-8"?> Listing 1.5 A schema for Listing 1.4 (SimpleBook01.xsd) Elementary XML Schema You will recognize the XML declaration and the element To declare an element with simple type... structures that XML Schema supports, and is located at www.w3.org/TR/2001/REC-xmlschema-1-20010502/ Part 2 is also a normative W3C document, defines the datatypes that W3C XML Schema supports, describes mechanisms for creating new datatypes, and is located at www.w3.org/TR/2001/REC-xmlschema-2-20010502/ An XSD Schema schema is intended to define the structure and constrain the content of a class of XML documents... does not use namespaces with an applicable schema document Listing 1.3 shows how this job can be done < ?xml version="1.0"?> XML Schema Essentials R Allen Wyke Andrew Watt John... (Book02 .xml) Elementary XML Schema The association of an instance document is a two-stage process The namespace declaration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" associates the namespace prefix xsi with the URI shown The xsi:noNamespaceSchemaLocation attribute, which belongs to the namespace http://www.w3.org/2001/XMLSchema-instance indicates the location of the schema The xsi:noNamespaceSchemaLocation... name="Publisher" type="xsd:string"/> Listing 1.2 W3C XML Schema syntax describing content of Listing 1.1 (Book.xsd) What Is XML Schema? XML Schema is the W3C-recommended schema definition language, expressed in XML 1.0 syntax, which is intended to describe the structure and constrain the content of documents written in XML It is explicitly intended to improve on the schema functionality that was provided... recommendations, the W3C XML Schema Recommendation is one of the most complex, and at times abstract, XML technology specifications In this book, we will be emphasizing aspects of W3C XML Schema that are practical, using many examples of W3C 3 Chapter 1 AM FL Y XML schemas and introducing the theory that sheds light on the practical use of schemas Let’s take a quick look at a simple XML schema so that you . Elementary XML Schema 3 What Is XML Schema? 5 How Does an XML Schema Processor Work? 6 What Is XML Schema for? 7 XSD Schema Schema Components 7 Other Schema. Team-Fly ® XML Schema Essentials John Wiley & Sons, Inc. Wiley Computer Publishing R. Allen Wyke Andrew Watt XML Schema Essentials John Wiley