web analytics

What is the difference between XSL and XSLT?

Options

codeling 1599 - 6654
@2016-01-20 13:17:07

XML documents have structure but no format. Extensible Stylesheet Language (XSL) adds formatting to XML documents.

XSL is a family of recommendations for defining XML document transformation and presentation. It consists of three parts:

  • XSL Transformations (XSLT): a language for transforming XML documents
  • The XML Path Language (XPath): an expression language used by XSLT to access or refer to parts of an XML document.
  • XSL Formatting Objects: An XML vocabulary for specifying formatting semantics

Informally, abbreviation "XSL" often refers to XSLT only, since the latter is in wider use. Also informally XSLT often refers to both XSLT itself and XPath, since most of XSLT document use XPath expressions.

@2016-01-20 13:27:42

The XSL specification defines XSL as a language for expressing stylesheets. Given a class of arbitrarily structured XML documents or data files, designers use an XSL stylesheet to express their intentions about how that structured content should be presented; that is, how the source content should be styled, laid out, and paginated in a presentation medium, such as a window in a Web browser or a hand-held device, or a set of physical pages in a catalog, report, pamphlet, or book. Formatting is enabled by including formatting semantics in the result tree.

Formatting semantics are expressed in terms of a catalog of classes of formatting objects. The nodes of the result tree are formatting objects. The classes of formatting objects denote typographic abstractions such as page, paragraph, table, and so forth.

Finer control over the presentation of these abstractions is provided by a set of formatting properties, such as those controlling indents, word and letter spacing, and widow, orphan, and hyphenation control. In XSL, the classes of formatting objects and formatting properties provide the vocabulary for expressing presentation intent.

@2016-01-21 14:56:43

XSLT (Extensible Stylesheet Language Transformations) is a declarative, XML-based language used for the transformation of XML documents into other XML documents. An XSLT stylesheet specifies the presentation of a class of XML documents by describing how an instance of the class is transformed into an XML document that uses a formatting vocabulary, such as (X)HTML or XSL-FO.

The XSLT processing model involves:

  • one or more XML source documents;
  • one or more XSLT stylesheet modules;
  • the XSLT template processing engine (the processor); and
  • one or more result documents.

The XSLT processor is provided with two tree structures to walk through. The first is the structure for the source XML document and the second is the XSLT document itself. After these two structures are provided, the XSLT processor attempts to match element or attribute names found in the XML document with templates contained in the XSLT tree structure. This matching process uses XPath expressions that are embedded within the XSLT document. When a node found within the XML document matches a template in the XSLT document, that template is processed.

Processing of templates found within an XSLT document normally starts with a template that matches the root node of the XML document and proceeds down to its children. When a template is processed, the output is added to the third tree structure mentioned earlier that is used in building the output document.

@2016-01-21 14:59:23

When we write XSLT stylesheets, we’ll use XSLT which defines a set of primitives used to describe a document transformation to tell the processor what to do, and we’ll use XPath which defines a syntax for describing locations in XML documents to tell the processor what document to do it to.

Stylesheet Structure

The general structure of an XSLT stylesheet looks like this:

<xsl:stylesheet version="1.0"
  xmlns:xsl= "http://www.w3.org/1999/XSL/Transform">

  <!-- optional top-level elements, such as: -->
  <xsl:import href="..."/>
  <xsl:param name="..."/>

  <!-- set of template rules: -->
  <xsl:template match="...">...</xsl:template>
  <xsl:template match="...">...</xsl:template>
  ...

</xsl:stylesheet>

The document, or root, element of the stylesheet is xsl:stylesheet. Alternatively, you can use the xsl:transform element, which behaves exactly the same way. Which you use is a matter of personal preference. The XSLT namespace is http://www.w3.org/1999/XSL/Transform. The conventional namespace prefix is xsl, but any prefix can be used—provided that it binds to the XSLT namespace URI.

Example

Here’s an XSLT stylesheet that defines how to transform the XML document:

<?xml version="1.0"?>
<!-- greeting.xsl -->
<xsl:stylesheet version="1.0" xmlns:xsl= "http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>

  <xsl:template match="/">
   <xsl:apply-templates select="greeting"/>
  </xsl:template>

  <xsl:template match="greeting">
    <html>
     <body>
      <h1>
        <xsl:value-of select="."/>
      </h1>
     </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

It can transform the following XML document into something we can view in an ordinary household browser:

<!-- greeting.xml -->
<greeting>
Hello, World!
</greeting>

if you’re using Microsoft’s MSXSL, type this command:

msxsl greeting.xml greeting.xsl -o greeting.html

This command transforms the document greeting.xml, using the templates found in the stylesheet greeting.xsl. The results of the transformation are written to the file greeting.html.

<html>
<body>
<h1>
Hello, World!
</h1>
</body>
</html>

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com