Chapter 8: XSLT


Every tradesman's tool case has one tool that is a little more worn than the others. It is the favorite tool, the "go to" tool that gets used when all other tools have failed, or maybe even the first tool for all problems. For XML developers, that tool is often XSLT (eXtensible Stylesheet Language Transformations). XSLT is a templating language that can be used to convert XML into something else. The result of the transformation can be XML, HTML, XHTML, or even plain text or binary. XSLT is a powerful tool and, like many powerful tools, it has a few sharp edges you should avoid. As XSLT is a functional language, it can seem a little alien at first for developers used to procedural languages such as C# or Java. In addition, XSLT has limited support for variables and conditional logic than either of those languages. This chapter shows you how you can use XSLT in your applications and avoid its potential problems. Examples will show how you can use XSLT standalone, or combined with other programming languages and tools.

What Is XSLT?

XSLT is a transformation language for XML. Its purpose is to take a source tree of XML nodes and convert them into a result by using a series of templates or rules. XSLT is itself an XML syntax (see Listing 8-1). The result of an XSLT transformation does not have to be XML, however. The XSLT specification allows the output to be XML, HTML, or text. In addition, you can target some other form of output, perhaps even binary content.

Listing 8-1: Sample XSLT

image from book
      <?xml version="1.0" encoding="UTF-8"?>      <xsl:stylesheet version="1.0"      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>        <xsl:template match="/">          <contacts>          <xsl:apply-templates />          </contacts>                </xsl:template>          <xsl:template match="customer">          <cust>            <xsl:attribute name="id">              <xsl:value-of select="generate-id(contact/name)" />            </xsl:attribute>            <xsl:attribute name="company">              <xsl:value-of select="company" />            </xsl:attribute>            <xsl:apply-templates select="contact" />          </cust>          </xsl:template>          <xsl:template match="contact" name="contact">          <xsl:attribute name="name"><xsl:value-of select="name" /></xsl:attribute>          </xsl:template>      </xsl:stylesheet> 
image from book

Just as an individual program is composed of a number of classes or modules, an XSLT stylesheet is composed of multiple templates. Each forms a logical subroutine that creates part of the output.

XSLT has less in common with traditional programming languages such as C# or Java and more in common with other declarative languages such as SQL. To program in XSLT, you must create a description of the output based on the source data.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net