• Aucun résultat trouvé

XSL Transformations

A template to transform a node in a tree has the following form:

< x s l : t e m p l a t e m a t c h = "XPath expr" >

value replacing the matching node(s)

< / x s l : t e m p l a t e >

Like with XML Schemas, we must distinguish between the XSLT predefined elements and the elements used to create the document. The namespace xsl is most often used for elements ofXSLT. In order to trigger axsl:template(a production rule), the transformation process must first identify the node (or nodes) to which it applies. This is done with the value of thematchattribute that specifies anXPathexpression. The content of thexsl:template defines the new structure of the tree by combining any part of the matched tree, new parts or even other parts of the document tree. The parts of the tree used as building blocks are referenced by XPath expressions and combined with functions, conditions, restricted looping constructs, etc. But the reader must remember thatXSLTis a declarative language (similar to Prolog in some ways), so the ordering of templates cannot be used to influence the order of processing of the document tree.

A stylesheet follows a simple process: find a node for which a template applies and then, according to the content of the template, build a new tree structure in the context of this node: a context gives access to the current node, its parent, its siblings and its position within its siblings. To build the new tree structure, a template usually involves the application of templates to its children and their combination. This is done with thexsl:apply-templates element; without attributes, this forces the application of templates to all the children nodes of the current node, but the transformation can be applied to other nodes by using the

selectattribute which specifies an XPath expression.

Templates can also be named and called with xsl:call-template, similar to procedures in standard programming languages. But be aware that these procedures arerules and that they cannot have variables that can change their value: XSLT is thus a single assignment language much like functional languages; parameters are the only mean of passing variable information between templates. Contrarily to ordinary templates, named templates do not change the context of their application. Therefore, we see that the principles underlying XSLT are general, simple and powerful.

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

{xsl:apply-templates |xsl:call-template |xsl:variable |xsl:attribute|xsl:element}*

</xsl:template>

<xsl:template name=”QName”>

xsl:param*

{xsl:apply-templates |xsl:call-template |xsl:variable |xsl:attribute|xsl:element}*

</xsl:template>

Table 4.2: A reminder of the subset of XSLT syntax used in our examples. Names in italics refer to other elements. Regular expressions are used to describe the allowed forms: braces are used for grouping, ? indicates an optional grouping, * a repetition (possibly none) and + a repetition at least once.

Table 4.2 shows the main stylesheet elements to define transformation rules. As a stylesheet is itself a XML document, it can be validated with the appropriate schema.

The root element is xsl:stylesheet which contains a certain number of templates.

A xsl:template element with a match attribute will be called when an element

match-ing its pattern is encountered durmatch-ing the processmatch-ing of the XML instance document. A xsl:templateelement with anameattribute must be called explicitly by axsl:call-templates. The content of the matched element in the source document is replaced by the content of the template which usually involves the application of templates to its children.

xsl:apply-templates is the fundamental operation for traversing the document tree.

Without attributes, it indicates that processing should be recursively applied to all children elements and text nodes (not to attributes). If the element is empty, then the nodes are processed in the document order but it is possible to specify a different ordering with a

xsl:sortelement. Actual parameters can also be given by name and value usingwith-param

elements. Formal parameters are declared at the start of the xsl:template elements.

xsl:value-of is the fundamental way of getting the information contained in an element

from the source document.

Local single assignment variables can be defined within templates with a xsl:variable element. Its value can then be recovered in an XPathexpression by prefixing its name with

$.

Conditional processing is achieved using eitherxsl:ifwhich returns its content when the value of its test attribute is true or a non-empty set of nodes. xsl:choose is for selecting the first value from a series of alternatives indicated byxsl:when. Thexsl:whenare tested in sequence and the first one that returns true is the value of this element. If no test succeeds and an xsl:otherwiseelement is present, its value is the value of the xsl:choose element.

Although recursive traversal of the document tree is the preferred way of going through nodes, it is also possible to do this traversal iteratively with a xsl:for-each.

Processing of the nodes is usually done in document order but the order can be changed using xsl:sort which allows to specify the sorting key with the select attribute and an ascending or descending sort according to the value of order attribute. The values sorted are usually the text value but their numeric value can also be used for sorting by specifying the data-type attribute.

The dynamic creation of target document elements, attributes and text nodes is done using the xsl:element,xsl:attribute and xsl:text elements.

We will now look at many examples of use of these principles and XSL elements in the following sections; first with straightforward transformations into HTML, then into plain text and finally intoformatting objects to produce more complex formatting.

Listing 4.1: [compactHTML.html]: HTML output (slightly reformatted here to fit in the page) produced by the transformation of listing 4.6 on the cellar book (listing 2.2)

< h t m l x m l n s = " h t t p : // www . w3 . org / 1 9 9 9 / x h t m l " >

Figure 4.1: [compactHTML.jpg]: HTML display of listing 4.1

Documents relatifs