没有合适的资源?快使用搜索试试~ 我知道了~
Java and XSLT.pdf
4星 · 超过85%的资源 需积分: 10 109 下载量 105 浏览量
2008-09-28
21:31:01
上传
评论
收藏 2.91MB PDF 举报
温馨提示
试读
333页
XSLT提供了一种将XML文档转换为HTML的强有力的工具.然而,当这种转换需要涉及更多逻辑的时候,就会显现出它的不足之处.文中讲述了如何使用Java扩展XSLT,从而更好地发挥两种语言的特色.最后给出一实例来具体展示如何将XML节点传送到Java类并返回到样式表以进一步处理.
资源推荐
资源详情
资源评论
Copyright © 2001 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.
Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered
trademarks of O'Reilly & Associates, Inc. The association between the image of an ermine and
Java and XSLT is a trademark of O'Reilly & Associates, Inc. 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 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.
Preface....................................................................................................................................................3
Audience............................................................................................................................................ 3
Software and Versions....................................................................................................................... 3
Organization.......................................................................................................................................4
Conventions Used in This Book........................................................................................................ 5
How to Contact Us.............................................................................................................................6
Acknowledgments..............................................................................................................................6
Chapter 1. Introduction ......................................................................................................................... 7
1.1 Java, XSLT, and the Web............................................................................................................ 7
1.2 XML Review................................................................................................................................9
1.3 Beyond Dynamic Web Pages..................................................................................................... 22
1.4 Getting Started........................................................................................................................... 24
1
1.5 Web Browser Support for XSLT............................................................................................... 26
Chapter 2. XSLT Part 1 -- The Basics................................................................................................. 26
2.1 XSLT Introduction..................................................................................................................... 27
2.2 Transformation Process..............................................................................................................32
2.3 Another XSLT Example, Using XHTML..................................................................................36
2.4 XPath Basics.............................................................................................................................. 40
2.5 Looping and Sorting...................................................................................................................45
2.6 Outputting Dynamic Attributes..................................................................................................47
Chapter 3. XSLT Part 2 -- Beyond the Basics......................................................................................49
3.1 Conditional Processing...............................................................................................................49
3.2 Parameters and Variables........................................................................................................... 53
3.3 Combining Multiple Stylesheets................................................................................................ 59
3.4 Formatting Text and Numbers................................................................................................... 61
3.5 Schema Evolution...................................................................................................................... 64
3.6 Ant Documentation Stylesheet...................................................................................................68
Chapter 4. Java-Based Web Technologies...........................................................................................77
4.1 Traditional Approaches..............................................................................................................77
4.2 The Universal Design.................................................................................................................84
4.3 XSLT and EJB........................................................................................................................... 90
4.4 Summary of Key Approaches.................................................................................................... 92
Chapter 5. XSLT Processing with Java................................................................................................93
5.1 A Simple Example..................................................................................................................... 93
5.2 Introduction to JAXP 1.1........................................................................................................... 97
5.3 Input and Output...................................................................................................................... 104
5.4 Stylesheet Compilation............................................................................................................ 120
Chapter 6. Servlet Basics and XSLT..................................................................................................124
6.1 Servlet Syntax.......................................................................................................................... 124
6.2 WAR Files and Deployment.................................................................................................... 127
6.3 Another Servlet Example......................................................................................................... 131
6.4 Stylesheet Caching Revisited................................................................................................... 148
6.5 Servlet Threading Issues.......................................................................................................... 151
Chapter 7. Discussion Forum.............................................................................................................154
7.1 Overall Process.........................................................................................................................155
7.2 Prototyping the XML............................................................................................................... 156
7.3 Making the XML Dynamic...................................................................................................... 173
7.4 Servlet Implementation............................................................................................................ 195
7.5 Finishing Touches.................................................................................................................... 210
Chapter 8. Additional Techniques......................................................................................................212
8.1 XSLT Page Layout Templates................................................................................................. 212
8.2 Session Tracking Without Cookies..........................................................................................219
8.3 Identifying the Browser............................................................................................................223
8.4 Servlet Filters........................................................................................................................... 225
8.5 XSLT as a Code Generator...................................................................................................... 232
8.6 Internationalization with XSLT............................................................................................... 235
Chapter 9. Development Environment, Testing, and Performance....................................................247
9.1 Development Environment...................................................................................................... 247
9.2 Testing and Debugging............................................................................................................ 258
9.3 Performance Techniques..........................................................................................................275
Chapter 10. Wireless Applications.....................................................................................................285
10.1 Wireless Technologies........................................................................................................... 285
2
10.2 The Wireless Architecture......................................................................................................286
10.3 Java, XSLT, and WML.......................................................................................................... 287
10.4 The Future of Wireless...........................................................................................................307
Appendix A. Discussion Forum Code............................................................................................... 307
Appendix B. JAXP API Reference.................................................................................................... 318
Appendix C. XSLT Quick Reference................................................................................................ 327
Colophon............................................................................................................................................332
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
3
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 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.
4
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:
• Anything that appears literally in a Java program, including keywords, datatypes, constants, method
names, variables, class names, and interface names
• All Java code listings
• HTML, XML, and XSLT documents, tags, and attributes
Constant width italic is used for:
5
剩余332页未读,继续阅读
wssmao
- 粉丝: 11
- 资源: 97
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页