- Chapter 1: Essential XSLT
- A Little Background
- XML Documents
- What Does XML Look Like in a Browser?
- XSLT Transformations
- Making an XSLT Transformation Happen
- Using Standalone XSLT Processors
- Using Browsers to Transform XML Documents
- Using XSLT and JavaScript in the Internet Explorer
- XSLT Transformations on Web Servers
- XML-to-XHTML Transformations
- XSLT Resources
- XSL Formatting Objects: XSL-FO
- XSL-FO Resources
- Formatting an XML Document
- The XSLT Stylesheet
- Transforming a Document into FormattingObject Form
- Creating a Formatted Document
A Little Background
XSL itself is a creation of the World Wide Web Consortium (W3C, http://www.w3.org), a coalition of groups originally founded by Tim Berners-Lee. The W3C is the body that releases the specifications, such as those for XSL, that are used in this book. They make XML and XSL what they are.
You can read about the history of W3C's work with style languages at http://www.w3.org/Style/History. It's interesting to see how much work has gone onand how much style languages have changed over the years.
The W3C originally developed the grandfather of XML, SGML (Standard Generalized Markup Language), in the 1980s, but it was too complex to find much use, and in fact, XML (like HTML) is a simplified version of SGML. The W3C also created a style language called DSSSL (for Document Style Semantics and Specification Language) for use with SGML, and in the same way that XML was derived from SGML, XSL is based on the original DSSSL. As the W3C says: "The model used by XSL for rendering documents on the screen builds upon many years of work on a complex ISO-standard style language called the Document Style Semantics and Specification Language (DSSSL)."
However, the original part of XSLthat is, XSL-FOhas not proven easy enough to find widespread use yet either, so XSLT was introduced to make it easier to convert XML documents to XSL-FO form. As it turns out, XSLT is what has really taken off, because it provides a complete transformation language that enables you to work with the contents of XML documents without writing programming code, transforming those documents into another XML document, HTML, or other text-based formats. The big success story here, surprising even the W3C, is XSLT.
XSLTXSL Transformations
XSLT lets you work with the contents of XML documents directly. For example, you might have a huge XML document that holds all baseball statistics for the most recent baseball season, but you might be interested only in the statistics for pitchers. To extract the data on pitchers, you can write your own program in Java, Visual Basic, or C++ that works with XML parsers. Parsers are special software packages that read XML documents and pass all the data in the document, piece by piece, to your own code. You can then write a new XML document, pitchers.xml, that contains only data about pitchers.
That way of doing things works, but it involves quite a bit of programming, as well as the investment of a lot of time and testing. XSLT was invented to solve problems such as this. XSLT can be read by XSLT processors, which work on XML documents for youall you have to do is create an XSLT stylesheet that specifies the rules you want to apply to transform one document into another. No programming is neededand that's what makes it attractive to many people, even experienced programmers. For the baseball example, all you'd have to do is write an XSLT stylesheet that specifies what you want to do, and let the XSLT processor do the rest.
Besides transforming one XML document into another XML document, you can also transform XML documents into other types of documents, such as HTML documents, rich text (RTF) documents, documents that use XSL-FO, and others. You can also transform XML documents into other XML-based languages, such as MathML, MusicML, VML, XHTML, and moreall without programming.
In many ways, XSLT can function like a database language such as SQL (Structured Query Language, the famous database-access language), because it enables you to extract the data you want from XML documents, much like applying an SQL statement to a database. Some people even think of XSLT as the SQL of the Web, and if you're familiar with SQL, that gives you some idea of the boundless horizons available to XSLT. For example, using an XSLT stylesheet, you can extract a subset of data from an XML document, create an entire table of contents for a long document, find all elements that match a specific testsuch as customers in a particular zip codeand so on. And you can do it all in one step!
XSL-FO: XSL Formatting Objects
The other part of XSL is XSL-FO, which is the formatting language part of XSL, and you'll get a taste of XSL-FO in this book. You can use XSL-FO to specify how the data in XML documents is to be presented, down to the margin sizes, fonts, alignments, header and footer size, and page width. When you're formatting an XML document, there are hundreds of items to think about, and accordingly, XSL-FO is much bigger than XSLT.
On the other hand, because of its very complexity, XSL-FO is not very popular yet, certainly not compared to XSLT. There's not much software that supports XSL-FO at this point, and none that implements anywhere near the complete standard. Just as the most common use of XSLT is to transform XML to HTML, the most common use of XSL-FO is to convert XML to formatted PDF (Portable Data Format), the format used by the Adobe Acrobat. You'll see an example of that at the end of this chapter, as well as in Chapter 11.
The W3C Specifications
W3C releases the specifications for both XML and XSL, and those specifications are what we'll be working with in this book. W3C specifications arenot called standards because by international agreement, standards are created only by government-approved bodies. Instead, the W3C starts by releasing the requirements for a new specification. The requirements are goals, and list a sort of preview of what the specification will be all about, but the specification isn't written at that point. Next, the W3C releases specifications first as working drafts, which anyone may comment on, then as candidate recommendations, which are still subject to review, and then finally as recommendations, which are final.
The following list includes the XSLT-related W3C specifications that we'll be using in this book and where you can find them:
The complete XSL candidate recommendation http://www.w3.org/TR/xsl/. This is the big document that specifies all there is to XSL.
The XSL Transformations 1.0 recommendation http://www.w3.org/TR/xslt. XSLT's function is to transform the contents of XML documents into other documents, and it's what's made XSL so popular.
The XSLT 1.1 working draft http://www.w3.org/TR/xslt11. This is the XSLT 1.1 working draft, which will not be upgraded into a recommendationthe W3C plans to add the XSLT 1.1 functionality to XSLT 2.0.
The XSLT 2.0 requirements http://www.w3.org/TR/xslt20req. W3C has released the set of goals for XSLT 2.0, including more support forXML schemas.
The XPath 1.0 specification http://www.w3.org/TR/xpath. You use XPath to locate and point to specific sections and elements in XML documents so that you can work with them.
The XPath 2.0 requirements http://www.w3.org/TR/xpath20req. XPath is being updated to offer more support for XSLT 2.0.
XSLT Versions
The specifications for XSLT have been considerably more active than the specifications for XSL as a whole. The XSLT 1.0 recommendation was made final November 16, 1999, and that's the version that forms the backbone of XSLT today.
Next came the XSLT 1.1 working draft, and although it was originally intended to go on and become a new recommendation, some people in the W3C started working on XSLT 2.0; after a while, the W3C decided to cancel the XSLT 1.1 recommendation. This means that the XSLT 1.1 working draft is not going to go any furtherit'll always stay in working draft form and will not become a recommendation. In other words, there will be no official version 1.1 of XSLT.
However, the W3C also says that it plans to integrate much of what was done in the XSLT 1.1 working draft into XSLT 2.0, and for that reason, I'll take a look at the XSLT 1.1 working draft in this book. I'll be sure to label material as "XSLT 1.1 working draft only" when we're discussing something new that was introduced in the XSLT 1.1 working draft.
Here are the changes from XSLT 1.0 that were made in the XSLT 1.1 working draft; note that this list is included just for reference, because most of this material probably won't mean anything to you yet:
The result tree fragment data type, supported in XSLT 1.0, was eliminated.
The output method no longer has complete freedom to add namespace nodes, because a process of namespace fixup is applied automatically.
Support for XML Base was added.
Multiple output documents are now supported with the <xsl:document> element.
The <xsl:apply-imports> element is now allowed to have parameters.
Extension functions can now be defined using the <xsl:script> function.
Extension functions are now allowed to return external objects, which do not correspond to any of the XPath data types.
This book covers the XSLT 1.0 recommendation, as well as the XSLT 1.1 working draft. In fact, the W3C has raced ahead and has released the requirements for XSLT 2.0, and we'll also cover what's known of XSLT 2.0 in this book. The following list gives you an overview of the goals for XSLT 2.0:
- Add more support for the use of XML Schema-typed content with XSLT.
- Simplify manipulation of string content.
- Make it easier to use XSLT.
- Improve internationalization support.
- Maintain backward compatibility with XSLT 1.0.
- Support improved processor efficiency.
Although XSLT 2.0 won't be out for quite a while yet, I'll cover all that's known about it so far when we discuss pertinent topics. For example, the W3C's successor for HTML is the XML-based XHTML. In XSLT 1.0 the XSLT 1.1 working draft, there is no special support for XML to XHTML transformations, so we'll have to create that transformation from scratch. However, that support is coming in XSLT 2.0, and I'll mention that fact when we discuss XHTML.
That provides us with an overview, and sets the stage. Now it's time to get to work. XSL is designed to work on XML documents, so I'm going to review the structure of XML documents first. You'll be working on XML documents, but XSL stylesheets themselves are actually XML documents as well, which is something you have to keep in mind as you write them. This book assumes that you have some knowledge of both XML and HTML. (As mentioned earlier, this book is the companion volume to New Rider's Inside XML.)