XSLT: Elements and Attributes

The most common XSLT element is the xsl:template element that defines a template in an XSLT style sheet. In this style sheet, the xsl:template element is typically associated with a match attribute. A match attribute associates a template with an element in an XML document. A match attribute takes an XPath expression as its value.

Another commonly used element is the xsl:value-of element. It extracts the contents of an element defined in an XML document. The xsl:value-of element is associated with the select attribute that takes an XPath expression as a value to locate an element in the XML document.

The code snippet on the right displays the XSLT style sheet that uses the xsl:template and xsl:value-of elements to transform the following XML document into an HTML document:

  <Book>
    <title>BookName</title>
    <author>AuthorName</author>
  </Book>

After creating an XSLT style sheet, you need to use an XSLT processor. An XSLT processor is a program that transforms the XML document according to the rules specified in the XSLT style sheet. An example of a commonly used XSLT processor is Xalan-Java, which implements the javax.xml.transform interface in Java API for XML Processing (JAXP).

The graphic displays the output of the preceding transformation.

<?xml version="1.0" encoding="ISO-8859-1"?>
  <xsl:stylesheet version = '1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    <xsl:template match="/">
      <h1> <xsl:value-ofselect="//title"/> </h1>
      <h2> <xsl:value-of select="//author"/> </h2>
    </xsl:template>
  </xsl:stylesheet>

The graphic shows the output of the XSLT transformation in a browser window.

Click the Next button to continue.