1. Trang chủ
  2. » Công Nghệ Thông Tin

XSLT Cookbook pptx

753 117 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Table of Contents Preface 1. Strings 1.1 Testing if a String Ends with Another String 1.2 Finding the Position of a Substring 1.3 Removing Specific Characters from a String 1.4 Finding Substrings from the End of a String 1.5 Duplicating a String N Times 1.6 Reversing a String 1.7 Replacing Text 1.8 Converting Case 1.9 Tokenizing a String 1.10 Making Do Without Regular Expressions 1.11 Using the EXSLT String Extensions 2. Numbers and Math 2.1 Formatting Numbers 2.2 Rounding Numbers to a Specified Precision 2.3 Converting from Roman Numerals to Numbers 2.4 Converting from One Base to Another 2.5 Implementing Common Math Functions 2.6 Computing Sums and Products 2.7 Finding Minimums and Maximums 2.8 Computing Statistical Functions 2.9 Computing Combinatorial Functions 2.10 Testing Bits 3. Dates and Times 3.1 Calculating the Day of the Week 3.2 Determining the Last Day of the Month 3.3 Getting Names for Days and Months 3.4 Calculating Julian and Absolute Day Numbers from a Specified Date 3.5 Calculating the Week Number for a Specified Date 3.6 Working with the Julian Calendar 3.7 Working with the ISO Calendar 3.8 Working with the Islamic Calendar 3.9 Working with the Hebrew Calendar 3.10 Formatting Dates and Times 3.11 Determining Secular and Religious Holidays 4. Selecting and Traversing 4.1 Optimizing Node Selections 4.2 Determining if Two Nodes Are the Same 4.3 Ignoring Duplicate Elements 4.4 Selecting All but a Specific Element 4.5 Performing a Preorder Traversal 4.6 Performing a Postorder Traversal 4.7 Performing an In-Order Traversal 4.8 Performing a Level-Order Traversal 4.9 Processing Nodes by Position 5. XML to Text 5.1 Dealing with Whitespace 5.2 Exporting XML to Delimited Data 5.3 Creating a Columnar Report 5.4 Displaying a Hierarchy 5.5 Numbering Textual Output 5.6 Wrapping Text to a Specified Width and Alignment 6. XML to XML 6.1 Converting Attributes to Elements 6.2 Converting Elements to Attributes 6.3 Renaming Elements or Attributes 6.4 Merging Documents with Identical Schema 6.5 Merging Documents with Unlike Schema 6.6 Splitting Documents 6.7 Flattening an XML Hierarchy 6.8 Deepening an XML Hierarchy 6.9 Reorganizing an XML Hierarchy 7. Querying XML 7.1 Performing Set Operations on Node Sets 7.2 Performing Set Operations on Node Sets Using Value semantics 7.3 Determining Set Equality by Value 7.4 Performing Structure-Preserving Queries 7.5 Joins 7.6 Implementing the W3C XML Query-Use Cases in XSLT 8. XML to HTML 8.1 Using XSLT as a Styling Language 8.2 Creating Hyperlinked Documents 8.3 Creating HTML Tables 8.4 Creating Frames 8.5 Creating Data-Driven Stylesheets 8.6 Creating a Self-Contained HTML Transformation 8.7 Populating a Form 9. XML to SVG 9.1 Transforming an Existing Boilerplate SVG 9.2 Creating Reusable SVG Generation Utilities for Graphs and charts 9.3 Creating a Tree Diagram 9.4 Creating Interactive SVG-Enabled Web Pages 10. Code Generation 10.1 Generating Constant Definitions 10.2 Generating Switching Code 10.3 Generating Message-Handling Stub Code 10.4 Generating Data Wrappers 10.5 Generating Pretty Printers 10.6 Generating a Test Data-Entry Web Client 10.7 Generating Test-Entry Web CGI 10.8 Generating Code from UML Models via XMI 10.9 Generating XSLT from XSLT 11. Vertical XSLT Application Recipes 11.1 Converting Visio VDX Documents to SVG 11.2 Working with Excel XML Spreadsheets 11.3 Generating XTM Topic Maps from UML Models via XMI 11.4 Generating Web Sites from XTM Topic Maps 11.5 Serving SOAP Documentation from WSDL 12. Extending and Embedding XSLT 12.1 Using Saxon's and Xalan's Native Extensions 12.2 Extending XSLT with JavaScript 12.3 Adding Extension Functions Using Java 12.4 Adding Extension Elements Using Java 12.5 Using XSLT from Perl 12.6 Using XSLT from Java 13. Testing and Debugging 13.1 Using xsl:message Effectively 13.2 Tracing the Flow of Your Stylesheet Through Its Input document 13.3 Automating the Insertion of Debug Output 13.4 Including Embedded Unit Test Data in Utility Stylesheets 13.5 Structuring Unit Tests 13.6 Testing Boundary and Error Conditions 14. Generic and Functional Programming 14.1 Creating Polymorphic XSLT 14.2 Creating Generic Element Aggregation Functions 14.3 Creating Generic Bounded Aggregation Functions 14.4 Creating Generic Mapping Functions 14.5 Creating Generic Node-Set Generators Index Preface Extensible Stylesheet Language Transformations (XSLT) is a powerful technology for transforming XML documents into other useful forms, but it is sometimes considered difficult to learn. Its template-based approach makes it a prime candidate for learning by example, and XSLT examples are often easily repurposed. When I first began working with XSLT, I longed for a cookbook that would accelerate my productivity by providing ready-made solutions to the challenges I faced. My first experience with such a book was O'Reilly's Perl Cookbook. This book was more influential to my reluctant learning and ultimate appreciation of Perl than the original camel book (Programming Perl) by Larry Wall. I believe cookbooks are important because most software developers are not satisfied with simply figuring out how to make something work: they are interested in mastering the technology and using the best-known techniques, and they want answers fast. There is no better way to master a subject than by borrowing from those who already discovered better ways to do things. Longing for a cookbook soon turned into a desire to write one, especially since I collected several useful recipes—some that were developed by others and some that I created. However, I did not want to write an XSLT book simply packaged in an alternate form; I wanted to provide a useful resource that also highlighted some less-obvious ways to apply XSLT. In the process, I hoped to attract XML developers who have not yet been motivated to learn XSLT and who, in my opinion, are missing out on one of XML's best productivity tools. If you are one of these folks who has not yet experienced XSLT, please bear with me for a few more paragraphs while I pitch the value of XSLT and the role of this book in helping you realize its potential. XSLT is a language that lives simultaneously on the fringes and in the mainstream of current software-development technology. While working on this project, I often found myself explaining to friends what XSLT was and why it was important enough to spend time writing a whole book about it. These same friends have heard of Java, Perl, and even XML, but not XSLT. I also observed an increasing number of requests for XSLT assistance on XSLT mailing lists and more industry attention in the form of books, articles, and sophisticated XSLT development tools. The XSLT user base is clearly growing daily; however, many software professionals and technology enthusiasts do not understand what it is and why it is important. I would estimate that more that half of all companies and individuals working with XML do not use XSLT. Not so long ago, a colleague who is otherwise well-versed in all the latest technologies described XSLT as just another styling language. One can certainly forgive such a blatant misunderstanding because XSLT advertises itself through the first three words in its name (Extended Stylesheet Language) and with the keyword that begins most XSLT programs (xsl:stylesheet). However, the last word in the XSLT acronym, Transformations, is what makes XSLT so important and is what drew me to the language in the first place. One of my goals in writing this book is to show how XSLT is relevant to a wide variety of problems. I also want to provide both novice and intermediate users of XSLT a one-stop shopping place for some of the most commonly requested XSLT techniques. Finally, I want to push the envelope of what one can do with XSLT so current users can go even further and the unconvinced can join the fold of highly productive XML transformers. Over the years, I have heard many sweeping statements about computer science. Opinions like, "All computation is simply fancy bit manipulation," "Computers are really just sophisticated number crunchers," or "Everything a computer does can be understood in terms of symbol manipulation" are true to some extent. However, I would like to make a sweeping generalization of my own: "Every problem we solve with software can be understood in terms of transformations." Mastery of computer science is mastery of transformation. Transformation is what CPUs do, it is what algorithms do, and it is what software developers do. And transformation is what XSLT does, at least when the input is XML (and sometimes when it is not). Of course, XSLT is not the only transformational game in town, and as with the thousands of languages that came before it, it is unclear whether it will evolve as an independent language or be absorbed into the next "big thing." What is clear is that the ideas behind XSLT will not go away because many of these ideas are as old as computer science itself. This book helps the reader master and apply these ideas to specific problems. Structure of This Book One of transformation's most primitive forms is the transformation of character sequences otherwise known as strings. Unlike the ancient language SNOBOL or the relatively modern Perl, XSLT was not specifically designed with string manipulation in mind. However, Chapter 1 shows that almost anything one wants to do with strings can be done within the confines of XSLT. Numerical transformation (commonly referred to as mathematics) is another crucial form of low- level transformation that pervades all software development simply because measurement and counting pervades life itself. Chapter 2 shows how to push the limits of XSLT's mathematical capabilities even though XSLT was not designed to be the next great Fortran replacement. Manipulating dates and times is a quintessentially human activity and a large part of our technological progress has been driven by an obsession with clocks, calendars, and accurate forecasting. Chapter 3 contains date and time recipes that augment an area standard XSLT currently lacks. This chapter does not cover XSLT per se. However, it presents fascinating and difficult problems arising in date conversion and transformation, ready-made XSLT solutions, and important links to external date- and calendar-related resources. All transformations begin by identifying the target you want to transform. If that target is a compound object, you need to traverse the objects constituent parts as the transformation proceeds. Chapter 4 covers these topics and explores the problems XSLT was specifically designed to solve. This chapter describes XML as a tree and shows how XSLT can manipulate such trees. It also provides pointers for getting the best performance out of XML processing tasks. Before there were word processors, HTML, PDF, or other forms of sophisticated textual presentation, there was plain old text. The problem of transforming data used for computer consumption to data organized for human consumption is important. When the source data is XML, then the problem is perfect for XSLT. Chapter 5 provides recipes that control how text extracted from XML is rendered for layout on the terminal, on the text editor, or for import to programs that require delimited data, such as comma-separated values. XML is quickly becoming the universal syntax for information transfer, and there is every indication that this trend will accelerate rather than abate. Therefore, a vast amount of XML transformation has XML as the destination as well as the source. Chapter 6 covers these types of transformations. It shows how XML documents can be split, merged, flattened, cleaned up, and otherwise reorganized with relatively little XSLT code. Much of transformation simply extracts information from raw data to answer questions. Chapter 7 presents a treasure trove of recipes that demonstrate XSLT as a query language. It provides solutions to a wide variety of query-use cases that will probably resemble queries you'll need to ask of your own XML data. HTML is an important target of XSLT transformation. Chapter 8 demonstrates solutions to problems that arise when generating web content, including links, tables, frames, forms, and other client-side transformation issues. Graphics programming transforms data to the visual domain. You would not think of XSLT as a graphics programming language, and it is not. However, when Scalable Vector Graphics (SVG) is the target of the transformation, XSLT can achieve impressive results. Chapter 9 describes the transformation of raw data into bar charts, pie charts, line plots, and other graphical components. It also covers the transformation of XML to a hierarchical tree diagram. This chapter emphasizes how transformations are structures that can be mixed and matched to create many different outputs. Generating code is an automation task that I have always been interested in. Of all the transformations, humans still do this one best (lucky for us who make a living at it). However, sometimes it is better to write a program that generates code rather write the code ourselves. Chapter 10 shows the advantage gained from representing the data that drives code generation in XML and illustrates how XSLT is ideal for writing code generators for C++, Java, and XSLT itself. The chapter also includes a code-generation recipe taken from a design pattern represented in UML via XMI. XSLT can enable some sophisticated applications. Chapter 11 includes some advanced uses of XSLT. The chapter is an eclectic mix that includes Visio VDX to SVG conversion, Microsoft Excel XML transformation, topic maps, and WSDL processing. Although XSLT is powerful in its own right, we can really do some wicked things with extensions or by embedding it in programs written in other languages. Chapter 12 provides extensive coverage of XSLT extensibility using Java and JavaScript. It also shows how XSLT can be used within Perl and Java programs. Testing and debugging are essential to any software development effort, and XSLT development is no exception. Chapter 13 demonstrates useful techniques that can help you transform misbehaved XSLT programs into functional ones even if you don't have a native XSLT debugger handy. Chapter 14 pushes the XSLT envelope to show how XSLT is far more than just another styling language. This chapter focuses on using XSLT as a generic and functional programming language. If nothing else, this chapter will open your eyes and stimulate your thoughts on the power of XSLT and how it can be used to create generic solutions. [...]... bit of common string manipulation can be done within the confines of XSLT Advanced string capabilities are implemented in XSLT by combining the capabilities of the native string functions and by exploiting the power of recursion, which is an integral part of all advanced uses of XSLT In fact, recursion is such an important technique in XSLT that it is worthwhile to look through some of these recipes even... of these recipes even if you have no intention of implementing your string-processing needs directly in XSLT This book also refers to the excellent work of EXSLT.org, a community initiative that helps standardize extensions to the XSLT language You may want to check out their site at http://www.exslt.org 1.1 Testing if a String Ends with Another String 1.1.1 Problem You need to test if a string ends... than [a] string —Scott Adams When it comes to manipulating strings, XSLT certainly lacks the heavy artillery of Perl XSLT is a language optimized for processing XML markup, not strings However, since XML is simply a structured form of text, string processing is inevitable in all but the most trivial transformation problems Unfortunately, XSLT has only nine standard functions for string processing Java,... engine XSLT programmers have two choices when they need to perform advanced string processing First, they can call out to external functions written in Java or some other language supported by their XSLT processor This choice is wise if portability is not an issue and fairly heavy-duty string manipulation is needed Second, they can implement the advanced string-handling functionality directly in XSLT. .. 1.4.3 Discussion Both XSLT string-searching functions ( substring-before and substring-after) begin searching at the start of the string Sometimes you need to search a string from the end The simplest way to do this in XSLT is to apply the built-in search functions recursively until the last instance of the substring is... substring( ), which may be slow on some XSLT implementations See Recipe 1.7 for an explanation It does have an advantage for processors that do not optimize tail recursion since it reduces the number of recursive calls significantly 1.5.4 See Also The so-called Piez Method can also duplicate a string without recursion This method is discussed at http://www.xml.org/xml /xslt_ efficient_programming_techniques.pdf... any other way because XPath and XSLT, unlike C, do not allow special characters to be escaped with a backslash (\) XPath 2.0 will allow the quotes to be escaped by doubling them up 1.7.3 Discussion Searching and replacing is a common text-processing task The solution shown here is the most straightforward implementation of search and replace written purely in terms of XSLT When considering the performance... matches, I failed to cause a stack overflow My next suspicion was that for some reason, XSLT substring( ) is actually slower than the substring-before( ) and substring-after( ) calls Michael Kay, the author of Saxon, indicated that Saxon's implementation of substring( ) was slow due to the complicated rules that XSLT substring must implement, including floating-point rounding of arguments, handling... optimization is tricky business, especially in XSLT where there can be a wide disparity between implementations and where new versions continually apply new optimizations Unless you are prepared to profile frequently, it is best to stick with simple solutions An added advantage of obvious solutions is that they are likely to behave consistently across different XSLT implementations 1.8 Converting Case... which the lowercase "ß" is converted to an uppercase "SS" Many modern programming languages provide case-conversion functions that are sensitive to locale, but XSLT does not support this concept directly This is unfortunate, considering that XSLT has other features supporting internationalization A slight improvement can be made by defining general XML entities for each type conversion, as shown in . number of requests for XSLT assistance on XSLT mailing lists and more industry attention in the form of books, articles, and sophisticated XSLT development tools. The XSLT user base is clearly. you transform misbehaved XSLT programs into functional ones even if you don't have a native XSLT debugger handy. Chapter 14 pushes the XSLT envelope to show how XSLT is far more than just. Test-Entry Web CGI 10.8 Generating Code from UML Models via XMI 10.9 Generating XSLT from XSLT 11. Vertical XSLT Application Recipes 11.1 Converting Visio VDX Documents to SVG 11.2 Working

Ngày đăng: 27/06/2014, 08:20

Xem thêm

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN