Complex derived attribute calculations
Although this tutorial does not demonstrate this fact, derived attribute calculations frequently need to do more than concatenate static strings with values that are extracted from the JET input model. Such manipulations can often be done by calling XPath functions within an XPath expression.
Suppose that you design a generated e-mail message to inform users that an account was created for them using their e-mail address as the ID, and a generated password. The generated password is the first letter of the given name plus their family name, with the entire password in lowercase letters. The generated e-mail message should contain this password as a reminder to the users.
The JET XPath engine provides several functions to help complete this task. The substring function returns a portion of a string, the concat function concatenates two or more strings, and the lowercase function manages case conversion. The following code fragment shows the calculation for the hypothetical derived attribute for the password on the mailing element:
{lower-case(concat(substring($mailing/@firstName,1,1), $mailing/@lastName))}JET includes most of the XPath 1.0 functions as defined in the XPath 1.0 specifications Core Function Library. Only two functions are not implemented: lang and id. JET also provides several additional XPath functions.
Filtering XPath path expressions
In some cases, you should use XPath predicates to process a subset of elements based on a particular criteria. A predicate is an expression that you apply to a step in an XPath expression to filter elements that match that step. A predicate can be either a Boolean expression or an integer expression, in which case it is treated as a 1-based index to the set of elements. Predicates are enclosed in square brackets ([]).
The following examples make this concept more concrete. The following <c:iterate> tag processes only mailing elements that contain an interest element:
<c:iterate select="$campaign/mailing[interest]" var="mailing">XPath converts the result of the predicate expression interest into a Boolean value by testing the cardinality of the set of matched nodes. The expression is equivalent to the following example:
<c:iterate select="$campaign/mailing[count(interest) > 0]" var="mailing">The following <c:iterate> tag processes mailing elements that specify Product1 as the first product in which a user is interested:
<c:iterate select="$campaign/mailing[interest[1]/@code = 'Product1']" var="mailing">