Eric M. Burke - Java and XSLT.pdf

(2039 KB) Pobierz
Microsoft Word - Java & XSLT.doc
Java and XSLT
Eric M. Burke
Publisher: O'Reilly
First Edition September 2001
ISBN: 0-596-00143-6, 528 pages
By GiantDino
Copyright
Table of Contents
Index
Full Description
About the Author
Reviews
Reader reviews
Errata
Learn how to use XSL transformations in Java programs ranging
from stand-alone applications to servlets. Java and XSLT introduces
XSLT and then shows you how to apply transformations in real-
world situations, such as developing a discussion forum,
transforming documents from one form to another, and generating
content for wireless devices.
Java and XSLT
Preface
Audience
Software and Versions
Organization
Conventions Used in This Book
How to Contact Us
Acknowledgments
1. Introduction
1.1 Java, XSLT, and the Web
1.2 XML Review
1.3 Beyond Dynamic Web Pages
1.4 Getting Started
1.5 Web Browser Support for XSLT
2. XSLT Part 1 -- The Basics
2.1 XSLT Introduction
2.2 Transformation Process
2.3 Another XSLT Example, Using XHTML
2.4 XPath Basics
2.5 Looping and Sorting
2.6 Outputting Dynamic Attributes
3. XSLT Part 2 -- Beyond the Basics
3.1 Conditional Processing
3.2 Parameters and Variables
3.3 Combining Multiple Stylesheets
15333099.003.png 15333099.004.png
3.4 Formatting Text and Numbers
3.5 Schema Evolution
3.6 Ant Documentation Stylesheet
4. Java-Based Web Technologies
4.1 Traditional Approaches
4.2 The Universal Design
4.3 XSLT and EJB
4.4 Summary of Key Approaches
5. XSLT Processingwith Java
5.1 A Simple Example
5.2 Introduction to JAXP 1.1
5.3 Input and Output
5.4 Stylesheet Compilation
6. Servlet Basics and XSLT
6.1 Servlet Syntax
6.2 WAR Files and Deployment
6.3 Another Servlet Example
6.4 Stylesheet Caching Revisited
6.5 Servlet Threading Issues
7. Discussion Forum
7.1 Overall Process
7.2 Prototyping the XML
7.3 Making the XML Dynamic
7.4 Servlet Implementation
7.5 Finishing Touches
8. Additional Techniques
8.1 XSLT Page Layout Templates
8.2 Session Tracking Without Cookies
8.3 Identifying the Browser
8.4 Servlet Filters
8.5 XSLT as a Code Generator
8.6 Internationalization with XSLT
9. Development Environment, Testing, and Performance
9.1 Development Environment
9.2 Testing and Debugging
9.3 Performance Techniques
10. Wireless Applications
10.1 Wireless Technologies
10.2 The Wireless Architecture
10.3 Java, XSLT, and WML
10.4 The Future of Wireless
A. Discussion Forum Code
B. JAXP API Reference
C. XSLT Quick Reference
Colophon
Preface
Java and Extensible Stylesheet Language Transformations (XSLT) are very different
technologies that complement one another, rather than compete. Java's strengths are portability,
its vast collection of standard libraries, and widespread acceptance by most companies. One
weakness of Java, however, is in its ability to process text. For instance, Java may not be the
best technology for merely converting XML files into another format such as XHTML or Wireless
Markup Language (WML). Using Java for such a task requires skilled programmers who
understand APIs such as DOM, SAX, or JDOM. For web sites in particular, it is desirable to
simplify the page generation process so nonprogrammers can participate.
XSLT is explicitly designed for XML transformations. With XSLT, XML data can be transformed
into any other text format, including HTML, XHTML, WML, and even unexpected formats such as
Java source code. In terms of complexity and sophistication, XSLT is harder than HTML but
easier than Java. This means that page authors can probably learn how to use XSLT successfully
but will require assistance from programmers as pages are developed.
XSLT processors are required to interpret and execute the instructions found in XSLT
stylesheets. Many of these processors are written in Java, making Java an excellent choice for
applications that must interoperate with XML and XSLT. For web sites that utilize XSLT, Java
servlets and EJBs are still required to intercept client requests, fetch data from databases, and
implement business logic. XSLT may be used to generate each of the XHTML web pages, but
this cannot be done without a language like Java acting as the coordinator.
This book explains the most important concepts behind the XSLT markup language but is not a
comprehensive reference on that subject. Instead, the focus is on interoperability with Java, with
particular emphasis on servlets and web applications. Every concept is backed by working
examples, all of which work on widely available, free tools.
Audience
Java programmers who want to learn how to use XSLT comprise the target audience for this
book. Java programming experience is essential, and basic familiarity with XML terminology is
helpful, but not required. Since so many of the examples revolve around web applications and
servlets, Chapter 4 and 6 are devoted to this topic, offering a fast-paced tutorial to servlet
technology. Chapter 2 and Chapter 3 contain a detailed XSLT tutorial, so no prior knowledge of
XSLT is required.
This book is particularly well-suited for readers who may have read a lot about these technologies
but have not used everything together in a complete application. Chapter 7 , for example,
presents the implementation of a web-based discussion forum from start to finish. Fully worked
examples can be found in every chapter, ranging from an Ant build file documentation stylesheet
in Chapter 3 to internationalization techniques in Chapter 8 .
Software and Versions
Keeping up with the latest technologies is always a challenge, particularly when writing about
XML-related tools. The set of tools listed in Table P-1 is sufficient to run just about every
example in this book.
Table P-1. Software and versions
15333099.005.png
Tool
URL
Description
Crimson
Included with JAXP 1.1
XML parser from Apache
JAXP 1.1
http://java.sun.com/xml
Java API for XML Processing
JDK 1.2.x
http://java.sun.com
Any Java 2 Standard Edition SDK
JDOM beta 6 http://www.jdom.org
Open source alternative to DOM
JUnit 3.7
http://www.junit.org
Open source unit testing framework
Tomcat 4.0 http://jakarta.apache.org
Open source servlet container
Xalan
Included with JAXP 1.1
XSLT processor
There are certainly other tools, most notably the SAXON XSLT processor available from
http://users.iclway.co.uk/mhkay/saxon . This can easily be substituted for Xalan because of
the vendor-independence that JAXP offers.
All of the examples, as well as JAR files for the tools listed in Table P-1 , are available for
download from http://www.javaxslt.com and from the O'Reilly web site at
http://www.oreilly.com/catalog/javaxslt . The included README.txt file contains
instructions for compiling and running the examples.
Organization
This book consists of 10 chapters and 3 appendixes, as follows:
Chapter 1
Provides a broad overview of the technologies covered in this book and explains how
XML, XSLT, Java, and other APIs are related. Also reviews basic XML concepts for
readers who are familiar with Java but do not have a lot of XML experience.
Chapter 2
Introduces XSLT syntax through a series of small examples and descriptions. Describes
how to produce HTML and XHTML output and explains how XSLT works as a language.
XPath syntax is also introduced in this chapter.
Chapter 3
Continues with material presented in the previous chapter, covering more sophisticated
XSLT language features such as conditional logic, parameters and variables, text and
number formatting, and producing XML output. This chapter concludes with a more
sophisticated example that produces summary reports for Ant build files.
Chapter 4
Offers comparisons between popular web development technologies, comparing each
with the Java and XSLT approach. The model-view-controller architecture is discussed in
detail, and the relationship between XSLT web applications and EJB is touched upon.
Chapter 5
Shows how to use XSLT processors with Java applications and servlets. Older Xalan and
SAXON APIs are mentioned, but the primary focus is on Sun's JAXP. Key examples
show how to use XSLT and SAX to transform non-XML files and data sources, how to
15333099.006.png 15333099.001.png 15333099.002.png
improve performance through caching techniques, and how to interoperate with DOM
and JDOM.
Chapter 6
Provides a detailed review of Java servlet programming techniques. Shows how to create
web applications and WAR files, how to deploy XML and XSLT files within these web
applications, and how to perform XSLT transformations from servlets.
Chapter 7
Implements a complete web application from start to finish. In this chapter, a web-based
discussion forum is designed and implemented using Java, XML, and XSLT techniques.
The relationship between CSS and XSLT is presented, and XHTML Strict is used for all
web pages.
Chapter 8
Covers important Java and XSLT programming techniques that build upon concepts
presented in earlier chapters, concluding with a detailed discussion of XSLT
internationalization. Other topics include XSLT page layout templates, servlet session
tracking without cookies, browser identification, and servlet filters.
Chapter 9
Offers practical advice for making a wide range of XML parsers, XSLT processors, and
various other Java tools work together. Shows how to resolve conflicts with incompatible
XML JAR files, how to write simple unit tests with JUnit, and how to write custom JAXP
error handlers. Also discusses performance techniques and the relationship between
XSLT and EJB.
Chapter 10
Describes the world of wireless technologies, with emphasis on Wireless Markup
Language (WML). Shows how to detect wireless devices from a servlet, how to write
XSLT stylesheets for these devices, and how to test using a variety of cell phone
simulators. An online movie theater application is developed to reinforce the concepts.
Appendix A
Contains all of the remaining code from the discussion forum example presented in
Chapter 7 .
Appendix B
Lists and briefly describes each of the classes in Version 1.1 of the JAXP API.
Appendix C
Contains a quick reference for the XSLT language. Lists all XSLT elements along with
required and optional attributes and allowable content within each element. Also cross
references each element with the W3C XSLT specification.
Conventions Used in This Book
Italic is used for:
Pathnames, filenames, and program names
New terms where they are defined
Internet addresses, such as domain names and URLs
Constant width is used for:
Zgłoś jeśli naruszono regulamin