XML How to Program

Why Rent from Knetbooks?

Because Knetbooks knows college students. Our rental program is designed to save you time and money. Whether you need a textbook for a semester, quarter or even a summer session, we have an option for you. Simply select a rental period, enter your information and your book will be on its way!

This comprehensive guide to programming in XML teaches readers how to use XML to create customized tags and addresses standard custom markup languages for science and technology, multimedia, commerce, and other fields.Includes a concise introduction to Java, as well as cutting edge topics such as XQL, SMIL and VoiceXML as well as a real-world e-Commerce case study. Also includes a complete chapter on Web-accessibility that addresses VoiceXML. Provides hundreds of valuable programming tips.For XML programmers and Internet/Web programmers.

Dr. Harvey M. Deitel, CEO of Deitel & Associates, Inc., has 40 years experience in the computing field including extensive industry and academic experience. He is one of the world's leading computer science instructors and seminar presenters. Dr. Deitel earned B.S. and M.S. degrees from the Massachusetts Institute of Technology and a Ph.D. from Boston University. He worked on the pioneering virtual memory operating systems projects at IBM and MIT that developed techniques widely implemented today in systems like UNIX, Linux and Windows NT. He has 20 years of college teaching experience including earning tenure and serving as the Chairman of the Computer Science Department at Boston College before founding Deitel & Associates, Inc. with Paul J. Deitel. He is author or co-author of several dozen books and multimedia packages and is currently writing many more. With translations published in Japanese, Russian, Spanish, Elementary Chinese, Advanced Chinese, Korean, French, Polish, Portuguese and Italian, Dr. Deitel's texts have earned international recognition. Dr. Deitel has delivered professional seminars internationally to major corporations, government organizations and various branches of the military.

Paul J. Deitel, Executive Vice President of Deitel & Associates, Inc., is a graduate of the Massachusetts Institute of Technology's Sloan School of Management where he studied Information Technology. Through Deitel & Associates, Inc. he has delivered Java, C, C++ and Internet and World Wide Web courses for industry clients including Compaq, Sun Microsystems, White Sands Missile Range, Rogue Wave Software, Computervision, Stratus, Fidelity, Cambridge Technology Partners, Boeing, Lucent Technologies, Adra Systems, Entergy, CableData Systems, NASA at the Kennedy Space Center, the National Severe Storm Laboratory, IBM and many other organizations. He has lectured on C++ and Java for the Boston Chapter of the Association for Computing Machinery, and has taught satellite-based Java courses through a cooperative venture of Deitel & Associates, Inc., Prentice Hall and the Technology Education Network. He and his father, Dr. Harvey M. Deitel, are the world's best-selling Computer Science textbook authors.

Tem R. Nieto, Director of Product Development with Deitel & Associates, Inc., is a graduate of the Massachusetts Institute of Technology where he studied engineering and computing. Through Deitel & Associates, Inc. he has delivered courses for industry clients including Sun Microsystems, Compaq, EMC, Stratus, Fidelity, Art Technology, Progress Software, Toys "R" Us, Operational Support Facility of the National Oceanographic and Atmospheric Administration, Jet Propulsion Laboratory, Nynex, Motorola, Federal Reserve Bank of Chicago, Banyan, Schlumberger, University of Notre Dame, NASA, various military installations and many others. He has co-authored several books and multimedia packages with the Deitels and has contributed to virtually every Deitel & Associates, Inc. publication.

Ted M. Lin is a senior at Carnegie Mellon University where he is double majoring in computer science and electrical/computer engineering. He enjoys building Web sites and internet applications involving leading-edge technologies.

Praveen Sadhu is a graduate student at Northeastern University majoring in engineering software design. He received his bachelor's degree in Electrical Engineering from Hyderabad, India.

Preface

xxvii

Introduction to the Internet and World Wide Web

1

(22)

Introduction

2

(1)

World Wide Web Consortium (W3C)

2

(1)

History of the Internet

3

(1)

History of the World Wide Web

4

(1)

Future of Computing

5

(1)

History of SGML

6

(1)

XML and XML How to Program

7

(3)

A Tour of the Book

10

(9)

W3C XML Resources

19

(1)

Internet and World Wide Web Resources

19

(4)

Introduction to HyperText Markup Language 4: Part I

23

(23)

Introduction

24

(1)

Markup Languages

25

(1)

Editing HTML

25

(1)

Common Elements

26

(2)

Headers

28

(1)

Linking

29

(3)

Images

32

(3)

Special Characters and More Line Breaks

35

(2)

Unordered Lists

37

(1)

Nested and Ordered Lists

38

(3)

Internet and World Wide Web Resources

41

(5)

Introduction to HyperText Markup Language 4: Part II

46

(33)

Introduction

47

(1)

Basic HTML Tables

47

(3)

Intermediate HTML Tables and Formatting

50

(2)

Basic HTML Forms

52

(3)

More Complex HTML Forms

55

(7)

Internal Linking

62

(2)

Creating and Using Image Maps

64

(3)

<meta> Tags

67

(1)

frameset Element

68

(2)

Nested framesets

70

(2)

Internet and World Wide Web Resources

72

(7)

Cascading Style Sheets™ (CSS)

79

(31)

Introduction

80

(1)

Inline Styles

80

(1)

Creating Style Sheets with the style Element

81

(3)

Conflicting Styles

84

(3)

Linking External Style Sheets

87

(2)

Positioning Elements

89

(3)

Backgrounds

92

(3)

Element Dimensions

95

(2)

Text Flow and the Box Model

97

(5)

User Style Sheets

102

(3)

Internet and World Wide Web Resources

105

(5)

Creating Markup with XML

110

(24)

Introduction

111

(1)

Introduction to XML Markup

111

(1)

Parsers and Well-formed XML Documents

112

(1)

Parsing an XML Document with msxml

113

(1)

Characters

114

(4)

Character Set

115

(1)

Characters vs. Markup

115

(1)

White Space, Entity References and Built-in Entities

115

(1)

Using Unicode in an XML Document

116

(2)

Markup

118

(4)

CDATA Sections

122

(1)

XML Namespaces

123

(3)

Case Study: A Day Planner Application

126

(1)

Internet and World Wide Web Resources

127

(7)

Document Type Definition (DTD)

134

(31)

Introduction

135

(1)

Parsers, Well-formed and Valid XML Documents

135

(1)

Document Type Declaration

136

(1)

Element Type Declarations

137

(8)

Sequences, Pipe Characters and Occurrence Indicators

139

(4)

EMPTY, Mixed Content and ANY

143

(2)

Attribute Declarations

145

(2)

Attribute Defaults (&num;Required, &num;Implied, &num;Fixed)

147

(1)

Attribute Types

147

(6)

Tokenized Attribute Type (ID, Idref, Entity, Nmtoken)

147

(6)

Enumerated Attribute Types

153

(1)

Conditional Sections

153

(2)

Whitespace Characters

155

(3)

Case Study: Writing a DTD for the Day Planner Application

158

(1)

Internet and World Wide Web Resources

159

(6)

Schemas

165

(27)

Introduction

166

(1)

Schema vs. DTDs

167

(1)

Microsoft XML Schema: Describing Elements

167

(7)

Microsoft XML Schema: Describing Attributes

174

(3)

Microsoft XML Schema: Data Types

177

(6)

W3C XML Schema

183

(2)

Case Study: Writing a Microsoft XML Schema for the Day Planner Application

Preface Live in fragments no longer. Only connect. Edward Morgan Forster Welcome to the exciting world of XML! This book is by an old guy and four young guys. The old guy (HMD; Massachusetts Institute of Technology 1967) has been programming and/or teaching programming for 40 years. The four young guys (PJD; MIT 1991, TRN; MIT 1992, TML; Carnegie Mellon 2001, PS; Northeastern 2000) have each been programming and/or teaching programming for many years. The old guy programs and teaches from experience; the young guys do so from an inexhaustible reserve of energy. The old guy wants clarity; the young guys want performance. The old guy seeks elegance and beauty; the young guys want results. We got together to produce a book we hope you will find informative, challenging and entertaining. Why We Wrote XML How to Program Today, XML is arguably the hottest technology in the computer industry. Therefore, university professors are eager to incorporate XML into their undergraduate and graduate Internet, Web, e-business and e-commerce curricula. Professionals are eager to use XML in their industrial-strength information-technology applications. Students are highly motivated by the fact that they are learning a leading-edge technology (XML) that will be immediately useful to them as they leave the university environment and head into a world where the Internet and World Wide Web have a massive prominence. After mastering the material in this book, students will be well prepared to take advantage of the Internet and the Web as they take upper-level courses and venture into the rapidly changing programming world. XML How to Programis the latest book in the Deitel/Prentice Hall How to Programseries. It is distinguished by its focus on XML-based application development using programming languages such as Java, VBScript and Perl. We have syntax-colored the code throughout the book. The key focus of this book is applications development with XML. Our audiences care about XML processing on the client, XML processing on the server, using XML encoded data as a database, etc. Many XML books are reference manuals with exhaustive listings of features. That is not our style. We concentrate on creating real, working applications. We provide the live-codeexamples on the CD accompanying this book (and on www.deitel.com) so that you can run the applications and see the results. We are excited about the enormous range of possibilities XML has to offer. We performed extensive research for this book and located hundreds of Internet and Web resources (which we provide as live links on the CD-ROM that accompanies this book and on www.deitel.com) to help you learn about XML and its related technologies. These links include general information, tutorials and demonstrations. Please read the tour of the book in Chapter 1 to familiarize yourself with the XML technologies we present. A cutting-edge technology, XML is constantly evolving. This creates tremendous challenges for us as authors, for our publisher--Prentice Hall, for instructors, and for students and professional people. We have worked hard to create useful live-codeexamples to help you master XML quickly and effectively. All of the code examples are on the accompanying disk and are available for free download from our Web sites: www.deitel.com www.prenhall.com/deitel Teaching Approach XML How to Programcontains a rich collection of examples and exercises drawn from many fields to provide the student with a chance to solve interesting real-world problems. The book concentrates on the principles of good software engineering and stresses clarity. We avoid arcane terminology and syntax specifications in favor of teaching by example. The book is written by educators who spend most of their time writing about and teaching edge-of-the-practice programming topics. Live-CodeTeaching Approach The book is loaded with live-codeexamples. This is the focus of the way we teach and write about programming, and the focus of our multimedia Cyber Classroomsand Web-Based Training Coursesas well. Each new concept is presented in the context of a complete, working program immediately followed by one or more windows showing the program''s input/output dialog. We call this style of teaching and writing our live-code approach. We use programming languages to teach programming languages.Reading these programs is much like entering and running them on a computer. XML How to Programshows how to create Web sites starting with HTML programming, then rapidly proceeding to programming in XML. HTML and XML are considered to be markup languages rather than programming languages, but many of our examples use XML in the context of Java, VBScript, Active Server Pages, Perl and JavaScript. For those readers who wish to review these programming technologies, we include full-chapter introductions to VBScript, Active Server Pages, Perl and Java. The Java treatment is especially substantial. World Wide Web Access All of the code for XML How to Program(and our other publications) is on the Internet free for download at the Deitel & Associates, Inc. Web site -- www.deitel.com Please download all the code and run each program as you read the text. Make changes to the code examples and immediately see the effects of those changes. It is a great way to learn programming. Note:You must respect the fact that this is copyrighted material. Feel free to use it as you study, but you may not republish any portion of it in any form without explicit permission from Prentice Hall and the authors. Objectives Each chapter begins with a statement of Objectives.This tells students what to expect and gives them an opportunity, after reading the chapter, to determine if they have met these objectives. It is a confidence builder and a source of positive reinforcement. Quotations The learning objectives are followed by quotations. Some are humorous, some are philosophical, and some offer interesting insights. Our students enjoy relating the quotations to the chapter material. Many of the quotations are worth a "second look" afteryou read each chapter. Outline The chapter Outlinehelps students approach the material in top-down fashion. This, too, helps students anticipate what is to come and set a comfortable and effective learning pace. Example XML documents (with Program Outputs) We present features in the context of complete, working XML documents. This is the focus of our teaching and our writing. We call it our live-codeapproach. Each Web document is followed by the outputs produced when the document is rendered in a Web browser (We use both Microsoft''s Internet Explorer 5 and Netscape 6) and its scripts are executed. This enables students to confirm that the Web pages are rendered as expected. Reading the book carefully is much like entering the code and rendering these documents on a computer. The documents range from just a few lines of code to substantial examples with several hundred lines of code. Students should download all the code for the book from our Web site, and run each program while studying that program in the text. Illustrations/Figures An abundance of charts, line drawings and program outputs is included. Programming Tips We have included programming tips to help students focus on important aspects of program development. We highlight these tips in the form of Good Programming Practices, Common Programming Errors, Testing and Debugging Tips, P