Building Oracle XML Applications
Steve Muench Publisher: O'Reilly First Edition September 2000 ISBN: 1-56592-691-9, 810 pages
Building Oracle XML Applications gives Java and PL/SQL developers a rich and detailed look at the many tools Oracle provides to support XML development. It shows how to combine the power of XML and XSLT with the speed, functionality, and reliability of the Oracle database. The author delivers nearly 800 pages of entertaining text, helpful and time-saving hints, and extensive examples that developers can put to use immediately to build custom XML applications. The accompanying CD-ROM contains JDeveloper 3.1, an integrated development environment for Java developers.
Building Oracle XML Applications
Copyright © 2000 O'Reilly & Associates, Inc. All rights reserved.
Printed in the United States of America.
Published by O'Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472.
The O'Reilly logo is a registered trademark of O'Reilly & Associates, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
Oracle®, JDeveloper™, and all Oracle-based trademarks and logos are trademarks or registered trademarks of Oracle Corporation, Inc. in the United States and other countries. Java™ and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. O'Reilly & Associates, Inc. is independent of Oracle Corporation and Sun Microsystems.
While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Table of Contents
Preface Audience for This Book Which Platform and Version? Structure of This Book About the Examples About the CD-ROM Conventions Used in This Book Comments and Questions Acknowledgments
I: XML Basics
1. Introduction to XML 1.1 What Is XML? 1.2 What Can I Do with XML? 1.3 Why Should I Use XML? 1.4 What XML Technologies Does Oracle Provide?
2. Working with XML 2.1 Creating and Validating XML 2.2 Modularizing XML 2.3 Searching XML with XPath
II: Oracle XML Fundamentals
3. Combining XML and Oracle 3.1 Hosting the XML FAQ System on Oracle 3.2 Serving XML in Any Format 3.3 Acquiring Web-based XML Content
4. Using JDeveloper for XML Development 4.1 Working with XML, XSQL, and JSP Files 4.2 Working with Database Objects 4.3 Using JDeveloper with Oracle XDK Components
5. Processing XML with PL/SQL 5.1 Loading External XML Files 5.2 Parsing XML 5.3 Searching XML Documents with XPath 5.4 Working with XML Messages 5.5 Producing and Transforming XML Query Results
6. Processing XML with Java 6.1 Introduction to Oracle8 i JServer 6.2 Parsing and Programmatically Constructing XML 6.3 Searching XML Documents with XPath 6.4 Working with XML Messages 6.5 Producing and Transforming XML Query Results
7. Transforming XML with XSLT 7.1 XSLT Processing Mechanics 7.2 Single-Template Stylesheets 7.3 Understanding Input and Output Options 7.4 Improving Flexibility with Multiple Templates
8. Publishing Data with XSQL Pages 8.1 Introduction to XSQL Pages 8.2 Transforming XSQL Page Results with XSLT 8.3 Troubleshooting Your XSQL Pages
9. XSLT Beyond the Basics 9.1 Using XSLT Variables 9.2 The Talented Identity Transformation 9.3 Grouping Repeating Data Using SQL 9.4 Sorting and Grouping Repeating Data with XSLT
10. Generating Datagrams with PL/SQL 10.1 Programmatically Generating XML Using PL/SQL 10.2 Automatic XML Generation with DBXML
11. Generating Datagrams with Java 11.1 Generating XML Using Java 11.2 Serving XML Datagrams over the Web 11.3 Automatic XML from SQL Queries
12. Storing XML Datagrams 12.1 Overview of XML Storage Approaches 12.2 Loading Datagrams with the XML SQL Utility 12.3 Storing Posted XML Using XSQL Servlet 12.4 Inserting Datagrams Using Java
13. Searching XML with interMedia 13.1 Why Use interMedia? 13.2 What Is interMedia? 13.3 The interMedia Query Language 13.4 Handling Heterogeneous Doctypes 13.5 Handling Doctype Evolution 13.6 Advanced interMedia
14. Advanced XML Loading Techniques 14.1 Storing Datagrams in Multiple Tables 14.2 Building an XMLLoader Utility 14.3 Creating Insert Transformations Automatically
III: Oracle XML Applications
15. Using XSQL Pages as a Publishing Framework 15.1 Overview of XSQL Pages Facilities 15.2 Additional XML Delivery Options
16. Extending XSQL and XSLT with Java 16.1 Developing Custom XSQL Actions 16.2 Integrating Custom XML Sources 16.3 Working with XSLT Extension Functions
17. XSLT-Powered Portals and Applications 17.1 XSLT-Powered Web Store 17.2 Building a Personalized News Portal 17.3 Online Discussion Forum
IV: Appendixes
A. XML Helper Packages A.1 Installing the XML Helper Packages A.2 Source Code for the XML Helper Packages
B. Installing the Oracle XSQL Servlet B.1 Supported Configurations B.2 Prerequisites B.3 Downloading and Installing the XSQL Servlet
C. Conceptual Map to the XML Family
D. Quick References
Colophon Preface
This book is a hands-on, practical guide that teaches you the nuts and bolts of XML and the family of Internet standards related to it and shows how to exploit XML with your Oracle database using Java™, PL/SQL, and declarative techniques. It’s a book for Oracle developers by an Oracle developer who has lived the technology at Oracle Corporation for over ten years and has directly catalyzed the company’s XML technology direction and implementation. As you read this book, I hope you will come to appreciate the wide variety of tools Oracle provides to enable you to combine the best of XML with the best of Oracle to build flexible, database-powered applications for the Web.
This book abounds with tested, commented, and fully explained examples because—in the unforgettable words of a high school mentor of mine—“you only get good at something by working through an ungodly number of problems." The examples include a number of helper libraries and utilities that will serve to jump-start your own Oracle XML development projects (see “About the Examples" later in this Preface for details).
If this book has one main goal, it is to educate, excite, and thoroughly convince you that by combining:
• The speed, functionality, and reliability of the Oracle database • The power of XML as a universal standard for data exchange • The flexibility to easily transform XML data into any format required we can accomplish some pretty amazing things, not to mention saving ourselves a lot of work in the process. Audience for This Book
This book is aimed mainly at Java and PL/SQL developers who want to use the XML family of Internet standards in conjunction with their Oracle databases. I also expect that this book may catch the eye of existing Oracle database administrators who want to update their skills to learn how to apply Java, PL/SQL, and XML to their daily work. In addition, the in-depth coverage of Oracle’s template-driven XSQL Pages technology should prove useful to non-programmers as well.
This book assumes no prior knowledge of XML on your part, but it does assume a basic working knowledge of SQL and familiarity with either Java or PL/SQL as a programming language.
Which Platform and Version?
Much of this book applies to Oracle8 and Oracle8i (and even Oracle7 in some cases). In general, if you want to use XML outside the database, you can use any Oracle version. However, if you want to use XML features inside the database (and take full advantage of the features I describe here), you must use Oracle8i. Wherever relevant, I note whether a particular XML feature is specific to Oracle8i or can be used with earlier Oracle versions as well.
The examples for this book were developed and tested on a Windows NT 4.0 platform using JDeveloper 3.1 as a development environment and Oracle8i Release 2 Enterprise Edition for NT (version 8.1.6 ) as the database. However, none of the examples, tools, or technologies covered in the book are Windows-specific. The JDeveloper 3.1 product—included on the CD-ROM that accompanies this book—is certified to run on Windows NT and Windows 2000.
Structure of This Book
This book is not divided strictly by individual tool and function. Instead, it begins in Part I with an overview of fundamental XML standards and concepts. Part II covers all core Oracle XML technologies, presenting increasingly detailed discussions of various Oracle XML capabilities. Part III describes combining the technologies we’ve learned to build applications and portals. Finally, Part IV includes four useful appendixes with installation and reference information.
The book uses extensive examples—in both PL/SQL and Java—to present material of increasing sophistication.
The following list summarizes the contents in detail.
Part I, introduces the basics of XML and provides a high-level overview of Oracle’s XML technology. It consists of the following chapters: • Chapter 1, provides a gentle introduction to XML by describing what it is, what you can do with it, why you should use it, and what software Oracle supplies to work with it. • Chapter 2, describes how to build your own “vocabularies" of tags to represent the information you need to work with, as well as how to use XML namespaces and entities to modularize your documents and XPath expressions to search them.
Part II, describes the core development activities that Oracle XML developers need to understand when using XML with an Oracle database. It consists of the following chapters:
• Chapter 3, provides a typical “day-in-the-life" scenario illustrating the power of combining XML with an Oracle database. • Chapter 4, describes how you can use Oracle’s JDeveloper product to help with XML development. • Chapter 5, explains how you can use PL/SQL to load XML files, parse XML, search XML documents, post XML messages, and both enqueue and dequeue XML messages from queues. • Chapter 6, explains how you can combine Java and XML both inside and outside Oracle8i to load XML files, parse XML, search XML documents, and post XML messages, as well as enqueue and dequeue XML messages from queues. • Chapter 7, explains the fundamentals of creating XSLT stylesheets to carry out transformations of a source XML document into a resulting XML, HTML or plain text output. • Chapter 8, explains how to build dynamic XML datagrams from SQL using declarative templates to perform many common tasks. • Chapter 9, builds on the fundamentals from Chapter 7 and explores additional XSLT functionality like variables, sorting and grouping techniques, and the many kinds of useful transformations that can be done using a variation on the identity transformation. • Chapter 10, gives Java developers a whirlwind introduction to PL/SQL and describes how to use PL/SQL to dynamically produce custom XML datagrams containing database information. • Chapter 11, describes numerous techniques for programmatically producing XML datagrams using Java by using JDBC™, SQLJ, JavaServer Pages™, and the Oracle XML SQL Utility. • Chapter 12, explains how to store XML datagrams in the database using the XML SQL Utility and other techniques, as well as how to retrieve them using XSQL pages and XSLT transformations. • Chapter 13, describes how you can use Oracle8i ’s integrated interMedia Text functionality to search XML documents, leveraging their inherent structure to improve text searching accuracy. • Chapter 14, describes the techniques required to insert arbitrarily large and complicated XML into multiple tables. It also covers using stylesheets to generate stylesheets to help automate the task.
Part III, describes how to build applications using Oracle and XML technologies. It consists of the following chapters: • Chapter 15, builds on Chapter 8, explaining the additional features that make XSQL Pages an extensible framework for assembling, transforming, and delivering XML information of any kind. • Chapter 16, describes how to extend the functionality of the XSQL Pages framework using custom action handlers, and how to extend the functionality of XSLT stylesheets by calling Java extension functions. • Chapter 17, builds further on Chapter 11 and on earlier chapters, describing best-practice techniques to combine XSQL pages and XSLT stylesheets to build personalized information portal and sophisticated online discussion forum applications.
Part IV, contains the following summaries:
• Appendix A, provides the source code for the PL/SQL helper packages we built in Chapter 3: xml, xmldoc, xpath, xslt, and http. • Appendix B, describes how to install the XSQL Servlet that you can use with any servlet engine (Apache JServ, JRun, etc.). • Appendix C, graphically summarizes the relationships between key XML concepts and the family of XML-related standards that supports them. • Appendix D, provides “cheat sheets" on XML, XSLT, and XPath syntax.
About the Examples
This book contains a large number of fully working examples. Many are designed to help you build your own Oracle XML applications. To that end, I’ve included all examples on the O’Reilly web site (http://www.oreilly.com/catalog/orxmlapp ). The site includes full source code of all examples and detailed instructions on how to create the sample data required for each chapter. I’ll try to keep the code up to date, incorporating corrections to any errors that are discovered, as well as improvements suggested by readers.
In order to run the complete set of examples yourself, you will need the following software:
• Oracle 8i Release 2 (version 8.1.6) or greater • Oracle JDeveloper 3.1 or greater
From the Oracle XML Developer’s Kit for Java:
• Oracle XML Parser/XSLT Processor for Java, Release 2.0.2.9 or greater • Oracle XSQL Pages and the XSQL Servlet Release 1.0.0.0 • Oracle XML SQL Utility
From the Oracle XML Developer’s Kit for PL/SQL:
• Oracle XML Parser/XSLT Processor for PL/SQL Release 1.0.2 or greater All of this software is downloadable from the Oracle Technology Network (OTN) web site for Oracle developers at http://technet.oracle.com and is available free of charge for single-developer use. For information on runtime distribution of the Oracle XML Developer’s kit components, read the license agreement on the download page of any of the components. For your convenience, all of the software listed—with the exception of the Oracle8i database itself—is available on the CD-ROM accompanying this book and is automatically installed as part of the JDeveloper 3.1 installation.
About the CD-ROM
We are grateful to Oracle Corporation for allowing us to include the JDeveloper 3.1 for Windows NT software (developer version) on the CD-ROM accompanying this book. This product provides a complete development environment for Java developers working with Oracle and XML. Chapter 4 covers the details of significant JDeveloper 3.1 features that are of interest to XML application developers. You’ll find full product documentation and online help on the CD-ROM as well.
Conventions Used in This Book
The following conventions are used in this book:
Italic
Used for file and directory names and URLs, menu items, and for the first mention of new terms under discussion
Constant width
Used in code examples and for package names, XML elements and attributes, and Java classes and methods
Constant width italic
In some code examples, indicates an element (e.g., a filename) that you supply
Constant width bold
Indicates user input in code examples
UPPERCASE
Generally used for Oracle SQL and PL/SQL keywords lowercase Generally used for table names in text and for table, column, and variable names in code examples
The following icons are used in this book:
This icon indicates a tip, suggestion, or general note related to surrounding text.
This icon indicates a warning related to surrounding text.
Comments and Questions
I have tested and verified the information in this book to the best of my ability, but you may find that features have changed (or even that I have made mistakes!). Please let me know about any errors you find, as well as your suggestions for future editions, by writing to:
O’Reilly & Associates 101 Morris Street Sebastopol, CA 95472 800-998-9938 (in the U.S. or Canada) 707-829-0515 (international or local) 707-829-0104 (FAX)
You can also send O’Reilly messages electronically. To be put on the mailing list or request a catalog, send email to: [email protected]
To ask technical questions or comment on the book, send email to: [email protected]
We have a web site for this book, where we’ll include examples (see Section P.4 earlier in the Preface), errata, and any plans for future editions. You can access this page at: http://www.oreilly.com/catalog/orxmlapp/
For more information about this book and others, see the O’Reilly web site: http://www.oreilly.com Acknowledgments
I owe an unrepayable debt of gratitude to my wife Sita. For over a year, she juggled our two active youngsters on nights and weekends while Daddy “disappeared" to work on his book—a true labor of love. She did not understand what demon drove me to write this book, but she felt I might regret not writing it for the rest of my life. I’m happy to say to her, Emma, and Amina, “Daddy’s home."
Thanks to my mother-in-law, Dr. Nila Negrin, who assisted me in finding the perfect XML insect to grace the cover of this book, Xenochaetina Muscaria Loew. Regrettably, O’Reilly couldn’t find a print of this Tennessee-native fly, so we had to go for plan B.
Many thanks to the technical reviewers for this book: Adam Bosworth, Terris Linenbach, Don Herkimer, Keith M. Swartz, Leigh Dodds, Murali Murugan, Bill Pribyl, and Andrew Odewahn. I owe Keith a special thank you for his amazingly detailed review.
Garrett Kaminaga, a key developer on Oracle’s interMedia Text product development team, wrote the lion’s share of Chapter 13, for which I am very grateful. In addition, thanks go to MK, Visar, and Karun in Oracle’s Server Technology XML development team for answering questions when I bumped into problems, and for always having an open mind to new ideas.
Norm Walsh, coauthor of DocBook: The Definitive Guide (O’Reilly & Associates), offered early encouragement for my then-crazy idea of authoring this entire book in XML, and he answered many questions at the outset about using the DocBook DTD for technical manuals.
Many thanks to Tony Graham at Mulberry Technologies for giving us permission to include the helpful XML, XSLT, and XPath quick references in Appendix D and to Oracle Corporation for allowing us to include JDeveloper 3.1 on the accompanying CD-ROM.
Thanks to the entire O’Reilly production team, especially to Madeleine Newell, the project manager and copyeditor, whose keen questions about wording and XML enhanced the book.
Finally, thanks to Debby Russell, my editor at O’Reilly, for believing in my initial idea and more importantly for not rushing me to finish. The book you’re now reading is everything I envisioned at the outset for a one-stop-shop book for developers using Oracle and XML. No compromises were made and thankfully, none was ever asked of me. Part I: XML Basics
This part of the book introduces the basics of XML and provides a high-level overview of Oracle's XML technology. It consists of the following chapters:
• Chapter 1, provides a gentle introduction to XML by describing what it is, what you can do with it, why you should use it, and what software Oracle supplies to work with it. • Chapter 2, describes how to build your own vocabularies of tags to represent the information you need to work with, as well as how to use XML namespaces and entities to modularize your documents and XPath expressions to search them. Chapter 1. Introduction to XML
The Internet is driving an unprecedented demand for access to information. Seduced by the convenience of paying bills, booking flights, tracking stocks, checking prices, and getting everything from gifts to groceries online, consumers are hungry for more. Compelled by the lower costs of online outsourcing and the ability to inquire, day or night, "What's the status?," businesses are ramping up to reap the rewards. Excited by improved efficiency and universal public access, governments are considering how all kinds of raw data, from financial reports to federally funded research, can be published online in an easily reusable format.
More than ever before, database-savvy web application developers working to capitalize on these exciting Internet-inspired opportunities need to rapidly acquire, integrate, and repurpose information, as well as exchange it with other applications both inside and outside their companies. XML dramatically simplifies these tasks.
As with any new technology, you first need to understand what XML is, what you can do with it, and why you should use it. With all the new terms and acronyms to understand, XML can seem like a strange new planet to the uninitiated, so let's walk before we run. This chapter introduces "Planet XML" and the "moons" that orbit it, and provides a high-level overview of the tools and technology Oracle offers to exploit the combined strengths of XML and the Oracle database in your web applications.
1.1 What Is XML?
First, let's look at some basic XML definitions and examples.
1.1.1 Extensible Markup Language
XML, which stands for the "Extensible Markup Language," defines a universal standard for electronic data exchange. It provides a rigorous set of rules enabling the structure inherent in data to be easily encoded and unambiguously interpreted using human-readable text documents. Example 1.1 shows what a stock market transaction might look like represented in XML.
Example 1.1. Stock Market Transaction Represented in XML
After an initial line that identifies the document as an XML document, the example begins with a
XML's straightforward "text with tags" syntax should look immediately familiar if you have ever worked with HTML, which also uses tags, text, and attributes. A key difference between HTML and XML, however, lies in the kind of data each allows you to represent. What you can represent in an HTML document is constrained by the fixed set of HTML tags at your disposal—like