XSLTools

docs/advanced.html

607:fb25bbeaabc5
2007-07-07 paulb [project @ 2007-07-07 21:22:07 by paulb] Fixed licensing references.
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     2 <html xmlns="http://www.w3.org/1999/xhtml"><head>     3   <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" />     4        5   <title>Advanced Template Design</title><meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" />     6   <link href="styles.css" rel="stylesheet" type="text/css" /></head>     7 <body>     8 <h1>Advanced Template Design</h1>     9 <p>In the <a href="template-design.html">"Template Design"</a> document, the special XSLForms annotations    10 were presented as a means to display the information contained in form    11 data documents (or just general XML documents with no special    12 connection to Web forms). However, since XSLForms templates build upon    13 XSL transformations, it is also possible to employ certain XSL    14 techniques to present the information in various different ways.</p><h2>Templates and Transformations</h2><p>When&nbsp;prepared, XSLForms templates are converted to XSL stylesheets, and    15 when form data is&nbsp;displayed, such XSL stylesheets are used to    16 transform the form data to a representation which looks like the    17 original template, but with different parts of the template populated    18 with&nbsp;the&nbsp;form data. The process can be illustrated as    19 follows:</p><table style="text-align: left; width: 80%;" align="center" border="0" cellpadding="5" cellspacing="5">    20     21   <tbody>    22     23     <tr>    24     25           26     27       <th style="border: 1px solid rgb(0, 0, 0); background-color: rgb(193, 255, 102); text-align: center; vertical-align: top;">Template<br />    28     29       <span class="method">A template presents the general form of the final output.</span></th>    30     31       <th style="border-style: solid; border-width: 1px; text-align: center; vertical-align: top;">Stylesheet<br />    32     33       <span class="method">Templates are converted to stylesheets, which are like programs specially designed to present XML information.</span></th>    34     35       <th style="border: 1px solid rgb(0, 0, 0); background-color: rgb(255, 204, 255); text-align: center; vertical-align: top;">Output<br />    36     37       <span class="method">The final output is produced when a stylesheet is combined with an XML document.</span></th>    38     39     </tr>    40     41         42     43       44   </tbody>    45 </table><p>Since the stylesheet is based on XSL, which is a rich    46 language in its own right, advanced template design techniques can    47 involve some of the features of XSL - at least those which do not    48 affect the simplicity or general structure of our templates.</p><h2>Template Extension Functions and Variables</h2><p>One    49 area where XSL features are already employed to affect the final output    50 in XSLForms is in the toolkit's use of template&nbsp;extension    51 functions and special variables - the latter being used in the output    52 production process. For example, we might decide to use the lower level    53 template functions to present the value of an attribute:</p><pre>&lt;span template:attribute="some-attribute" template:value="$this-value"&gt;xxx&lt;/span&gt;</pre><p>In the above example, we reference the special variable&nbsp;<code>$this-value</code> which refers to the value of&nbsp;<code>some-attribute</code>. An example of template functions in use looks like this:</p><pre>&lt;span id="{template:this-element()}"&gt;xxx&lt;/span&gt;</pre><p>In the above example, we invoke the template function&nbsp;<code>template:this-element</code>    54 in order to create a unique identifier in the final output. Since we    55 use the function inside an attribute which is not prefixed with&nbsp;<code>template</code>, we must enclose the expression between&nbsp;<code>{</code> and&nbsp;<code>}</code> characters.</p><h2>Beyond Template Extension Functions</h2><p>Since    56 the above template extension functions and variables are merely special in the    57 sense that XSLForms provides them to produce its output, and since they    58 are accessed in the stylesheet using normal XSL-based mechanisms, there    59 is no technical barrier to using other kinds of valid XSL (or more    60 precisely, XPath) expressions in cases such as those given above. The    61 rules for using such expressions in attributes are straightforward:</p><ul><li>Attributes prefixed with&nbsp;<code>template</code> can contain expressions as one would write them normally.</li><li>Other attributes&nbsp;must have their expressions enclosed&nbsp;between&nbsp;<code>{</code> and&nbsp;<code>}</code> characters so that the expression is evaluated and replaced with the result in the final output.</li></ul><p>Here is a trivial example of the usage of an XPath expression, based on one of the above examples:</p><pre>&lt;span template:attribute="some-attribute" template:value="string-length($this-value)"&gt;xxx&lt;/span&gt;</pre><p>In the above example, we invoke the standard XPath function&nbsp;<code>string-length</code> in order to produce within the&nbsp;<code>span</code> element the length of the value of&nbsp;<code>some-attribute</code> (instead of the actual value).</p><p>More    62 interesting applications of XPath expressions and non-XSLForms    63 functions arise when using more of the potential of XPath to select    64 arbitrary elements and attributes and to perform calculations on the    65 selected nodes. The following example originates from the Configurator    66 example application:</p><pre>&lt;span template:value="sum(/configuration//*[@value-is-set]/@price) + sum(/configuration//*[@value = ../@value]/@price)"&gt;&lt;/span&gt;</pre><p>This complicated expression, to be inserted within the&nbsp;<code>span</code> element, finds all elements in the system configuration having the&nbsp;<code>value-is-set</code>    67 attribute and adds their prices together; this total is combined with    68 the sum of the prices from all elements marked as selected in their    69 respective lists. In other words, it gets the total of all selected    70 components and inserts it into the final output.</p></body></html>