XML Programming Using the Microsoft XML Parser SOD MEE FOO AND WEI MENG LEE APress Media, LLC XML Programming Using the Microsoft XML Parser Copyright ©2002 by Soo Mee Foo and Wei Meng Lee Originally published by Apress in 2002 All rights reserved. No part of this work 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 the prior written permission of the copy right owner and the publisher. ISBN 978-1-893115-42-2 ISBN 978-1-4302-0829-7 (eBook) DOI 10.1007/978-1-4302-0829-7 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Editorial Directors: Dan Appleman, Peter Blackburn, Gary Cornell, Jason Gilmore, Karen Watterson Technical Reviewer: Ronald Landers Managing Editor: Grace Wong Project Managers: Alexa Stuart, Erin Mulligan Development Editor: Kenyon Brown Copy Editors: Kit Cooley Production Editor: Kari Brooks Compositor: Impressions Book and Journal Services, Inc. Artist: Kurt Krames Indexer: Rebecca Plunkett Cover Designer: Tom Debolski Marketing Manager: Stephanie Rodriguez The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.comin the Downloads section. You will need to answer questions pertaining to this book in order to successfully download the code. Contents at a Glance About the Authors .................................................... .x i About the Technical Reviewer ...................................... .x i Acknowledgments ..................................................... .x iii Introduction ........................................................ .x iv Chapter 1: Introduction to the Extensible Markup Language (XML) .................................. 1 Chapter 2: Addressing XML Parts Using XPath ................... .41 Chapter 3: The Extensible Stylesheet Language Transformation (XSL T) ................................. 69 Chapter 4: The Document Object Model (DOM) ................... .1 07 Chapter 5: Support of XML in ActiveX Data Objects (ADO) 2.1 and Above .........................1 51 Chapter 6: XML Support in SQL Server 2000 ..................... 197 Chapter 7: Simple API for XML (SAX) .............................2 69 Chapter 8: XML Schemas ........................................... .291 Chapter 9: The Wireless Markup Language (WML) ................. 319 Chapter 10: Simple Object Access Protocol (SOAP) and Web Services .....................................3 47 Appendix A: Installing Microsoft XML Tools .................... .393 Appendix B: Document Object Model (DOM) Level 1 Core Interfaces ............................3 99 Appendix C: XML-Data Reduced (XDR) Schema ..................... .417 Index .................................................................4 29 iii Contents About the Authors .....................................................x i About the Technical Reviewer .......................................x i Acknowledgments ......................................................x iii Introduction .........................................................x iv Chapter 1: Introduction to the Extensible Markup Language (XML) ........................... l W3( and XML ...........................................................2 Basic XML Terminology ...............................................6 Rules of XML ..........................................................6 Document Type Definition (DTD) ...................................1 1 (DATA and P(DATA ....................................................1 8 Elements or Attributes .............................................1 9 XML Schemas ..........................................................2 2 XML Encoding and Unicode ...........................................2 3 Namespaces ...........................................................2 7 XML Parsers ..........................................................2 9 Microsoft XML Parser (MSXML) ......................................3 1 Uses and Benefits of XML ...........................................3 1 XML and Web Browser Support .......................................3 2 Using the Microsoft XML Parser Release 3.0 SPl .................3 6 XML Editors ..........................................................3 9 Useful Web Links ....................................................3 9 Summary ...............................................................4 0 Chapter 2: Addressing XML Parts Using XPath ........ .41 XPath Expression ................................................... .42 XPath Data Model ................................................... .44 Location Path ........................................................5 0 Functions ............................................................5 8 Abbreviated Syntax ..................................................6 5 Useful Web Links ....................................................6 7 Summary ...............................................................6 7 v Contents Chapter 3: The Extensible Stylesheet Language Transformation (XSL T) ........... 69 Motivation for XSLT .............................................. .. 70 Support of XS LT in MSXML3 ......................................... 72 A First Look at XSL T ................................................ 73 XSLT Fundamentals .................................................... 79 Client-Side versus Server-Side XSL T Transformation .......... .1 02 Useful Web Links .................................................. .1 05 Summary ............................................................. .1 05 Chapter 4: The Document Object Model (DOM) ........ .. 107 Introduction to DOM .............................................. .1 07 XML DOM Parsers ................................................... .1 08 Support of DOM in MSXML3 ........................................ .1 08 Representing XML Document as a Tree ............................ 111 Creating a DOM Tree of an XML Document in Memory ............. 114 Saving a DOM Tree .................................................. 118 Fundamental APIs for Processing a DOM Tree .................... 119 Client-Side DOM Programming-Shopping Cart ..................... 131 Server-Side DOM Programming-Shopping Cart ..................... 145 Useful Web Links ................................................... 148 Summary .............................................................. 149 Chapter 5: Support of XML in ActiveX Data Objects (ADO) 2.1 and Above .............. 151 Universal Data Access ............................................... 151 The ADO Object Model ............................................... 154 Connection Object .................................................. 155 Recordset Object .................................................... 161 Command Object .................................................... .1 70 Record Object ....................................................... 175 Stream Object ....................................................... 175 Error Handling ..................................................... 179 Case Study: Updating Book Prices Using Three-Tier Architecture ................................ 183 Useful Web Links .................................................... 195 Summary .............................................................. 195 vi Contents Chapter 6: XML Support in SQL Server 2000 .......... 197 Additional Software Installation and Configuration ........... 197 Retrieving and Transforming Rowset Data into XML Data .................................................. .2 03 Providing Rowset Data from XML Data Using the OPENXML Provider .......................................... .228 Accessing SQL Server Using HTTP ..................................2 35 Persisting Changes Using Updategram ........................... .257 Useful Web Links ................................................... .268 Summary ..............................................................2 68 Chapter 7: Simple API for XML (SAX) .................. .269 An Alternative to DOM ............................................ .269 SAX in MSXML3 .......................................................2 78 Error Handling in SAX .............................................2 81 Using SAX ...........................................................2 84 When Do You Use SAX? ..............................................2 88 Useful Web Links ................................................... .2 90 Summary ............................................................. .2 90 Chapter 8: XML Schemas ......................................2 91 XML Schema and XDR Schema ........................................2 93 First Look at XML Schema .........................................2 93 XML Schema Data Types ............................................ .3 00 Groupings .......................................................... .3 10 Linking Schemas and Redefining Definitions ....................3 12 Documenting the Schema Using <annotation> ......................3 13 Tools for Validating XML Schemas .................................3 14 Useful Web Links ...................................................3 18 Summary ..............................................................3 18 vii Contents Chapter 9: The Wireless Markup Language (WML) ..... 319 Architecture of WAP ...............................................3 19 Understanding the Wireless Markup Language (WML) ..............3 22 Testing WML Applications ..........................................3 29 Tailoring WAP Content with XML and XSLT ....................... .3 30 Using the Microsoft XSL ISAPI ....................................3 37 Useful Web Links ...................................................3 45 Summary ..............................................................3 45 Chapter 10: Simple Object Access Protocol (SOAP) and Web Services ............................. 347 Introduction to SOAP .............................................. 347 Web Services ....................................................... .3 50 Creating a Web Service ............................................. 357 Consuming a Web Service .......................................... .358 Developing Web Services Using the Microsoft SOAP Toolkit Version 2.0 ..................................................... 358 Providing Web Services Using the SOAP Toolkit: A Case Study ....................................................3 59 Web Services Security .............................................3 88 Useful Web Links .................................................. .3 90 Summary ..............................................................3 91 Appendix A: Installing Microsoft XML Tools ......... 393 Installing IE Tools for Validating XML Documents ..............3 93 Checking the Version of the Installed XML Parser .............3 95 Appendix B: Document Object Model (DOM) Level 1 Core Interfaces ...................3 99 Understanding Basic Terms in the DOM ...........................3 99 Fundamental Interfaces ........................................... .400 Extended Interfaces .............................................. .413 viii Contents c: Appendix XML-Data Reduced (XDR) Schema ......... .417 Basic Elements in XDR Schema .................................... .417 General Layout ..................................................... 418 Specification of the Content Model ............................. .419 Data Types .......................................................... 424 Annotations ......................................................... 427 Index ................................................................4 29 ix About the Authors Wei Meng Lee currently holds a full-time job as a lecturer at the School of Information and Communications Technology, Ngee Ann Polytechnic (Singapore). Besides teaching, he offers consultant services to the industry and conducts in-house training in the areas ofW eb development. He is also a con tributing author to magazines like XML Journal, XML Magazine, and .NET Magazine, and he is the co-author of Beginning WAP, WML and WMLScript (Wrox) and Dynamic WAP Application Development (Manning). A frequent speaker at developer conferences and seminars, Wei Meng spoke at the Wrox Professional Wireless Developer Conference (July 2000, Amsterdam) and Wireless Developer Conference (December 2000, San Jose). Contact Wei Meng at wei _m [email protected]. Soo Mee Foo is currently lecturing at Ngee Ann Polytechnic (Singapore) in Web technologies and security. She also conducts workshops for IT professionals and is involved in designing Web-based courseware and industrial projects. She is a co-author of Beginning WAP, WML and WMLScript and Dynamic WAP Application Development. Soo Mee can be contacted at soomee _f oo@hotmail. com. About the Technical Reviewer Ronald Landers is the owner of Right -Click Consulting, LLC, a Los Angeles-based consulting, development, and training firm. The company specializes in Microsoft -based technologies including Active Server Pages, SQL Server, Visual Basic, Internet Explorer, and XML. Mr. Landers is a faculty member and instructor at UCLA Extension and has been teaching and consulting for the past 12 years. xi