ebook img

Learn XML In a Weekend PDF

283 Pages·2002·13.984 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn XML In a Weekend

Learn XML in a Weekend ERIK WESTERMANN Premier Press, a division of Course Technology 2645 Erie Avenue, Suite 41 , Cincinnati , Ohio 45208 Copyright © 2002 by Premier Press, a division of Course Technology. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system without written permission from Premier Press, except for the inclusion of brief quotations in a review. The Premier Press logo and related trade dress are trademarks of Premier Press, Inc. and may not be used without written permission. Publisher: Stacy L. Hiquet Marketing Manager: Heather Hurley Managing Editor: Sandy Doell Acquisitions Editor: Todd Jensen Project Editor/Copy Editor: Sean Medlock Editorial Assistants: Margaret Bauer, Elizabeth Barrett Technical Reviewer: Michelle Jones Interior Layout: Marian Hartsough Cover Designer: Mike Tanamachi Indexer: Katherine Stimson Proofreader: Lorraine Gunter Extensible Markup Language (XML) 1.0 (Second Edition), © 2000 W3C (MIT, INRIA, Keio), All Rights Reserved. W3C liability, trademark, document use, and software licensing rules apply. The Unicode Consortium, UNICODE STANDARD VERSION 3.0, Fig. 2-3 pg. 14, © 2000, 1992 by Unicode, Inc. Reprinted by permission of Pearson Education, Inc. DocBook, © 1992–2000 HaL Computer Systems, Inc., O'Reilly & Associates, Inc., AborText, Inc., Fujitsu Software Corporation, Norman Walsh, and the Organization for the Advancement of Structured Information Standards (OASIS). All other trademarks are the property of their respective owners. Important: Premier Press cannot provide software support. Please contact the appropriate software manufacturer's technical support line or Web site for assistance. Premier Press and the author have attempted throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. Information contained in this book has been obtained by Premier Press from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, Premier Press, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aware of the fact that the Internet is an ever-changing entity. Some facts may have changed since this book went to press. ISBN: 1-59200-010-X Library of Congress Catalog Card Number: 2002106524 Printed in the United States of America 02 03 04 05 BH 10 9 8 7 6 5 4 3 2 1 For the two greatest boys in the world, my sons, Vikranth and Siddharth. ABOUT THE AUTHOR ERIK WESTERMANN is an independent, accomplished developer with more than 10 years of experience in professional programming and design. Erik also enjoys writing and has written for a number of publications on the Internet and in print. Erik's professional affiliations include the IEEE Computer Society (http://computer.org), the Association for Computing Machinery (http://acm.org), and the Worldwide Institute of Software Architects (http://wwisa.org), where he is a practicing member. Erik has spoken at conferences including VSLive 2001 in Sydney, Australia. Erik's Web site is http://www.designs2solutions.com. ACKNOWLEDGMENTS First and foremost, I'd like to thank Brad Jones for helping me get this project off the ground; Todd Jensen, acquisitions editor, for putting up with my "short" e-mails; Amy Pettinella, my project editor, for overseeing the project from (almost) the beginning; and Michelle Jones, technical editor, for her comments and suggestions. I would also like to thank Altova, the producers of XML Spy, for the copy of XML Spy, and Jon Bachman at eXcelon for helping to get a copy of eXcelon Stylus Studio for the readers of this book. I'd like to thank Tom Archer for his support throughout the project, and for helping me get my writing career started in the first place. I could not have done it without you. Thanks, Tom! I'd also like to thank my sons, Vikranth and Siddharth, for understanding when I was busy, and for the time they gave up spending with me so that I could produce this book for you. I'd also like to thank my wife, Shanthi, for her ceaseless support in all of my endeavors. Foreword The first time I met Erik was while running the popular CodeGuru Web site a few years ago, where he was responsible for writing the book reviews. While Erik's reviews had proven to be one of the most popular aspects of the site, we never had a system in place that would allow us to easily provide a means for the user to read archived reviews. Obviously, we could have simply organized the reviews much like we did the code articles, but we also wanted a means by which reviews could be searched using criteria such as rating, publisher, author, and title. The solution Erik came up with was both elegant and functional. By combining the powers of ASP (Active Server Pages), XML, and XSL, in a weekend he wrote the foundations for the book review archive section that is still in use today at CodeGuru, as well as many other popular Web sites. His application design was so flexible that his work was later expanded to work with archived newsletters and many other document types. Okay, so we know that Erik is great with XML, but will reading this book make you as productive as he is? I'll admit that when I was approached about writing this foreword, I was a bit wary that any reasonable amount of XML could be learned in a single weekend. I told Erik that I would need to read the entire book to make sure my name would be associated with something that I believe in. Well, two days later, not only was I surprised that the book does indeed deliver on its promises, but I actually learned several new bits of information about XML despite having used it for over two years now! If you're new to XML and have no time to waste on theoretical discussions, this book is a goldmine of information. By the end of Saturday afternoon's lesson, many XML documents that you may have seen but never quite understood will begin to make sense. By the end of Saturday afternoon's lesson, you will understand basic XML constructs such as elements and attributes, you will have worked with XML namespaces and fully comprehend how to use them properly, and you'll understand how XML fits into practical applications. By Sunday evening, you will have done everything from working with document models and DTDs, to creating and interfacing your own XML documents with style sheets (both CSS and XSL), to programmatically accessing XML documents from your applications using the XML DOM. The key is that Erik takes a pragmatic approach, helping you become productive quickly while taking the time to explain important details along the way. I found the discussions on character sets, character encoding, and schemas particularly interesting because they were so detailed, yet so easy to read and understand. That's unique in books like this. Erik enjoys teaching others, and his experience shines though on every page. The numerous sample XML documents throughout the book make it an interesting read, but Erik goes beyond that and includes code for Web pages and applications using programming languages like VBScript, JavaScript, and C#. Also, the samples are interesting even if you're not a programmer, because they provide you with another perspective on how developers work with XML. Simply put, the clear explanations, real-world examples, and a focus on relevant technologies make this book an essential addition to your bookshelf if you're serious about XML. Tom Archer http://www.theCodeChannel.com July 2002 Introduction Welcome to Learn XML In a Weekend. This book contains seven lessons and other resources that are focused on only one thing: getting you up to speed with XML, its related technologies, and its latest developments. The lessons span a weekend, beginning on Friday evening and ending on Sunday evening. Yes, you can learn XML in a weekend! As you look at all of the other XML books that line the shelves, you might ask, "What's so special about this book?" This book is different from the rest of the pack because not only does it explain what XML is and how to use it, but it presents relevant, practical, and real-world uses of XML. While a lot of books focus on core XML (its syntax, DTDs, and so on), which is very useful information, they often assume that you have the expertise to integrate XML into your organization's operations. This book focuses on relevant XML technologies like XPath, XSD, DTD, and CSS, and explains why other technologies, like XDR, may not be important in certain scenarios. This book also takes a practical approach to working with XML. After showing you the core syntax and other rules, I'll show you how to work with XML using two of the best XML editors on the market today: eXcelon's Stylus Studio and Altova's XML Spy. There's not much point in writing XML documents, schemas, and transformations by hand if XML editors can generate a lot of the XML for you! I'll also discuss how to use XML in Internet Explorer, Microsoft Active Server Pages, and Microsoft's latest offerings: the .NET Framework and the Visual C# .NET programming language. This book succinctly describes XML and its related technologies, focusing only on what's relevant in today's rapidly changing marketplace. I'll help you make choices that can mean the difference between a successful solution and one that fails because it uses irrelevant, incompatible, or outdated standards. Skim through the book now and take a look through Saturday afternoon's lesson, which describes how to create XML documents. That single lesson covers everything you need to know, from basic syntax to creating XML documents using different languages (important in today's global marketplace). By the end of that lesson alone, you'll already understand terms like entity reference, character sets, and namespaces. How This Book Is Organized This book is organized into seven lessons that span a weekend, beginning on Friday evening and ending on Sunday evening. By Monday morning, you'll be right up to speed with XML and its related technologies. If you're like me and cannot devote an entire weekend to reading a book because of other commitments, feel free to read this book whenever you like. Here's an overview of each lesson: Friday Evening focuses on introducing XML: what it is, why it's useful, and how people use it. Saturday Morning is a slightly longer lesson that focuses on using XML in Internet Explorer with HTML and XSL, and using XML with Microsoft's Active Server Pages. This lesson gives you an overview of what you can do with XML. Don't worry if you're not a programmer or don't understand the programming language that's used in the lesson. The idea is to expose you to these technologies so that you'll gain a better understanding of how others use XML. Saturday Afternoon is a slightly longer lesson, focusing on how to write XML documents by following the rules that XML imposes. This lesson covers basic document structure, working with attributes, comments, and CDATA sections. The lesson also covers character encoding, which allows international users to read your XML documents, and namespaces, a feature that makes your XML documents more useful by allowing you to share them with others. Saturday Evening is one of the longest lessons in the book, focusing on document modeling using DTD and XSD. I suggest that you start reading this chapter as soon as you can after you complete Saturday afternoon's lesson so that you can complete it in one evening. Sunday Morning focuses on using XML Spy and Stylus Studio to create and work with XML solutions. The lesson also covers XSL debugging using Stylus Studio, which can save you hours of frustration when your XSL code doesn't work as you expect it should. This lesson also describes Microsoft XML Core Services, how to determine what version is installed on your system, and how to get the latest updates. Sunday Afternoon is a longer lesson, so I recommend you try to start it as soon as possible after completing the previous lesson. This lesson focuses on presenting data on the Web using presentation technologies like CSS and XSL. It examines how to repurpose an XML document using XSL that you create using Stylus Studio's graphical XSL editor. Sunday Evening shows you how to use XML with Internet Explorer's Data Source Object (DSO), the XML Document Object Model (XML DOM), and Microsoft's .NET Framework. The DSO produces impressive results, like support for paging through long sets of data without any programming. The XML DOM is useful for creating and manipulating an XML document programmatically (via an application's code), and the Microsoft .NET Framework offers support for XML throughout. Appendix A provides an HTML and XPath reference to help you become more productive. This appendix includes examples and screen shots. Appendix B presents the W3C XML 1.0 Specification. This is a shorter specification than the one published by the W3C and uses examples throughout. Appendix C is a list of Web resources. The Glossary is a comprehensive listing of terms, along with their definitions. Most terms are used in the book, but there are some additional terms that you'll come across as you work with XML but do not appear in the book. Conventions Used in This Book This book uses a number of conventions that make it easier to read: Note Notes provide additional information. Tip Tips highlight information that appears in the surrounding text. Code that appears within the body of a paragraph is shown in another font to make it stand out from the rest of the surrounding text. Code listings appear in another font, sometimes including bold lines to highlight certain parts of the listing. The following is an example of a listing that contains bold text: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="license_t"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="licenseNumber" type="xs:string"/> <xs:attribute name="ownerName" type="xs:string"/> References The following is a list of materials I used to prepare this book: W3C, Extensible Markup Language (XML) 1.0 (Second Edition), World Wide Web Consortium, 2000, http://www.w3.org/TR/REC-xml W3C, XML Path Language (XPath) Version 1.0, World Wide Web Consortium, 1999, http://www.w3.org/TR/xpath W3C, XSL Transformations (XSLT) Version 1.0, World Wide Web Consortium, 1999, http://www.w3.org/TR/xslt W3C, Cascading Style Sheets, level 1, World Wide Web Consortium, 1996, http://www.w3.org/TR/REC-CSS1 Nikola Ozu et al, Professional XML, Wrox Press, 2001 Khun Yee Fung, XSLT: Working with XML and HTML, Addison Wesley, 2000 The Unicode Consortium, UNICODE STANDARD VERSION 3.0, 2000 Friday Evening: Introducing XML Good evening! Tonight you begin learning how people use XML in real-world scenarios. This evening introduces you to what XML is, how to create XML documents and play by XML's rules, the benefits of using XML, and how XML relates to HTML. The remainder of the evening discusses the typical life cycle of an XML document, describes how others make XML work for them, and covers the basics of the types of XML documents you'll probably encounter. What Is XML? XML stands for extensible markup language, a syntax that describes how to add structure to data. A markup language is a specification that adds new information to existing information while keeping the two sets of information separate. If it were as simple as that, I could describe XML to you in just a few pages. However, XML is more complicated than that. It's a simple syntax that describes information, a set of technologies that allows you to format and filter information independently of how that information is represented, and the embodiment of an idea that reduces data to its purest form, devoid of formatting and other irrelevant aspects, to attain a very high level of usefulness and flexibility. Oddly enough, XML is not a markup language. Instead, it defines a set of rules for creating markup languages. There are many types of markup languages, the most popular of which is HTML (Hypertext Markup Language), the publishing language of the Internet. HTML combines formatting information with a Web page's content so that you see the page in the way the designer intended for you to see it. The two most important elements that make HTML work are the HTML itself and software that's capable of interpreting HTML. When you view a Web page, your browser retrieves the page, interprets the HTML, and displays the resulting document on your screen. The same two elements, XML itself and software that's capable of interpreting XML, are needed with XML. Assume that you're working with a file that looks like this: Learn XML In A Weekend, Erik Westermann, 159200010X This file describes information about a book using three fields: the title, author, and ISBN (a number that uniquely identifies a book). While it's clear to you and me that Learn XML In A Weekend represents the title of a book, a computer would have a tough time figuring out that • There are three fields in the file (separated by commas). • Each field represents an individual piece of data. XML enables you to add structure to the data. Here's the same file marked up with XML: <books> <book> <title>Learn XML In A Weekend</title> <author>Erik Westermann</author> <isbn>159200010X</isbn> </book> </books> It's now apparent, both to us and to software that's capable of interpreting XML, that the file contains information about a collection of books (there's only one book in this collection) broken into three fields: title, author, and ISBN. For software to be able to interpret the XML, the sample follows certain rules: • Text inside the angle brackets (< and >) represents a markup element. • Text outside of the angle brackets is data. • The beginning of a unit of data has a start tag prefix. • The end of a unit of data is marked with an end tag. This is almost identical to a start tag, except that it begins with a slash (/). For example, <title> is a start tag, Learn XML In A Weekend represents a unit of data, and </title> is an end tag. XML defines only the syntax—the rules—and leaves it to you to decide how you structure it and what data you store in it. XML documents reside in files that you can create with an editor like Windows Notepad, making XML very accessible. Specialized editors are available to help you manage XML documents and ensure that you follow the rules of the XML specification. I'll cover two such editors later in this book. Note Windows Notepad is a simple text editor that comes with Windows. You can start Notepad by clicking Start, Run, and then typing notepad. It is important to understand that XML is an enabling technology, which is analogous to any written or spoken language. A language doesn't communicate for us. We're able to communicate because we use language. Just as you play a role in reading the words on this page (the words are meaningless, unless someone reads them), XML becomes useful only in the context of a system that's able to interpret it. Unlike written and spoken languages, you're not likely to directly read or write XML. People rarely read XML documents—in most cases, software creates an XML file and then other software uses it without anyone actually viewing the XML document itself. However, you still need to understand what XML is and how to use it to your advantage. There are three important characteristics of XML that make it useful in a variety of systems and solutions: • XML is extensible. • XML separates data from presentation. • XML is a widely accepted public standard. XML Is Extensible Think of XML like this: one syntax, many languages. XML describes the basic syntax—the basic format—and rules that XML documents must follow. Unlike markup languages like HTML, which has a predefined set of tags (items with the angle brackets, as in the previous sample), XML doesn't put any limitations on which tags you can use or create. For example, there isn't any reason you couldn't rename the <book> tag to <manuscript> or <record>. XML essentially allows you to create your own language, or vocabulary, that suits your application. The XML standard (described shortly) describes how to create tags and structure an XML document, creating a framework. As long as you stay within the framework, you're free to define tags that suit your data or application. XML Separates Data from Presentation Take a close look at the page layout of this book—it contains several types of headings and other formatting elements. The information on this page wouldn't change if you changed its format, though. If you remove the headings, italic characters, and other formatting, you'll be left with the essence of this book—the information that it contains, or its content. XML allows you to store content with regard to how it will be presented—whether in print, on a computer screen, on a cellular phone's tiny display screen, or even read aloud by speech software. When you want to present an XML document, you'll often use another XML vocabulary (set of XML tags) to describe the presentation. Also, you'll use other software to perform the transformation from XML into the format you want to present the content in, as shown in Figure 1.1.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.