Several Jelly Examples
Introduction In Jelly a lot is possible. The functionality used most by the user is the use of variables. It is possible to use variables as text, where XML is not interpreted, but it is also possible to interpret the XML and even to select only a part of the XML with an XPath expression. More information about Jelly can be found in . Note that the Jelly expression $${..} will be executed even from a CDATA tag or a <!-- --> comment section. The way to escape it is to use an extra dollar as in $$$${..}. Note that variables inside a JEXL expression may not contain -. It is allowed to use _ and to use dotted variables like my.dotted.variable. Some variables are automatically set and allow access to various information. It is possible to overwrite these locally, but this is not recommended. The variables are: $formdata For a submitted form this consists of a tag variables, containing the submitted form data. $instance This contains internal information and should not be needed. First an example will be given with text and then another with XML code. Note that the namespaces used are xmlns:c="jelly:core" and xmlns:x="jelly:xml".
Some Examples An example with text set and read using <code>jelly:core</code>. 4.2 The version is or $${version}. ]]> This is an example where the text 4.2 is put in a variable named version. The trim attribute eith value true indicates that any whitespace at the beginning or the end should be removed. The result is displayed below:
4.2 The version is or ${version}.
An example with XML set and read using <code>jelly:xml</code>. 2 1 The formula is . The part of the formula right of the equals-sign is . ]]> This is an example where an Openmath function definition is put in a variable called formula. With x:copyOf it is possible to select the whole object or to select a part of it. The output is displayed below.
2 1 The formula is . The part of the formula right of the equals-sign is .
An example with <tag>core:if</tag> Jelly has both core:if and xml:if tags. The contents will only be processed if the value of the test attribute evaluates to true. In the following code a counter is either set to 1 or increased if it already exists. The counter is now $${counter}. Increasing the counter. The counter is now $${counter}. ]]> When run this will output the following: Initialising the counter. The counter is now ${counter}. Increasing the counter. The counter is now ${counter}. The tag xml:if is like core:if, but uses XPath and XML variables. An example with <tag>core:choose</tag> and <tag>core:when</tag>. Instead of using several core:if statements it is possible to use a core:choose. A core:choose consists at least one core:when tags and might contain an core:otherwise tag. The first core:when tag for which the test evaluates to true will be processed. If no core:when tags have a test that evaluates to true and a core:otherwise is present then the core:otherwise tag will be processed. The following code converts the counter from the previous example to text. The counter is now $${counter}. The text is now $${countertext}. ]]> Ininitialise or increase the counter. The counter is now ${counter}. The text is now ${countertext}. Note that the expressions now need to be written as a jelly expression: $${..}. Furthermore note that there is no xml:choose tag, so it is impossible to use XML variables or XPath in a choose.
More information The main Jelly page is the Apache Jelly page. On that site is also more about the jelly:core tags (c:-tags), more about the other tags (one of which is xml) and more about JEXL, the Java Expression Language.