XPath functions

This section describes the functions that can be used in XPath expressions.

Core XForms functions

The XForms Core Function Library includes the entire XPath Core Function Library, including operations on node-sets, strings, numbers, and booleans. In addition XForms adds the functions described in the following sections.

days-from-date()

number days-from-date(string)

This function returns a whole number of days, according to the following rules:

  1. If the string parameter represents a legal lexical xsd:date or xsd:dateTime, the return value is equal to the number of days difference between the specified date or dateTime (normalized to UTC) and 1970-01-01.
  2. Hour, minute, and second components are ignored after normalisation.
  3. Any other input parameter causes a return value of NaN.

Examples:

days-from-date("2002-01-01")

returns 11688

days-from-date("1969-12-31")

returns -1

More examples

days-to-date()

string days-to-date(number)

This function returns a string containing a lexical xsd:date that corresponds to the number of days passed as the parameter according to the following rules:

  1. The number parameter is rounded to the nearest whole number.
  2. The result is interpreted as the difference between the desired date and 1970-01-01.
  3. An input parameter value of NaN results in output of the empty string.

Examples:

days-to-date(11688)

returns 2002-01-01

days-to-date(-1)

returns 1969-12-31

More examples

instance()

node-set instance(string)

An XForms Model can contain more that one instance. This function allows access to instance data, within the same XForms Model, but outside the instance data containing the context node.

The argument is converted to a string as if by a call to the string function. This string is treated as an IDREF, which is matched against instance elements in the containing document. If a match is located, and the matching instance data is associated with the same XForms Model as the current context node, this function returns a node-set containing just the root element node (also called the document element node) of the referenced instance data. In all other cases, an empty node-set is returned.

Example:

For instance data corresponding to this XML:

<xforms:instance xmlns="" id="orderform">
  <orderForm>
    <shipTo>
      <firstName>John</firstName>
    </shipTo>
  </orderForm>
</xforms:instance>

The following expression selects the firstName node. Note that the instance function returns an element node, effectively replacing the leftmost location step from the path:

ref="instance('orderform')/shipTo/firstName"

power()

number power(number, number)

Raises the first argument to the power of the second argument, returning the result. If the calculation does not result in a real number, then NaN is returned.

Examples:

power(-1, 0.5)

returns NaN.

if (prin>0 and dur>0 and rate>0, prin*rate/(1-power(1+rate, -dur)), 0)

returns a compounded interest payment value given a non-zero principal (prin), duration (dur) and periodic interest rate (rate).

More examples.

Functions added by formsPlayer

formsPlayer adds some additional functions.

globalInstance()

The standard XForms function instance() must only return instances from the model of the current evaluation context, and although trying to obtain an instance from another model won't result in an error, the return value will be null.

The rationale is to improve performance of XForms processors, since if an author is unable to create dependencies between models, then calculations for each model can be performed on each model individually as necessary, rather than for every model in a form.

Although authors are prevented from creating depencies between models, there will still be occassions when data needs to be moved from one model to another. For this reason formsPlayer adds the globalInstance() function, which can return a reference to any instance in the form. Note however, that if this function is used in bind statement no dependency is created; in other words, if the data in an instance in another model changes, the bind statement will not automatically be recalculated. If a recalculation is needed, then it is the author's responsibility.

A proposal has been made to add this feature to XForms.

Adding custom XPath functions

With formsPlayer it is possible to write your own XPath functions to manipulate instance data within the XForms data processing model. One example of doing this is discussed in the context of controlling a 3D viewer, which shows how to create inline functions (i.e., functions that are defined in the current source document).

It's also possible to create XPath functions which are methods on COM components.