Xslt
  • Alternative view 1 of Xslt
  • Alternative view 2 of Xslt

Xslt

3.0 6
by Doug Tidwell
     
 

View All Available Formats & Editions

After years of anticipation and delay, the W3C finally released the XSLT 2.0 standard in January 2007. The revised edition of this classic book offers practical, real-world examples that demonstrate how you can apply XSLT stylesheets to XML data using either the new specification, or the older XSLT 1.0 standard.

XSLT is a critical language for converting XML

Overview

After years of anticipation and delay, the W3C finally released the XSLT 2.0 standard in January 2007. The revised edition of this classic book offers practical, real-world examples that demonstrate how you can apply XSLT stylesheets to XML data using either the new specification, or the older XSLT 1.0 standard.

XSLT is a critical language for converting XML documents into other formats, such as HTML code or a PDF file. With XSLT, you get a thorough understanding of XSLT and XPath and their relationship to other web standards, along with recommendations for a honed toolkit in an open platform-neutral, standards-based environment. This book:

  • Covers the XSLT basics, including simple stylesheets and methods for setting up transformation engines
  • Walks you through the many parts of XSLT, particularly XSLT's template-based approach to transformations
  • Applies both XSLT 1.0 and 2.0 solutions to the same problems, helping you decide which version of XSLT is more appropriate for your project
  • Includes profuse examples that complement both the tutorial and the reference material

The new edition of XSLT has been updated thoroughly to explain XSLT 2.0's many dependencies, notably XML Schema and XPath 2.0. Want to find out how the 2.0 specification improves on the old? This book will explain.

Product Details

ISBN-13:
9780596527211
Publisher:
O'Reilly Media, Incorporated
Publication date:
07/03/2008
Edition description:
Second Edition
Pages:
990
Sales rank:
555,622
Product dimensions:
7.10(w) x 9.10(h) x 1.60(d)

Read an Excerpt

Chapter 5: Creating Links and Cross-References

Contents:

Generating Links with the id() Function
Generating Links with the key() Function
Generating Links in Unstructured Documents
Summary

If you're creating a web site, publishing a book, or creating an XML transaction, chances are many pieces of information will refer to other things. This chapter discusses a several ways to link XML elements. It reviews three techniques:

  • Using the id() function

  • Doing more advanced linking with the key() function

  • Generating links in unstructured documents

Generating Links with the id() Function

Our first attempt at linking will be with the XPath id() function.

The ID, IDREF, and IDREFs Datatypes

Three of the basic datatypes supported by XML Document Type Definitions (DTDs) are ID, IDREF, and IDREFS. Here's a simple DTD that illustrates these datatypes:

<!--glossary.dtd-->
<!--The containing tag for the entire glossary-->
<!ELEMENT glossary  (glentry+) >

<!--A glossary entry-->
<!ELEMENT glentry  (term,defn+) >

<!--The word being defined-->
<!ELEMENT term  (#PCDATA) >
<!--The id is used for cross-referencing, and the 
    xreftext is the text used by cross-references.-->
<!ATTLIST term
               id  ID    #REQUIRED 
               xreftext  CDATA    #IMPLIED  >

<!--The definition of the term-->
<!ELEMENT defn  (#PCDATA | xref | seealso)* >

<!--A cross-reference to another term-->
<!ELEMENT xref   EMPTY  >

<!--refid is the ID of the referenced term-->
<!ATTLIST xref
               refid  IDREF    #REQUIRED >

<!--seealso refers to one or more other definitions-->
<!ELEMENT seealso EMPTY>
<!ATTLIST seealso
                  refids   IDREFS  #REQUIRED >

In this DTD, each <term> element is required to have an id attribute, and each <xref> element must have an refid attribute. The ID and IDREF datatypes work according to two rules:

  • Each value of the id attribute must be unique.

  • Each value of the refid attribute must match a value of an id attribute elsewhere in the document.

To round out our example, the <seealso> element contains an attribute of type IDREFS. This datatype contains one or more values, each of which must match a value of an ID elsewhere in the document. Multiple values, if present, are separated by whitespace.

There are some complications of ID and related datatypes, but we'll discuss them later. For now, we'll focus on how the id() function works.

An XML Document in Need of Links

To illustrate the value of linking, we'll use a small glossary written in XML. The glossary contains some <glentry> elements, each of which contains a single <term> and one or more <defn> elements. In addition, a definition is allowed to contain a cross-reference (<xref>) to another <term>. Here's a short sample document:

<?xml version="1.0" ?>
<!DOCTYPE glossary SYSTEM "glossary.dtd">
<glossary>
  <glentry>
    <term id="applet">applet</term>
    <defn>
      An application program,
      written in the Java programming language, that can be 
      retrieved from a web server and executed by a web browser. 
      A reference to an applet appears in the markup for a web 
      page, in the same way that a reference to a graphics
      file appears; a browser retrieves an applet in the same 
      way that it retrieves a graphics file. 
      For security reasons, an applet's access rights are limited
      in two ways: the applet cannot access the file system of the 
      client upon which it is executing, and the applet's 
      communication across the network is limited to the server 
      from which it was downloaded. 
      Contrast with <xref refid="servlet"/>.
      <seealso refids="wildcard-char DMZlong pattern-matching"/>
    </defn>
  </glentry>

  <glentry>
    <term id="DMZlong" xreftext="demilitarized zone">demilitarized 
      zone (DMZ)</term>
    <defn>
      In network security, a network that is isolated from, and 
      serves as a neutral zone between, a trusted network (for example, 
      a private intranet) and an untrusted network (for example, the
      Internet). One or more secure gateways usually control access 
      to the DMZ from the trusted or the untrusted network.
    </defn>
  </glentry>

  <glentry>
    <term id="DMZ">DMZ</term>
    <defn>
      See <xref refid="DMZlong"/>.
    </defn>
  </glentry>

  <glentry>
    <term id="pattern-matching">pattern-matching character</term>
    <defn>
      A special character such as an asterisk (*) or a question mark 
      (?) that can be used to represent zero or more characters. 
      Any character or set of characters can replace a pattern-matching 
      character.
    </defn>
  </glentry>

  <glentry>
    <term id="servlet">servlet</term>
    <defn>
      An application program, written in the Java programming language, 
      that is executed on a web server. A reference to a servlet 
      appears in the markup for a web page, in the same way that a 
      reference to a graphics file appears. The web server executes
      the servlet and sends the results of the execution (if there are
      any) to the web browser. Contrast with <xref refid="applet" />.
    </defn>
  </glentry>

  <glentry>
    <term id="wildcard-char">wildcard character</term>
    <defn>
      See <xref refid="pattern-matching"/>.
    </defn>
  </glentry>
</glossary>

In this XML listing, each <term> element has an id attribute that identifies it uniquely. Many <xref> elements also refer to other terms in the listing. Notice that each time we refer to another term, we don't use the actual text of the referenced term. When we write our stylesheet, we'll use the XPath id function to retrieve the text of the referenced term; if the name of a term changes (as buzzwords go in and out of fashion, some marketing genius might want to rename the "pattern-matching character," for example), we can rerun our stylesheet and be confident that all references to the new term contain the correct text.

Finally, some <term> elements have an xreftext element because some of the actual terms are longer than we'd like to use in a cross-reference. When we have an <xref> to the term ASCII (American Standard Code for Information Interchange), it would get pretty tedious if the entire text of the term appeared throughout our document. For this term, we'll use the xreftext attribute's value, ensuring that the cross-reference contains the less-intimidating text ASCII....

Meet the Author

Doug Tidwell is a senior programmer at IBM. He has more than a sixth of a century of programming experience, and has been working with markup languages for more than a decade. He was a speaker at the first XML conference in 1997, and has taught XML classes around the world. His job as a Cyber Evangelist is to look busy and to help people use new technologies to solve problems. Using a pair of zircon-encrusted tweezers, he holds a master's degree in computer science from Vanderbilt University and a bachelor's degree in English from the University of Georgia. He lives in Raleigh, North Carolina, with his wife, cooking teacher Sheri Castle (see her web site at http://www.sheri-inc.com) and their daughter Lily.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >

XSLT 3 out of 5 based on 0 ratings. 6 reviews.
Guest More than 1 year ago
Although Tidwell emphasizes a real-world approach to tackling XSLT and includes plenty of practical examples in the book, he doesn't skimp when it comes to delivering the types of conceptual explanations (sections like 'How a Stylesheet is processed' in Chpt 2 and 'The XPath View of an XML Document' in Chpt. 3) that help readers understand what's going on 'under the hood'. Including a separate chapter covering the basics of XPath early in the book also makes for much easier reading, since we aren't left scrounging for scraps of information scattered throughout the text when XPath-related questions arise. If not for the fact that some authors have actually taken the opposite approach (introducing XPath concepts as they arise in the context of a discussion of XSLT), this would have seemed like a no-brainer. No review of this book would be complete without mentioning the value added by the appendices. Once you've digested all of the material in the body of the text, you'll likely continue to keep Tidwell's book close at hand because of Appendices A and C. Appendix A, the XSLT Reference, features a comprehensive dictionary-style reference for every element in XSLT 1.0 - including an XML source document, an example stylesheet that makes use of the element, and the result of the transformation for each. Appendix C, the XSLT and XPath Function Reference, follows a similar format.
Anonymous More than 1 year ago
XSLT and related technology are sufficiently different from typical programming lanuages that they benefit from explanations that are careful, precise, and free of ambiguity. This is not such a book. It is full of careless phrasing, ambiguities, puzzling logic, and poorly chosen analogies. Frequently while reading it I wanted to throw it across the room.
Anonymous More than 1 year ago
Anonymous More than 1 year ago
Anonymous More than 1 year ago
Anonymous More than 1 year ago