Table Of ContentAdvanced XML
Applications from the
Experts at The XML Guild
Q Q Q
Members of The XML Guild
© 2007 Thomson Course Technology, a division of Thomson Learning Inc. All rights Publisher and General
reserved. No part of this book may be reproduced or transmitted in any form or Manager, Thomson
by any means, electronic or mechanical, including photocopying, recording, or by Course Technology PTR:
Stacy L. Hiquet
any information storage or retrieval system without written permission from
Thomson Course Technology PTR, except for the inclusion of brief quotations in a Associate Director of
review. Marketing:
The Thomson Course Technology PTR logo and related trade dress are trademarks Sarah O’Donnell
of Thomson Course Technology, a division of Thomson Learning Inc., and may not
Manager of Editorial
be used without written permission. Services:
All trademarks are the property of their respective owners. Heather Talbot
Important: Thomson Course Technology PTR cannot provide software support. Marketing Manager:
Please contact the appropriate software manufacturer’s technical support line or Mark Hughes
Web site for assistance.
Acquisitions Editor:
Thomson Course Technology PTR and the authors have attempted throughout this Mitzi Koontz
book to distinguish proprietary trademarks from descriptive terms by following
Marketing Assistant:
the capitalization style used by the manufacturer.
Adena Flitt
Project Editor:
Kezia Endsley
PTR Editorial Services
Coordinator:
Erin Johnson
Information contained in this book has been obtained by Thomson Course
Technology PTR from sources believed to be reliable. However, because of the Copy Editor:
possibility of human or mechanical error by our sources, Thomson Course Kezia Endsley
Technology PTR, or others, the Publisher does not guarantee the accuracy,
Interior Layout Tech:
adequacy, or completeness of any information and is not responsible for any errors
Digital Publishing
or omissions or the results obtained from use of such information. Readers should
Solutions
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. Cover Designer:
Mike Tanamachi
Educational facilities, companies, and organizations interested in multiple copies
or licensing of this book should contact the Publisher for quantity discount Indexer:
information. Training manuals, CD-ROMs, and portions of this book, are also Kelly Dobbs Henthorne
available individually or can be tailored for specific needs.
Proofreader:
ISBN-10: 1-59863-214-0 Sandi Wilson
ISBN-13: 978-1-59863-214-9
eISBN-10: 1-59863-215-9
Library of Congress Catalog Card Number: 2006923271
Printed in the United States of America
07 08 09 10 11 PH 10 9 8 7 6 5 4 3 2 1
Thomson Course Technology PTR, a division of Thomson Learning Inc.
25 Thomson Place
Boston, MA 02210
http://www.courseptr.com
CONTENTS
Microformats ....................................................................................................76
The Use Case ..............................................................................................76
W3C XML Schema .....................................................................................77
RELAX NG ...................................................................................................77
Schematron .................................................................................................81
Transformations Needed ..........................................................................82
Wrapping Up ...................................................................................................86
Confirm That You’ve Really Hit a Limitation ............................................86
Consider Using a Workaround .................................................................88
Look at Other Schema Languages ...........................................................89
Think Differently .........................................................................................89
Summary ..........................................................................................................90
Chapter 3 XSLT .............................................................................................91
XPath .................................................................................................................91
Running XSLT ....................................................................................................91
XSLT 1.0 ............................................................................................................92
XSLT 1.0 Elements ............................................................................................93
Document Elements ....................................................................................93
Top-Level Elements .....................................................................................94
Instruction Elements ...................................................................................95
Push versus Pull ................................................................................................96
Using XSLT to Convert XML to HTML ..............................................................96
The “Pull” Method ......................................................................................96
The “Push” Method ....................................................................................97
Tips and Tricks for XSLT 1.0 .............................................................................99
Dynamic XML Transformation Using XSLT ..............................................99
xml-to-string.xsl ........................................................................................100
Namespace Declaration Normalizer .....................................................100
Excel Spreadsheet Cleanup ....................................................................102
xvi
Q Q Q
CONTENTS
Reversing a Table ....................................................................................104
Generating XSLT with XSLT .....................................................................106
Building a Data Mapping Stylesheet from a Mapping Expressed
in XML .................................................................................................107
Parsing Strings with XSLT ........................................................................110
Using XSLT to Convert XML to Java/C# Code ......................................115
XSLT Example of “for i=1 to n do” .........................................................119
Muenchian Method for Grouping ..........................................................119
Handling Character Entities in XSLT 1.0 .................................................122
XPath 1.0 Subtleties .......................................................................................128
Using position() Properly ........................................................................128
Using Variables to Store Literal or Numeric Values .............................130
XSLT 2.0 ..........................................................................................................130
XSLT 2.0 Features Overview .........................................................................132
Temporary Trees ......................................................................................132
Grouping ..................................................................................................133
Regular Expressions ................................................................................138
Sequences ................................................................................................139
Multiple Output Files ...............................................................................140
Stylesheet Functions ................................................................................141
Schema Awareness and Type Checking ...............................................142
Handling Special Characters ..................................................................142
Tunnel Parameters ...................................................................................144
Next Match ..............................................................................................144
Summary ........................................................................................................145
Chapter 4 Web Services ............................................................................147
Functional Overview .....................................................................................147
Web Services Technology Overview ...........................................................150
Examples of Web Services .....................................................................150
xvii
Q Q Q
CONTENTS
SOAP ..............................................................................................................152
SOAP Messaging and HTTP Binding ......................................................152
SMTP Bindings ..........................................................................................152
Example SOAP Messages ......................................................................153
SOAP and RPCs .......................................................................................154
WSDL and SOAP ...........................................................................................154
WSDL Message ........................................................................................155
WSDL Binding ..........................................................................................155
WSDL Service ...........................................................................................155
Example WSDL Message ........................................................................156
UDDI ...............................................................................................................159
WS-I .................................................................................................................163
WS-I Profiles .............................................................................................164
WS-I Sample Applications .......................................................................164
WS-I Testing Tools ....................................................................................164
Alternatives: REST and ebXML .....................................................................165
REST ..........................................................................................................165
ebXML .......................................................................................................166
Summary ........................................................................................................166
Chapter 5 XML APIs ...................................................................................167
Comparison of XML APIs ..............................................................................168
Streaming APIs ...............................................................................................168
SAX ...........................................................................................................169
XMLReader ...............................................................................................179
StAX ..........................................................................................................182
Random-Access APIs .....................................................................................185
DOM .........................................................................................................186
dom4j, JDOM, and XOM ........................................................................189
VTD-XML ...................................................................................................189
xviii
Q Q Q
CONTENTS
JAXP XPath ...............................................................................................189
XPathDocument (.NET, Mono) ................................................................191
Schema Compilers .........................................................................................193
JAXB ..........................................................................................................195
.NET XML Schema Definition Tool ..........................................................203
C24 IO ......................................................................................................206
Relaxer .....................................................................................................209
XML-Object Mappers ....................................................................................209
Object Serializers ...........................................................................................210
Transformers ..................................................................................................211
JAXP Transformation API (TrAX) ............................................................211
System.Xml.Xsl .........................................................................................214
XQJ ...........................................................................................................216
Selecting the Right XML API for the Job ......................................................216
Choose the Right Kind of Efficiency ........................................................216
Use Multiple XML APIs When Appropriate ...........................................217
Robustness of XML APIs ..........................................................................218
Summary ........................................................................................................219
Chapter 6 XML and Databases ................................................................221
Approaches to Storing XML Data ................................................................222
Using Blob Storage ..................................................................................224
Using Shredded Storage .........................................................................225
Using Native Storage ..............................................................................228
The Role of Schemas in XML Storage and Query .......................................229
Schema-Awareness in XQuery ...............................................................231
Managing Schema Variety and Change ...............................................234
When Not to Use a Schema ...................................................................237
Choosing a Database Product ......................................................................238
Summary ........................................................................................................242
xix
Q Q Q
CONTENTS
Chapter 7 XQuery .....................................................................................243
XQuery Data Model ......................................................................................243
Atomic Values ..........................................................................................244
Nodes .......................................................................................................244
Sequences ................................................................................................245
Constructs Not in XQuery Data Model ..................................................246
Grammar Notes .............................................................................................246
Constructors .............................................................................................246
Expressions ..............................................................................................247
Enclosed Expressions ..............................................................................248
Comma Operators ...................................................................................250
Path Expressions ............................................................................................250
Selecting the Root Element ......................................................................251
Selecting Child Elements ..........................................................................251
Selecting Attributes ..................................................................................252
Restricting the Selection ...........................................................................253
How Do Path Expressions Work? ..........................................................253
FLWOR Expressions ......................................................................................259
For Clauses ...............................................................................................260
Let Clauses ................................................................................................261
Where Clauses .........................................................................................264
Order By Clauses .....................................................................................265
Joining Documents with FLWOR Expressions .......................................266
Other Expressions .........................................................................................269
Arithmetic Expressions ............................................................................269
Comparison Expressions .........................................................................269
Conditional Expressions (if-then-else) ....................................................270
Set Expressions ........................................................................................271
Functions .........................................................................................................271
Input Functions .........................................................................................271
String and Data Functions .......................................................................272
xx
Q Q Q
CONTENTS
User-Defined Functions ............................................................................274
Datatypes .......................................................................................................276
Using XQuery without Datatypes ..........................................................276
Using XQuery with Datatypes ................................................................278
Expressions That Use Datatypes ............................................................280
Some Important Details .................................................................................281
Atomization ..............................................................................................281
Effective Boolean Values .........................................................................282
Modules and Prologs ....................................................................................283
Library Modules .......................................................................................283
Global Variables .....................................................................................285
Namespaces ............................................................................................285
Summary ........................................................................................................286
Chapter 8 XML Authoring .........................................................................289
Defining an XML Authoring Environment ....................................................290
Using a WYSIWYG Authoring Tool ........................................................290
Using XML IDE .........................................................................................291
Using Standard Word Processing ..........................................................291
Converting Data into XML ......................................................................292
Selecting an Appropriate Schema ...............................................................292
Schema Constructs That Affect Authors .................................................293
Approaches for Difficult Schema Structures ................................................297
Tables ........................................................................................................297
Common Standard Schemas ..................................................................302
Form-Based Authoring ..................................................................................303
HTML Custom Forms ................................................................................303
Creating Forms with XForms ...................................................................307
Summary ........................................................................................................310
xxi
Q Q Q
CONTENTS
Chapter 9 XSL-FO .......................................................................................311
XML Transformation and Rendering Using HTML ......................................313
XML Transformation and Pagination Using XSL-FO ...................................313
The XSL-FO Processing Model ......................................................................314
Bordering and Area Tree Rectangles ...........................................................316
Un-Bordered and Bordered Areas .........................................................316
Area Placement for Block-Level Constructs ...........................................320
Area Placement for Inline-Level Constructs ...........................................323
Area Backgrounds ...................................................................................325
Bidirectional Text Protection .........................................................................326
Unicode Directionality .............................................................................326
Formatting Without Consideration for Direction ...................................328
Formatting with Consideration for Direction .........................................330
Disambiguation and Aggregation in Area Tree References ......................333
A Problem of Ambiguity ..........................................................................334
Adding Stylesheet References ................................................................336
Making Numerous References ...............................................................337
Retrieve-Marker Arbitration .........................................................................338
Summary ........................................................................................................341
Appendix A XML Tools and Implementations ............................................343
Index ...................................................................................................355
xxii
Q Q Q
1
XML Namespaces
by Evan Lenz
This chapter is about XML namespaces, the standard mechanism for assigning expanded-names
to elements and attributes. The first part, “Understanding Namespaces,” looks at what their pur-
pose is and how the mechanism itself works. The second part, “Namespaces in XSLT,” presents
an in-depth look at how XSLT handles namespaces, including coverage of features that are new
in XSLT 2.0.
Understanding Namespaces
Namespaces are used heavily across many XML applications, tools, and technologies. Even if
you decide not to use them for your own custom XML vocabularies, you will need to learn how
they work. For example, XSLT uses namespaces to disambiguate between code and data. All of
XSLT’s elements are in the XSLT namespace, whereas other elements are taken to be literal result
elements, that is, elements that are copied to the result as data rather than interpreted as code.
Motivation for Using XML Namespaces
The “Namespaces in XML 1.0” recommendation cites two primary reasons you might want to
use XML namespaces: to avoid name collisions and to facilitate name recognition.
Q
Note
See “Namespaces in XML 1.0” at http://www.w3.org/TR/xml-names/.
Avoiding Name Collisions
Namespaces allow you to use multiple markup vocabularies within the same document with-
out having to worry about name collisions. For example, you might have an XML document
that contains two different elements named title. One of them might describe the title of a
1
Q Q Q