1 <?xml version="1.0" encoding="iso-8859-1"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml"><head> 4 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> 5 <title>Advanced Template Design</title> 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> 15 16 <h2>Templates and Transformations</h2> 17 18 <p>When prepared, XSLForms templates are converted to XSL stylesheets, and 19 when form data is displayed, such XSL stylesheets are used to 20 transform the form data to a representation which looks like the 21 original template, but with different parts of the template populated 22 with the form data. The process can be illustrated as 23 follows:</p> 24 25 <table style="text-align: left; width: 80%;" align="center" border="0" cellpadding="5" cellspacing="5"> 26 <tbody> 27 <tr> 28 <th style="border: 1px solid rgb(0, 0, 0); background-color: rgb(193, 255, 102); text-align: center; vertical-align: top;">Template<br /> 29 <span class="method">A template presents the general form of the final output.</span></th> 30 <th style="border-style: solid; border-width: 1px; text-align: center; vertical-align: top;">Stylesheet<br /> 31 <span class="method">Templates are converted to stylesheets, which are like programs specially designed to present XML information.</span></th> 32 <th style="border: 1px solid rgb(0, 0, 0); background-color: rgb(255, 204, 255); text-align: center; vertical-align: top;">Output<br /> 33 <span class="method">The final output is produced when a stylesheet is combined with an XML document.</span></th> 34 </tr> 35 </tbody> 36 </table> 37 38 <p>Since the stylesheet is based on XSL, which is a rich 39 language in its own right, advanced template design techniques can 40 involve some of the features of XSL - at least those which do not 41 affect the simplicity or general structure of our templates.</p> 42 43 <h2>Template Extension Functions and Variables</h2> 44 45 <p>One 46 area where XSL features are already employed to affect the final output 47 in XSLForms is in the toolkit's use of template extension 48 functions and special variables - the latter being used in the output 49 production process. For example, we might decide to use the lower level 50 template functions to present the value of an attribute:</p> 51 52 <pre><span template:attribute="some-attribute" template:value="$this-value">xxx</span></pre> 53 54 <p>In the above example, we reference the special variable <code>$this-value</code> which refers to the value of <code>some-attribute</code>. An example of template functions in use looks like this:</p> 55 56 <pre><span id="{template:this-element()}">xxx</span></pre> 57 58 <p>In the above example, we invoke the template function <code>template:this-element</code> 59 in order to create a unique identifier in the final output. Since we 60 use the function inside an attribute which is not prefixed with <code>template</code>, we must enclose the expression between <code>{</code> and <code>}</code> characters.</p> 61 62 <h2>Beyond Template Extension Functions</h2> 63 64 <p>Since 65 the above template extension functions and variables are merely special in the 66 sense that XSLForms provides them to produce its output, and since they 67 are accessed in the stylesheet using normal XSL-based mechanisms, there 68 is no technical barrier to using other kinds of valid XSL (or more 69 precisely, XPath) expressions in cases such as those given above. The 70 rules for using such expressions in attributes are straightforward:</p> 71 72 <ul><li>Attributes prefixed with <code>template</code> can contain expressions as one would write them normally.</li><li>Other attributes must have their expressions enclosed between <code>{</code> and <code>}</code> characters so that the expression is evaluated and replaced with the result in the final output.</li></ul> 73 74 <p>Here is a trivial example of the usage of an XPath expression, based on one of the above examples:</p> 75 76 <pre><span template:attribute="some-attribute" template:value="string-length($this-value)">xxx</span></pre> 77 78 <p>In the above example, we invoke the standard XPath function <code>string-length</code> in order to produce within the <code>span</code> element the length of the value of <code>some-attribute</code> (instead of the actual value).</p> 79 80 <p>More 81 interesting applications of XPath expressions and non-XSLForms 82 functions arise when using more of the potential of XPath to select 83 arbitrary elements and attributes and to perform calculations on the 84 selected nodes. The following example originates from the Configurator 85 example application:</p> 86 87 <pre><span template:value="sum(/configuration//*[@value-is-set]/@price) + sum(/configuration//*[@value = ../@value]/@price)"></span></pre> 88 89 <p>This complicated expression, to be inserted within the <code>span</code> element, finds all elements in the system configuration having the <code>value-is-set</code> 90 attribute and adds their prices together; this total is combined with 91 the sum of the prices from all elements marked as selected in their 92 respective lists. In other words, it gets the total of all selected 93 components and inserts it into the final output.</p> 94 95 <h3>Using XSLT Extension Function Libraries</h3> 96 97 <p>Unlike the standard XPath functions, the XSLT extension functions supported 98 by libxslt must have their library namespace declared within the template. For 99 example:</p> 100 101 <pre><span xmlns:math="http://exslt.org/math" math:expr-prefix="math http://exslt.org/math" 102 template:value="math:max(product/@price)">highest price</span></pre> 103 104 <p>Here, the <code>math</code> namespace is declared so that the 105 <code>math:max</code> function can be used. In addition, the 106 <code>math:expr-prefix</code> attribute ensures that such declarations are 107 preserved not only within the XSLForms framework and in the preparation of the 108 output stylesheet, but also as a measure to prevent potentially damaging edits 109 to the template. See the <a 110 href="reference.html#housekeeping-annotations">"Housekeeping Annotations"</a> 111 section of the attribute reference guide for details.</p> 112 113 </body></html>