Jump to content

XSLT

0% developed
From Wikibooks, open books for an open world

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents (of the same XML schema or not), or other objects such as HTML for web pages, or plain text for other uses, or many other txt-based formats (ex. RTF or any SGML document).

XSLT or XSL?
The usual 3-letters-extension of XSLT files are .xsl, so, it causes some confusion with .xls which is the suffix of older Microsoft Excel files.
XSLT or XSL-FO?
XSLT can be used to transform XML into XSL Formatting Objects (XSL-FO), which can then be converted to PDF, PostScript and PNG. XSLT and XSL-FO are distinct things.

When transforming "XML to XML" a chain of more transforms are possible, when transforming "XML to object", the chaining must stop (objects are terminal).

There are two main XSLT standard groups:

  • XSLT-v1 (old): was published as a W3C recommendation in November 1999. As of 2014, however, XSLT v1.0 is still widely used, since 2.0 and 3.0 is not supported natively in web browsers or for environments like LAMP.
  • XSLT-v2 and XSLT-v3 (newers): only works in "Java world" and few proprietary frameworks, like Oracle's or Altova's products.

The present wikibook is about XSLT-v1, that have the wider community of users.

The XSLT-v1 parsers, like libXml2, are fast and stable. The main XSLT-v1 use, in a MVC context, is as MVC-Viewer to filter or render, with minimal processing cost, the XML produced by the MVC-modeler. For a "Turing complete" processing, to avoid XSLT-v2 or XSLT-v3 migration, the usual workaround is to use processing from the embedding language, by the register-functions facility. Example: this wikibook shows how to use register-functions when XSLT is used in a PHP environment.

Registered functions

[edit | edit source]

XPath-v1 and XSLT-v1 are essential standards, present in almost all XML applications, and many open platform browsers (Firefox, Chrome and others), portable systems (like ANDROID), desktop systems (like UBUNTU or Windows), databases (ex. PostgreSQL, MySQL, etc.), and others.

But XPath-v1 and XSLT-v1 are limited in processing capabilities. A good alternative is to use external functions, that can be registered in the XPath or XSLT parser. This facility is knowed as registeredFunctions.

RegisteredFunctions are essential to writing good XPath-v1 and XSLT-v1 applications, but are rarely well explained: to enhance processing capabilities you need to understand RegisterdFunctions, whose purpose and exact contexts of use.

A programming language (ex. PHP) or an agent (ex. web-browser) that uses the XSLT or XPath parser, can be called back for offer their internal functions (ex. browser's Javascript functions or PHP's functions).

RegisteredFunctions are not well documented. W3C standards not defined all details and not offer a "tutorial for registeredFunctions".

The PHP/Python/Javascript/etc. manual itself doesn't have a simple explanation that actually captures the essence and most posts written around the web seem to be missing some detail or other as well.

This wikibook attempts to fill that gap; and takes a broad sweep of related topics to do so. Two main wais, to use the external functions, are proposed here:

  1. free use: any function, build-in or user-defined, when the best practice is to use a specific namespace, like "php" for PHP or "py" for Python.
  2. controled use: a convention, independent of language, using a local-stanard library. The best practice is to use a generic namespace, like "fn". So, if the project changes, for instance, from PHP to Pythom, the XSLT (or XPath) stay with the same code and namespaces.
[edit | edit source]

For free use registered functions:

For controled registered functions (conventions for use the FN namespace):