Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development...

30
Professional XML Bill Evjen, Kent Sharkey, Thiru Thangarathinam, Michael Kay, Alessandro Vernet, Sam Ferguson

Transcript of Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development...

Page 1: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

Professional XML

Bill Evjen, Kent Sharkey,

Thiru Thangarathinam, Michael Kay,

Alessandro Vernet, Sam Ferguson

01_777779 ffirs.qxp 3/1/07 11:41 PM Page i

Page 2: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in
Page 3: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

Professional XML

Bill Evjen, Kent Sharkey,

Thiru Thangarathinam, Michael Kay,

Alessandro Vernet, Sam Ferguson

01_777779 ffirs.qxp 3/1/07 11:41 PM Page i

Page 4: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

Professional XMLPublished byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256www.wiley.com

Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana

Published simultaneously in Canada

ISBN: 978-0-471-77777-9

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

Library of Congress Cataloging-in-Publication Data:

Professional XML / Bill Evjen . . . [et al.].p. cm.

Includes index.ISBN-13: 978-0-471-77777-9 (paper/website)ISBN-10: 0-471-77777-3 (paper/website)

1. XML (Document markup language) I. Evjen, Bill.QA76.76.H94P7638 2007006.7'4—dc22

2007006214

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by anymeans, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, orauthorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 RosewoodDrive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should beaddressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317)572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions.

LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP-RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON-TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUTLIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE-ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON-TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THEUNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OROTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF ACOMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THEAUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATIONOR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR-THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR-MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE.FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVECHANGED OR DISAPPEARED BETWEEN THEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.

For general information on our other products and services please contact our Customer Care Department withinthe United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress aretrademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and othercountries, and may not be used without written permission. All other trademarks are the property of their respec-tive owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not beavailable in electronic books.

01_777779 ffirs.qxp 3/1/07 11:41 PM Page ii

Page 5: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

CreditsSenior Acquisitions EditorJim Minatel

Development EditorSydney Jones

Technical EditorsAlexei GorkovSteve DanielsonCody Reichenau

Production EditorWilliam A. Barton

Copy EditorsMary LaguKathryn Duggan

Editorial ManagerMary Beth Wakefield

Vice President & Executive Group PublisherRichard Swadley

Vice President and PublisherJoseph B. Wikert

Project CoordinatorErin Smith

Graphics and Production SpecialistsJonelle BurnsCarrie A. FosterBrooke GraczykDenny HagerJennifer MayberryBarbara MooreRashell SmithAlicia B. South

Quality Control TechniciansLaura AlbertChristy Pingleton

Proofreading and IndexingAptara

Anniversary Logo DesignRichard J. Pacifico

01_777779 ffirs.qxp 3/1/07 11:41 PM Page iii

Page 6: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

To my three little ones—Sofia, Henri, and Kalle.—Bill Evjen

To Babi, for keeping me alive, and putting up with me. Hopefully for a longtime to come.—Kent Sharkey

Thanks to my beautiful wife Jacquie for her patience, my cat Peggy for hercompany (and technical input), and to all my colleagues at API who

continue to inspire and challenge me.—Sam Ferguson

01_777779 ffirs.qxp 3/1/07 11:41 PM Page iv

Page 7: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

About the Authors

Lead AuthorsBill Evjen is an active proponent of .NET technologies and community-based learning initiatives for.NET. He has been actively involved with .NET since the first bits were released in 2000. In the sameyear, Bill founded the St. Louis .NET User Group (www.stlnet.org), one of the world’s first suchgroups. Bill is also the founder and former executive director of the International .NET Association(www.ineta.org), which represents more than 450,000 members worldwide.

Based in St. Louis, Missouri, USA, Bill is an acclaimed author (more than 13 books to date) and speakeron ASP.NET and XML Web services. He has written or co-written Professional C# 2005; Professional VB2005; and the best-selling Professional ASP.NET 2.0, as well as ASP.NET Professional Secrets, XML WebServices for ASP.NET, Web Services Enhancements: Understanding the WSE for Enterprise Applications, VisualBasic .NET Bible, and more. In addition to writing, Bill is a speaker at numerous conferences, includingDevConnections, VSLive, and TechEd. Along with these items, Bill works closely with Microsoft as aMicrosoft Regional Director and he has received the Microsoft MVP designation for many years.

Bill is the Technical Architect for Lipper (www.lipperweb.com), a wholly-owned subsidiary of Reuters,the international news and financial services company. He graduated from Western WashingtonUniversity in Bellingham, Washington, with a Russian language degree. When he isn’t tinkering on thecomputer, he can usually be found at his summer house in Toivakka, Finland. You can reach Bill [email protected]. He presently keeps his weblog at www.geekswithblogs.net/evjen.

Kent Sharkey is an independent consultant who lives and codes in the midst of the wilds of VancouverIsland. Before going solo, Kent worked at Microsoft as a Technical Evangelist and Content Strategist,promoting the use of .NET technologies. When not coding or writing, he’s off hiking, biking, or canoeing(or exploring the wilds of Azeroth). He shares his house with his wife, Margaret, and two “children,”Squirrel and Cica.

Contributing AuthorsThiru Thangarathinam is a Microsoft MVP who specializes in architecting, designing, and developingdistributed enterprise class applications using .NET-related technologies. He is the author of the booksProfessional ASP.NET 2.0 XML and Professional ASP.NET 2.0 Databases from Wrox press and has coau-thored a number of books on .NET-related technologies. He is a frequent contributor to leading technology-related online publications.

Michael Kay is widely known in the XML world as the developer of the Saxon XSLT and XQuery pro-cessor, and as the editor of the XSLT 2.0 specification. His Wrox books XSLT 2.0 Programmer’s Referenceand XPath 2.0 Programmer’s Reference are regarded as the definitive guides to these languages. Michaelruns his own company, Saxonica, which develops the Saxon technology and provides support and con-sultancy for XSLT and XQuery users. His background is as a software designer creating database prod-ucts for a mainframe manufacturer. He is a Fellow of the British Computer Society and a Visiting Fellowat the University of Reading (UK). In his spare time he sings and plays croquet.

01_777779 ffirs.qxp 3/1/07 11:41 PM Page v

Page 8: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

vi

About the Authors

Alessandro Vernet co-founded Orbeon in 1999, which makes Orbeon Forms, an open source product tobuild and deploy sophisticated forms on the Web. He is one of the authors of Professional Web 2.0Programming and is a member of two W3C Working Groups: the XForms and XML ProcessingModel Working Groups. Before co-founding Orbeon, Alessandro was at Symantec as part of theVisualCafé team, working on their next-generation RAD for web applications. He holds an MS/CS fromthe Swiss Institute of Technology (EPFL) in Lausanne, Switzerland, and since 1998 lives in the incrediblyenergetic Silicon Valley.

Sam Ferguson is a Project Manager with API Software, a Microsoft Gold Certified Partner, based inGlasgow, Scotland. Sam, who lives in Ayrshire, specializes in SQL Server, Microsoft Office Server System2007, .NET, and all XML-related technologies. In what little spare time he has, Sam enjoys playing golfand is an avid fan of the Glasgow Rangers.

01_777779 ffirs.qxp 3/1/07 11:41 PM Page vi

Page 9: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

Acknowledgments

This book, like most, took an entire team to get out the door. First and foremost, I would like to thankJim Minatel for providing me the opportunity to write this book. Big thanks go to Sydney Jones, thebook’s development editor who kept this book together despite numerous delays.

Thanks to my family for putting up with this second job. I love you all greatly.—Bill Evjen

01_777779 ffirs.qxp 3/1/07 11:41 PM Page vii

Page 10: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

01_777779 ffirs.qxp 3/1/07 11:41 PM Page viii

Page 11: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

Contents

Acknowledgments viiIntroduction xxiii

PPaarrtt II:: XXMMLL BBaassiiccss 11

Chapter 1: XML Syntax 3

The Purpose of XML 3XML Syntax and Rules 6

XML Parsers 7XML Elements and Tags 7XML Text 12The XML Document 17Attributes 26

XML Namespaces 29Summary 32

Chapter 2: XML Editors 33

Visual Studio .NET 2003 33Basic Editing in Visual Studio .NET 2003 34Schema Development in Visual Studio .NET 2003 35Other Features 35

Visual Studio 2005 35Basic Editing in Visual Studio 2005 35Schema Development in Visual Studio 2005 38XSLT Development in Visual Studio 2005 38

Altova XMLSpy 2006 39Basic Editing in Altova XMLSpy 2006 40Schema Development in XMLSpy 44XSLT Development in XMLSpy 46Other Features in XMLSpy 46

02_777779 ftoc.qxp 3/1/07 11:41 PM Page ix

Page 12: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

x

Contents

Stylus Studio 2006 47Basic Editing in Stylus Studio 2006 47Schema Development in Stylus Studio 2006 50XSLT Development in Stylus Studio 51Other Features in Stylus Studio 54

Oxygen XML Editor 6.2 55Basic Editing in Oxygen XML Editor 6.2 55Schema Development in Oxygen XML Editor 6.2 57Converting between Schema Types in Oxygen XML Editor 6.2 58XSLT Development in Oxygen XML Editor 6.2 60Other Features in Oxygen XML Editor 6.2 61

Other XML Tools 62Summary 63

PPaarrtt IIII:: PPrreesseennttaattiioonn 6655

Chapter 3: XHTML and CSS 67

Understanding XHTML 67The Evolution of Markup 67The Basics of XHTML 68Validating XHTML 69

Understanding CSS 81Basics of CSS 81CSS Examples 88Validating CSS 94

Using Microformats 94Elemental Microformats 95Compound Microformats 96

Summary 99Resources 100

Chapter 4: XSL-FO 101

The Composition of XSL 102XSL-FO Overview 103

Page Templates 105fo:simple-page-master 105

XSL-FO Basics 108Hello World for XSL-FO 109Basic Formatting 111A Working Example 117

Summary 131

02_777779 ftoc.qxp 3/1/07 11:41 PM Page x

Page 13: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xi

Contents

PPaarrtt IIIIII:: DDeeffiinniinngg SSttrruuccttuurree 113333

Chapter 5: Document Type Definitions (DTDs) 135

Why Document Type Definitions? 135Internal DTDs 138External DTDs 140Building Your Own DTD 142

Document Type Declaration 144Element Declarations 147Attribute Declarations 158Entity Declarations 163Notation Declarations 165

Using XML Tools to Create the DTD 166DTD Validation 166Summary 167

Chapter 6: XML Schemas 169

The Issues with DTDs 169Building the Root XML Schema Document 171

The XML Declaration 172The Root Element 172

Declaring Elements 176Simple Types 176Complex Types 180Element Types 184Groups and Choices 189Element Restrictions 192Defining Attributes 198

Putting XML Schema Document Together 203<import> 203<include> 204

Commenting XML Schemas 205Standard XML Comments 205<annotation > 206

XML Schema Tools 207Microsoft’s Visual Studio 2005 207Altova’s XMLSpy 209

Summary 210

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xi

Page 14: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xii

Contents

Chapter 7: RELAX NG 211

Why Another Schema Language? 211Defining a RELAX NG Schema 212Declaring Elements 214

Simple Elements 214Attributes 219Order of Elements 220Defining Grammar 223Reusing Types 224Merging Schemas 226Namespaces and Name Classes 229Annotating Schemas 231

RELAX NG Tools 232Oxygen 232Trang 233

RELAX NG Compact 236Summary 238Resources 238

PPaarrtt IIVV:: XXMMLL aass DDaattaa 223399

Chapter 8: XSLT 241

What Is XSLT? 241XSLT Syntax 242

Required Items 242Templates 247Retrieving Values 248Conditional Processing 251Looping 252Variables and Parameters 255Other Functions and Expressions 256Extending XSLT 257

Executing XSLT 258Executing XSLT at the Command-line 258Executing XSLT via Code 259Executing XSLT in a Browser 262

Changes with XSLT 2.0 263Generating Output with XSLT 268

Generating HTML with XSLT 268Converting between XML Syntaxes with XSLT 272

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xii

Page 15: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xiii

Contents

Debugging XSLT 274Summary 276Resources 276

Chapter 9: XPath 277

Major Features of XPath 277Nodes 278Tree Structure 278Path Expressions 279Predicates 279Axes 281Sequences 282

Lessons from the Trenches 282When A != B Is Different from not(A = B) 282The Many Faces of a Document 283Tuning Your XPath Expressions 284Function Calls in Path Expressions 285Using Comments and Nested Comments 286Using Regular Expressions 287The unordered() Function: Quite an Oddity 288Union and Sequence Operators 289//h1[1] Different Than (//h1)[1] 289Reverse Axis — Evil at Times 291Debugging with trace() 292

XPath in Java, .NET, and PHP 293XPath in Java 293XPath on .NET 295XPath in PHP 296

Tools for XPath 296Online XPath Sandbox 296XPath in Your Browser 297XML Editors 299Eclipse and IntelliJ 300

Summary 300References 301

Chapter 10: XQuery 303

What Is XQuery? 303XQuery Use Cases 304Advantages of XQuery 304Structure of an XQuery Expression 304

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xiii

Page 16: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xiv

Contents

A Simple XQuery Example 305Enclosed Expressions 306FLWOR Expressions 306

A Simple FLWOR Expression 307An In-Depth Look at FLWOR Expressions 308FLWOR Expressions Versus XPath Expressions 310

XQuery Functions 311XQuery Built-In Functions 311XQuery User-Defined Functions 313

XQuery in Java 314Pre-requisites 314Selecting XML with XQuery 314

XQuery in Relational Databases 318XQuery in SQL Server 2005 318XML Data Type Query and Data Modification 319

Summary 322

Chapter 11: XML in the Data Tier 323

XML and Databases 323Retrieving Data as XML 323Storing XML 324

Relational Databases 325Microsoft SQL Server 2005 325Oracle 10g 340

XML Databases 345Xindice 345

Other Databases 349Summary 349Resources 350

PPaarrtt VV:: PPrrooggrraammmmiinngg XXMMLL 335511

Chapter 12: XML Document Object Model (DOM) 353

What Is DOM? 353Why Client-Side XML Processing? 354XML DOM Object Model 354Using the Document Interface 357Loading an XML Document 358Using the Element Interface 360Creating a New Element 361

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xiv

Page 17: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xv

Contents

Using the Node Interface 361Using the NodeList Interface 365Using the NamedNodeMap Interface 366Using the Attr Interface 366Creating Attributes 367Using the CharacterData Interface 368Using the Comment Interface 369Using the Text Interface 369Using the CDATASection Interface 369Handling Errors in XML DOM 370XML Transformation Using XSL 371

XML Validation Using XML DOM 373Summary 376

Chapter 13: Simple API for XML (SAX) 377

Introducing XML Parsing 377Tree-Based APIs 378A Simple API for XML (SAX) 378Installing SAX 378

SAX Architecture 380Basic SAX Application Flow 380

SAX Packages and Classes 381The SAXParser Class 381The XMLReader Interface 382Receiving SAX Events 382Using the XMLReader Interface 384DefaultHandler Class 385Handling Errors and Warnings 392

Searching in an XML File 393Writing XML Contents Using SAX 396XML Validation Using SAX 399Advantages and Disadvantages of SAX 403Summary 403

Chapter 14: Ajax 405

Adding JavaScript to a Web Page 406Data Types in JavaScript 407

XMLHttpRequest 410The DOM 415

Objects in the DOM 416Events in the DOM 417

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xv

Page 18: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xvi

Contents

Putting It All Together 418Ajax Libraries 427

Using the Microsoft AJAX Library to Add Ajax Functionality 428Using Prototype to Add Ajax Functionality 432

Ajax Resources 434Summary 434

Chapter 15: XML and .NET 435

The Serialization of XML 435Serializing Using the XmlSerializer Class 436Changing the Output of the Serialized Object 438

Deserializing XML 444XmlWriter 447

Writing XML Using XmlTextWriter 447Writing XML Using XmlWriter 450Writing XML Programmatically Using XmlWriter 452

XmlReader 454Reading XML with a Schema Validation 457Casting XML Types to .NET-Compliant Types 459

Reading XML Using XPathDocument 460XML in ASP.NET 2.0 461

The XmlDataSource Server Control 462The XmlDataSource Control’s Namespace Problem 466The Xml Server Control 467

Summary 469

Chapter 16: XML and Java 471

Reading and Writing XML 472Parsing from Java 472Writing XML (Serialization) 477

XML Tree Models 480Alternatives to DOM 481Java/XML Data Binding 486Controlling XSLT, XQuery, and XPath Processing from Java 491

Summary 500

Chapter 17: Dynamic Languages and XML 501

Perl 502Reading and Writing XML 502Support for Other XML Formats 509

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xvi

Page 19: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xvii

Contents

Python 509Reading and Writing XML 510Support for Other XML Formats 516

Ruby 516Reading and Writing XML 517Support for Other XML Formats 526

Summary 526Resources 527

Perl Resources 527Python Resources 527Ruby Resources 527

PPaarrtt VVII:: XXMMLL SSeerrvviicceess 552299

Chapter 18: RSS and Atom 531

What Is RSS? 532Specifications 535

RSS 2.0 535RSS 1.0 540

What Is Atom? 543Reading RSS and Atom 548

Reading with .NET 550Reading RSS with Java 556

Writing RSS and Atom 559Writing with .NET 559Writing with Java 560Class Libraries Available for Processing RSS and Atom 562

Summary 563Resources 563

Chapter 19: Web Services 565

Why Web Services? 565The Composition of Web Services 567

Representing and Communicating Data in Web Services 568Describing Web Services 568Discovering Web Services 570

Building Web Services with C# 571Using the Microsoft Web Services Test Page 575Testing the WebMethod 576Altering the Protocols Used by the Web Service 578

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xvii

Page 20: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xviii

Contents

Building Web Services with Java 579Introduction to Axis and Tomcat 580Setting Up Axis and Tomcat 580Publishing Web Services Using Axis 582

Consuming Web Services with C# 587Consuming Web Services Using ASP.NET 587Consuming Web Services Using Windows Forms 591

Consuming Web Services with Java 594Consuming the HelloWorld Service 594Running the Client Application 596Consuming the ProductService 597Running the Client Application 599

Caching Web Services 599Asynchronous Consumption of Web Services 601

Building a Slow Web Service 602Consuming the TakeLongTime() WebMethod Asynchronously 603

Summary 605

Chapter 20: SOAP and WSDL 607

SOAP Speak 607The Basics of SOAP 608

Remember: SOAP Is XML! 609Transport Protocols for SOAP 609

Looking Closely at the SOAP Specification 610The SOAP Message 610The SOAP Envelope 611The SOAP Body 612The SOAP Header 614SOAP 1.1 Faults 617SOAP 1.2 Faults 619SOAP Encoding of Data Types 621

Tracing SOAP Messages 621The Microsoft Trace Utility 622XMLSpy’s SOAP Debugging 624

Working with SOAP Headers 629Creating SOAP Messages Using SOAP Headers 630Consuming SOAP Messages Using SOAP Headers 631

Defining Web Services Using WSDL 633The Structure of WSDL Documents 637<definitions> 638

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xviii

Page 21: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xix

Contents

<types> 640<message> 641<portType> 642<binding> 643<service> 645<import> 645<documentation> 647

Summary 648

Chapter 21: Advanced Web Services 649

Expanding on a Foundation 649Web Services Framework — The Paper 650

Message Envelope and Controlled Extensibility 650Binary Attachments 650Message Exchange aka Routing 650Message Correlation 651Guaranteed Message Exchange 651Digital Signature 651Encryption 651Transactions and Activities 651Service Description 651Process Flow Contract Description 651Inspection 651Discovery 652

WS-I.org 652Extending XML Web Services 653

SOAP Basics 653SOAP Headers 655SOAP Extensions 655

WS-* Specifications 656WS-Security 657WS-Addressing 657WS-Attachments 658WS-Coordination 658WS-MetadataExchange 658Core Specifications 659

Looking at Microsoft’s Web Services Enhancements 3.0 663The WSE 1.0 Contents 664The WSE 2.0 Contents 664The WSE 3.0 Contents 664

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xix

Page 22: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xx

Contents

Functionality Provided by the WSE 664How the WSE Works 665Building a WSE 3.0 Example — The Server 666Building a WSE 3.0 Example — The Client Application 676The Result of the Exchange 679

Summary 681

Chapter 22: REST 683

Introducing the Basics of REST 683Pure REST 683Just-enough REST 684

Accessing REST Services 685Accessing REST Service Examples 685

Creating REST Services 700Just-enough REST Service Example 700A Pure REST Service Example 713

Summary 717Resources 718

PPaarrtt VVIIII:: AAppppllyyiinngg XXMMLL 771199

Chapter 23: XML Form Development 721

Creating Forms 721XForms Model 722Binding Instance Data 737XForms Submit Protocol 741

XForms Logic 743Events 743Actions 744

XForms Sample 745Alternatives to XForms 750

Microsoft InfoPath 750Comparing XForms and InfoPath 754

Summary 755Resources 755

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xx

Page 23: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xxi

Contents

Chapter 24: The Resource Description Framework (RDF) 757

The Core Structure of RDF 757The RDF Graph Model 760Using Altova’s SemanticWorks 762The RDF XML Schema 769Summary 771

Chapter 25: XML in Office Development 773

Using XML with Microsoft Excel 773Using XML with Microsoft Word 782Using XML in Other Office Applications 792

Microsoft Access 792Microsoft InfoPath 798

Office 2007 — Open XML Format 799OpenOffice — The Open Document Format 802Summary 805Resources 806

Chapter 26: XAML 807

Thin or Thick? 808One More Application Style — Windows Presentation Foundation 809

WPF Within Visual Studio 2005 812Nesting Controls 812Case Study: Building a Document Viewer Using XAML 815

Summary 829

Index 831

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xxi

Page 24: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

02_777779 ftoc.qxp 3/1/07 11:41 PM Page xxii

Page 25: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

II nn tt rr oodduucc tt ii oonn

As many people predicted, XML has changed the world! When XML was introduced, many consideredit a revolutionary leap forward in how we look at data representation. Since then, XML has grown con-siderably and many new technologies have been introduced. The number of new technologies basedupon XML is staggering. From Web services, to blogging, to alerts and notifications—there is so muchcoming out today completely based upon this technology.

This book covers not just the basics of XML and the XML specification, but it also takes a look at thetechnologies based on XML that are driving the tech industry forward. This book not only introducesthese technologies to you, but it also shows you examples of these new technologies in action. So sitback, pull up that keyboard, and let’s have some fun!

What You Need for This BookThis book is vendor agnostic as XML can be utilized on any major operating system out there. But thisbook is also about various technologies that are based upon XML, and for this reason, you will some-times need specific vendor tools to complete the examples provided in the chapters. In these cases, thelocations of the tools or technologies and where you can acquire them are called out in the chapter.

You will find that this book focuses on both Microsoft- and Java-based technologies, and therefore, youare sometimes asked to work with that vendor’s specific development tools and environments to com-plete the examples.

Who Should Read This Book?This book was written to provide you with the latest and greatest information on XML, and to look atthe new technologies and capabilities being built on XML today. We assume you have a general under-standing of programming technologies, such as C# or Java. If you understand the basics of these pro-gramming languages, then you shouldn’t have much trouble following along with this book’s content.

If you are brand new to XML, be sure to check out Beginning XML by David Hunter and others (pub-lished by Wrox; ISBN: 978-0-7645-7077-3) to help you understand the basics.

You may also be wondering whether this book is focused on the Microsoft developer or the Java devel-oper. We’re happy to say that it’s for both! You will find that there are chapters focused on each of theseprogramming technologies.

03_777779 flast.qxp 3/1/07 11:42 PM Page xxiii

Page 26: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xxiv

Introduction

What This Book CoversThis book spends its time reviewing the big changes that have occurred in the 2.0 release of ASP.NET.Each major new feature included in ASP.NET 2.0 is covered in detail. The following list tells you some-thing about the content of each chapter.

❑ Chapter 1, “XML Syntax.” This first chapter gives a good grounding in the XML specification.This chapter looks at the reasoning to include XML and its related technologies within an appli-cation’s architecture. In addition to this introduction, this chapter will also look at the syntacti-cal rules of the XML markup language.

❑ Chapter 2, “XML Editors.” This chapter takes a look at the XML tools that can be used for work-ing with XML and its related technologies. These are also tools used in the chapters throughoutthe book. This chapter introduces you to the various tools out there, where they can be found, aswell as some basics for working with them.

❑ Chapter 3, “XHTML and CSS.” The next set of chapters looks at presenting XML and XML-based presentation technologies. This first chapter focuses on the popular XHTML specificationand how it can be used to present content within a browser. In continuing on with the presenta-tion theme, this chapter looks at how to present XML documents visually in a browser usingCascading Style Sheets.

❑ Chapter 4, “XSL-FO.” This chapter takes a look at presenting XML using an XML-based presen-tation markup language—Extensible Stylesheet Language Formatting Objects (XSL-FO), alsoknown as simply XSL.

❑ Chapter 5, “Document Type Definitions (DTDs).” This chapter takes a look at defining anXML structure using DTDs which have been around for quite awhile. Though it is preferred touse XML Schema today, DTDs should still be understood as developers may encounter legacyXML documents.

❑ Chapter 6, “XML Schemas.” XML Schemas are the latest and most preferred way to define thestructure and data types of an XML document. This is an important topic to understand as it isreferenced throughout the book. This chapter takes a deep look into XML Schemas and how tobuild them.

❑ Chapter 7, “RELAX NG.” RELAX NG is a new XML specification that allows you to validate anXML structure as well as make a link to datatype libraries. This standard was born to simplifywhat was perceived as difficult and complicated about XML Schema. This chapter looks at howto work with RELAX NG.

❑ Chapter 8, “XSLT.” This chapter looks at transforming XML documents in a multitude of waysusing XSLT. Starting with the basics of XSLT, this chapter gives you an understanding of howand when to use this technology.

❑ Chapter 9, “XPath.” XPath allows for the searching and manipulation of particular subsets of anXML document. This chapter takes a look at this popular technology and will focus on the latestrelease of XPath—version 2.0.

❑ Chapter 10, “XQuery.” XQuery is another search and manipulation technology that often com-petes with XPath. This chapter takes a look at XQuery and what makes it different than XPath.By the end of chapters 9 and 10, you should have a good understanding of both XPath andXQuery and when to use which technology.

03_777779 flast.qxp 3/1/07 11:42 PM Page xxiv

Page 27: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xxv

Introduction

❑ Chapter 11, “XML in the Data Tier.” In the continuing look at XML as data, this chapter focuseson the use of XML in the data tier. With focuses on Microsoft’s SQL Server 200 and SQL Server2005, as well as Oracle, MySQL, and more, this chapter shows what XML capabilities there arefor working with the various data storage technologies.

❑ Chapter 12, “XML Document Object Model (DOM).” This chapter will take a look at usingthe XML-DOM to program your XML documents. Included in this chapter is an introductionto the XML Document Object Model as well as information about how to parse XML using theXML-DOM.

❑ Chapter 13, “Simple API for XML (SAX).” Another method to use to program your XML docu-ments is through the use of SAX. By the end of this chapter, you should understand the differ-ences between working with the XML-DOM and SAX and when to use which technology.

❑ Chapter 14, “Ajax.” One of the more talked about programming technologies of 2005 and 2006has been programming using XMLHTTP, also known as Ajax. Google has made this program-ming style popular through its use in various Google applications; the use of this asynchronousJavaScript and XML programming technique is now a sought after feature. This chapter takes alook at XMLHTTP and how to use this object to build truly unique applications.

❑ Chapter 15, “XML and .NET.” This chapter takes a look at using the System.Xml namespaceand other XML capabilities that are provided with Microsoft’s .NET Framework. Also covered isXML in the Microsoft development space and reading and writing XML using the classes pro-vided via the .NET Framework 2.0.

❑ Chapter 16, “XML and Java.” This chapter takes a look at using XML with the Java languageand what XML documents can do within a Java application environment.

❑ Chapter 17, “Dynamic Languages and XML.” This chapter takes a look at how to use XMLwith a PHP application. Also reviewed will be XML with Perl and Ruby.

❑ Chapter 18, “RSS and Atom.” One big use of XML as of late has been in regards to contentsyndication. Both RSS and Atom (competing standards) offer the ability to expose content foraggregation purposes. This chapter takes a close look at both of these technologies and howthey can be effectively used.

❑ Chapter 19, “Web Services.” Getting beyond the hype, this chapter takes an introductory lookat Web services and what it actually means to expose content and logic as SOAP in this dis-parate world.

❑ Chapter 20, “SOAP and WSDL.” Digging deeper into Web services, this chapter takes a look atthe main specification in the Web services world—SOAP. This chapter will focus on SOAP docu-ments including looking at SOAP headers and SOAP faults. Furthering the discussion aroundSOAP-based Web services, this chapter looks at the WSDL and UDDI specifications. WSDL isused to define a SOAP interface, whereas UDDI is used to locate services. Both of these specifi-cations will be discussed in detail.

❑ Chapter 21, “Advanced Web Services.” This chapter takes a look at these advanced specifica-tions and what they do for your Web services. In addition to examining the specifications, thischapter also describes implementing these specifications in your applications today.

❑ Chapter 22, “REST.” REST, a competing standard to SOAP, is heavily used in the UNIX world.In fact, companies such as Amazon have seen considerable success in using REST comparedto using SOAP. This chapter will take a look at exposing data and services using “the other” standard.

03_777779 flast.qxp 3/1/07 11:42 PM Page xxv

Page 28: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xxvi

Introduction

❑ Chapter 23, “XML Form Development.” Forms, as popular as they are on the Web, can now bedefined using XForms. This chapter takes a close look at XForms and how it can be used, alongwith other XML-based technologies, to produce various types of forms.

❑ Chapter 24, “The Resource Description Framework (RDF).” The RDF specification allows forthe relation of metadata to presentation content. RDF is a framework for describing and inter-changing metadata and is introduced in this chapter as well as demonstrated through someexamples.

❑ Chapter 25, “XML in Office Development.” This chapter takes a close look at using XML inOffice development with a particular focus on Microsoft Office.

❑ Chapter 26, “XAML.” This chapter takes a look at XAML—the new way Microsoft is promotingthe presentation of a GUI. XAML provides for presentation that is fluid and enriching. Thischapter takes a look at the basics of XAML and how to build some basic XAML applications.

ConventionsThis book uses a number of different styles of text and layout to help differentiate among various typesof information. Here are examples of the styles used and an explanation of what they mean:

❑ New words being defined are shown in italics.

❑ Keys that you press on the keyboard, such as Ctrl and Enter, are shown in initial caps andspelled as they appear on the keyboard.

❑ File and folder names, file extensions, URLs, and code that appear in regular paragraph text areshown in a monospaced typeface.

When we show a block of code that you can type as a program and run, it’s shown on separate lines,like this:

<?xml version=”1.0” encoding=”UTF-8” ?><Employee>

<FirstName>Bill</FirstName><LastName>Evjen</LastName><JobTitle>Technical Architect</JobTitle><Company>Lipper</Company><StartDate>10/04/2001</StartDate><WorkLocation>St. Louis, Missouri</WorkLocation><NumberOfDependents>3</NumberOfDependents>

</Employee>

or like this:

<?xml version=”1.0” encoding=”UTF-8” ?><Employee>

<FirstName>Bill</FirstName><LastName>Evjen</LastName><JobTitle>Technical Architect</JobTitle><Company>Lipper</Company><StartDate>10/04/2001</StartDate>

03_777779 flast.qxp 3/1/07 11:42 PM Page xxvi

Page 29: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xxvii

Introduction

<WorkLocation>St. Louis, Missouri</WorkLocation><NumberOfDependents>3</NumberOfDependents>

</Employee>

Sometimes you see code in a mixture of styles, like this:

<?xml version=”1.0” encoding=”UTF-8” ?><Employee>

<FirstName>Bill</FirstName><LastName>Evjen</LastName><JobTitle>Technical Architect</JobTitle><Company>Lipper</Company><StartDate>10/04/2001</StartDate><WorkLocation>St. Louis, Missouri</WorkLocation><NumberOfDependents>3</NumberOfDependents>

</Employee>

When mixed code is shown like this, the code with no background represents code that has been shownpreviously and that you don’t need to examine further. Code with the gray background is what youshould focus on in the current example.

We demonstrate the syntactical usage of methods, properties, and so on using the following format:

<?[target] [data]?>

Here, the italicized parts indicate placeholder text: object references, variables, or parameter values thatyou need to insert.

Most of the code examples throughout the book are presented as numbered listings that have descriptivetitles, like this:

LLiissttiinngg 11--88:: CCrreeaattiinngg aann XXMMLL ffiillee

Each listing is numbered (for example: 1-8) where the first number represents the chapter number andthe number following the hyphen represents a sequential number that indicates where that listing fallswithin the chapter. Downloadable code from the Wrox Web site (www.wrox.com) also uses this number-ing system so that you can easily locate the examples you are looking for.

Source CodeAs you work through the examples in this book, you may choose either to type all the code manually orto use the source code files that accompany the book. All the source code used in this book is availablefor download at www.wrox.com. When you get to the site, simply locate the book’s title (either by usingthe Search box or one of the topic lists) and click the Download Code link. You can then choose to down-load all the code from the book in one large zip file or download just the code you need for a particularchapter.

Because many books have similar titles, you may find it easiest to search by ISBN; this book’s ISBN is978-0-471-77777-9.

03_777779 flast.qxp 3/1/07 11:42 PM Page xxvii

Page 30: Professional XML · Stylus Studio 2006 47 Basic Editing in Stylus Studio 2006 47 Schema Development in Stylus Studio 2006 50 XSLT Development in Stylus Studio 51 Other Features in

xxviii

Introduction

After you download the code, just decompress it with your favorite compression tool. Alternatively, youcan go to the main Wrox code download page at www.wrox.com/dynamic/books/download.aspx tosee the code available for this book and all other Wrox books. Remember, you can easily find the codeyou are looking for by referencing the listing number of the code example from the book, such as“Listing 1-8.” We used these listing numbers when naming the downloadable code files.

ErrataWe make every effort to ensure that there are no errors in the text or in the code. However, no one is per-fect, and mistakes do occur. If you find an error in one of our books, such as a spelling mistake or faultypiece of code, we would be very grateful if you’d tell us about it. By sending in errata, you may spareanother reader hours of frustration; at the same time, you are helping us provide even higher-qualityinformation.

To find the errata page for this book, go to www.wrox.com and locate the title using the Search box or oneof the title lists. Then, on the book details page, click the Book Errata link. On this page, you can view allerrata that have been submitted for this book and posted by Wrox editors. A complete book list includinglinks to each book’s errata is also available at www.wrox.com/misc-pages/booklist.shtml.

If you don’t spot “your” error already on the Book Errata page, go to www.wrox.com/contact/techsupport.shtml and complete the form there to send us the error you have found. We’ll checkthe information and, if appropriate, post a message to the book’s errata page and fix the problem insubsequent editions of the book.

p2p.wrox.comFor author and peer discussion, join the P2P forums at p2p.wrox.com. The forums are a Web-based sys-tem for you to post messages relating to Wrox books and technologies and to interact with other readersand technology users. The forums offer a subscription feature that enables you to receive e-mail on top-ics of interest when new posts are made to the forums. Wrox authors, editors, other industry experts,and your fellow readers are represented in these forums.

At http://p2p.wrox.com you will find a number of different forums that will help you not only asyou read this book but also as you develop your own applications. To join the forums, just follow thesesteps:

1. Go to p2p.wrox.com and click the Register link.

2. Read the terms of use and click Agree.

3. Supply the information required to join, as well as any optional information you want toprovide, and click Submit.

03_777779 flast.qxp 3/1/07 11:42 PM Page xxviii