Categories
XSL XSL Languages

XSLT

XSL is the programming language used for creating XSL style sheets, for:

  • creating new documents of various types, based on the content in source XML documents,
  • processing the data they contain, and
  • applying customizations (not to be confused with format styling).

XSLT is the most important part of XSL. It uses XPath to navigate the XML tree structure (nodes), to find and access the XML content (node sets).

XSLT Stylesheet Elements

Element Description
<xsl:template match=”/”/> Defines a template to match a node from the input stream (making it the current element).
<xsl:value-of select=”title”/> Defines the node to match, and then extracts its value.
<xsl:for-each select=”catalog/cd”/> Defines a collection of nodes to match, and then loops through them.
<xsl:sort select=”artist”/> Used to sort the output. Meant to be used within the <xsl:for-each/> loop.
<xsl:if test=”price &gt; 10”/> Used to refine the match by filtering-out content.
<xsl:choose/> Used to refine the match in specific cases (by filtering-out content).
<xsl:when test=”price &gt; 10”/>  
<xsl:otherwise/>  
<xsl:apply-templates/> Applies a template to the current element or to the current element’s child nodes.
Include vs. Import

Both of these directives bring more templates into the current stylesheet. The difference between them lies in how conflicts are handled when both stylesheets contain templates that the exact same node.

<xsl:include href=”fixup_href.xsl”/> Use include when you want imported stylesheet’s templates to be used.

<xsl:import href=”common.xsl”/> Use import when you want the current stylesheet’s templates to be used. I.e., imported stylesheets are always overridden. An import element must always be a top-level element, and must always come before any other elements.

XSL Parameters

A template takes a paramater when you declare a parameter, but you don’t initialize it.
E.g., <xsl:param name=”tableTitle”/>

Modules

A module is an XSL file that you intend to include in the main stylesheet.

Each module defines generic XSLT parameters and templates that can be used from different stylesheets.

E.g.,

<xsl:import href=”params.xsl”/> <xsl:import href=”common-code.xsl”/> <xsl:import href=”page-layout.xsl”/>

Use the Validation Scenarios to validate such compound/interconnected XSL stylesheets.

Leave a Reply