Home > Articles

Essential XSLT

This chapter is from the book

This chapter is from the book

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.

W3C and Style Languages

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 on—and 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 XSL—that is, XSL-FO—has 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.

XSLT—XSL 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 you—all 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 needed—and 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 more—all 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 test—such as customers in a particular zip code—and 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 recommendation—the 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 further—it'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.)

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020