This document is also available in these non-normative formats: diff-marked HTML .
The English version of this specification is the only normative version. Non-normative translations may also be available.
Copyright © 2008 W3C® ( MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
This XPath functions module provides a base collection of XPath 1.0 functions that are useful across many modules of XForms.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document is a Working Draft of the W3C. This document has been produced by the W3C Forms Working Group as part of the Forms Activity within the W3C Interaction Domain. The authors of this document are the W3C Forms Working Group participants.
Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
Comments on this document are welcome. Please send discussion comments to www-forms@w3.org. Please send formal comment about this document to the public editor mailing list www-forms-editor@w3.org. ( archive).
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. The Working Group maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
1 Introduction
2 XPath Function Library
2.1 Data Integrity Functions
2.1.1 The is-card-number() Function
2.1.2 The digest() Function
2.1.3 The hmac() Function
2.2 Date and Time Functions
2.2.1 The local-date() Function
2.2.2 The local-dateTime() Function
2.2.3 The now() Function
2.2.4 The days-from-date() Function
2.2.5 The days-to-date() Function
2.2.6 The seconds-from-dateTime() Function
2.2.7 The seconds-to-dateTime() Function
2.2.8 The adjust-dateTime-to-timezone() Function
2.2.9 The seconds() Function
2.2.10 The months() Function
2.3 Conditional Functions
2.3.1 The if() Function
2.3.2 The choose() Function
2.3.3 The compare() Function
2.3.4 The count-non-empty() Function
2.3.5 The boolean-from-string() Function
2.3.6 The current() Function
2.4 Math Functions
2.4.1 The avg() Function
2.4.2 The min() Function
2.4.3 The max() Function
2.4.4 The power() Function
2.4.5 The random() Function
3 Glossary Of Terms
A References
A.1 Normative References
A.2 Informative References
B Acknowledgements (Non-Normative)
C Production Notes (Non-Normative)
This document describes an XPath Functions Module, which includes a set of useful, general-purpose XPath 1.0 functions.
This document uses the terms must, must not, required, shall, shall not, recommended, should, should not, may, and optional in accord with [RFC 2119].
The function library for XPath expressions includes the entire [XPath 1.0] Core Function Library, including operations on node-sets, strings, numbers, and booleans. The following sections define additional functions that are required to implement by this module. Other modules of the consuming profile may add more functions to the function library. Also, a number of useful community extensions are defined at [EXSLT]
boolean is-card-number(string?)
If the string parameter conforms to the pattern restriction of zero or more numeric digits, then this function applies the algorithm described in [Luhn Patent] and returns true if the string is empty or if the number in the string satisfies the Luhn formula. Otherwise, false is returned. If the parameter is omitted, it defaults to the string-value of the current context node.
Examples:
is-card-number(.)
returns true if and only if the context node contains a string that contains zero or more digits and satisfies the formula.
is-card-number('4111111111111111')returns true. Other examples of string constants that will return true are : 5431111111111111, 341111111111111 and 6011601160116611.
is-card-number('123')returns false.
string digest(string, string, string?)
This function accepts a string of data, a string indicating a cryptographic hashing algorithm, and an optional string indicating an encoding method. The data string is serialized as UTF-8, the hash value is then computed using the indicated hash algorithm, and the hash value is then encoded by the indicated method, and the result is returned by the function. The following table presents the keywords for the second string parameter and the corresponding hash algorithms:
| Keywords | Hash Algorithm | Status |
|---|---|---|
MD5 | The MD5 hash algorithm defined in [MD5] | Required |
SHA-1 | The SHA-1 hash algorithm defined in [SHA2] | Required |
SHA-256 | The SHA-256 hash algorithm defined in [SHA2] | Required |
SHA-384 | The SHA-384 hash algorithm defined in [SHA2] | Optional |
SHA-512 | The SHA-512 hash algorithm defined in [SHA2] | Optional |
| Any other NCName | Reserved. Their use results in an exception (see 2 XPath Function Library for the exception type) | Required |
| QNameButNotNCName | An implementation-specific hash algorithm is used. If the implementation does not support the indicated hash algorithm, then an exception occurs (see 2 XPath Function Library for the exception type). | Required |
This recommendation defines the values hex and base64 for the third string parameter that indicates the encoding method. If the parameter is missing, then the default is base64. The hex and base64 encoding methods of this function correspond to the encodings defined in [XML Schema part 2] for the datatypes hexBinary and base64Binary, respectively. Any other string value given for the encoding method is unsupported.
digest('abc', 'SHA-1', 'hex')returns a9993e364706816aba3e25717850c26c9cd0d89d.
digest('abc', 'MD5', 'hex')returns 900150983cd24fb0d6963f7d28e17f72.
digest('abc', 'SHA-256', 'hex')returns ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
string hmac(string, string, string, string?)
This function accepts a string for a key or shared secret, a string of data, a string indicating a cryptographic hashing algorithm, and an optional string indicating an encoding method. The key and data strings are serialized as UTF-8, and they are subjected to the HMAC algorithm defined in [HMAC] and parameterized by the the hash algorithm indicated by the third parameter. The result is encoded with the method indicated by the fourth parameter, and the result is returned by the function.
The following table presents the keywords for the third string parameter and the corresponding hash algorithms:
| Keywords | Hash Algorithm | Status |
|---|---|---|
MD5 | The MD5 hash algorithm defined in [MD5] | Required |
SHA-1 | The SHA-1 hash algorithm defined in [SHA2] | Required |
SHA-256 | The SHA-256 hash algorithm defined in [SHA2] | Required |
SHA-384 | The SHA-384 hash algorithm defined in [SHA2] | Optional |
SHA-512 | The SHA-512 hash algorithm defined in [SHA2] | Optional |
| Any other NCName | Reserved. Their use results in an exception (see 2 XPath Function Library for the exception type) | Required |
| QNameButNotNCName | An implementation-specific hash algorithm is used. If the implementation does not support the indicated hash algorithm, then an exception occurs (see 2 XPath Function Library for the exception type). | Required |
This recommendation defines the values hex and base64 for the fourth string parameter that indicates the encoding method. If the parameter is missing, then the default is base64. The hex and base64 encoding methods of this function correspond to the encodings defined in [XML Schema part 2] for the datatypes hexBinary and base64Binary, respectively. Any other string value given for the encoding method is unsupported.
hmac('Jefe', 'what do ya want for nothing?', 'SHA-1', 'hex')returns effcdf6ae5eb2fa2d27416d5f184df9c259a7c79
hmac('Jefe', 'what do ya want for nothing?', 'MD5', 'hex')returns 750c783e6ab0b503eaa86e310a5db738
hmac('Jefe', 'what do ya want for nothing?', 'SHA-256', 'hex')returns 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843
Note:
The following XML Schema datatypes do not have specific functions for manipulation within expressions: xsd:time, xsd:gYearMonth, xsd:gYear, xsd:gMonthDay, xsd:gDay, xsd:gMonth. Extension functions may be added to perform needed operations on these datatypes.
string local-date()
This function returns a lexical xsd:date obtained as if by the following rules: the result of now() is converted to a local date based on the user agent time zone information. If no time zone information is available, then the date portion of the result of now() is returned.
Example:
local-date()
could return 2006-10-13-07:00
substring(local-date(), 1, 10)
could return 2006-10-13
days-to-date(days-from-date(local-date()) + 31)
returns a date that is 31 days from today. For example, if local-date() returns 2006-10-13-07:00, then the result is 2006-11-13.
string local-dateTime()
This function returns a lexical xsd:dateTime obtained as if by the following rules: the result of now() is converted to a local dateTime based on the user agent time zone information. If no time zone information is available, then the result of now() is returned.
Example:
local-dateTime()
could return 2006-10-13T16:04:17-07:00
adjust-dateTime-to-timezone(seconds-to-dateTime(seconds-from-dateTime(local-dateTime()) + 7200))
Adds two hours (7200 seconds) to the local date and time, returning the result in the local time zone. For example, if local-dateTime() returns 2007-10-02T14:26:43-07:00, then the above expression returns 2007-10-02T16:26:43-07:00
string now()
The now function returns the current UTC date and time as a string value in the canonical XML Schema xsd:dateTime format. If time zone information is available, it is used to convert the date and time to UTC. If no time zone information is available, then the date and time are assumed to be in UTC.
now()
returns 2006-10-14T01:04:17Z if local-dateTime() returns 2006-10-13T18:04:17-07:00
seconds-to-dateTime(seconds-from-dateTime(now()) + 7200)
Computes two hours from now, returning the result in UTC time. For example, if now() returns 2007-10-02T21:26:43Z, then the above expression returns 2007-10-02T23:26:43Z
number days-from-date(string)
This function returns a whole number of days, according to the following rules:
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. Hour, minute, and second components are ignored after normalization. Any other input parameter causes a return value of NaN.
Note:
If an xsd:date is given as the parameter, the timezone is ignored if provided because there is no way to normalize to the date in the UTC timezone without both the time and timezone.
Examples:
days-from-date("2002-01-01")returns 11688
days-from-date("2002-01-01-07:00")returns 11688
days-from-date("1969-12-31")returns -1
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:
The number parameter is rounded to the nearest whole number, and the result is interpreted as the difference between the desired date and 1970-01-01. 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
number seconds-from-dateTime(string)
This function returns a possibly fractional number of seconds, according to the following rules:
If the string parameter represents a legal lexical xsd:dateTime, the return value is equal to the number of seconds difference between the specified dateTime (normalized to UTC) and 1970-01-01T00:00:00Z. If no time zone is specified, UTC is used. Any other input string parameter causes a return value of NaN. This function does not support leap seconds.
Example:
seconds-from-dateTime('1970-01-01T00:00:00Z')returns 0
seconds-from-dateTime('1970-01-01T00:00:00-08:00')returns 28800 because it is eight hours later than 1970-01-01T00:00:00Z
seconds-from-dateTime('1970-01-02T00:00:00Z')returns 86400
seconds-from-dateTime('1969-12-31T00:00:00Z')returns -86400, which is negative because it is one day before 1970-01-01T00:00:00Z
string seconds-to-dateTime(number)
This function returns a string containing a lexical xsd:dateTime that corresponds to the number of seconds passed as the parameter according to the following rules:
The number parameter is rounded to the nearest whole number, and the result is interpreted as the difference between the desired UTC dateTime and 1970-01-01T00:00:00Z. An input parameter value of NaN results in output of the empty string. This function does not support leap seconds.
Examples:
seconds-to-dateTime(0)
returns 1970-01-01T00:00:00Z
seconds-from-dateTime(28800)
returns 1970-01-01T08:00:00Z
seconds-to-dateTime(seconds-from-dateTime(now()) + 7200)
Computes two hours from now, returning the result in UTC time. For example, if now() returns 2007-10-02T21:26:43Z, then the above expression returns 2007-10-02T23:26:43Z
adjust-dateTime-to-timezone(seconds-to-dateTime(seconds-from-dateTime(now()) + 7200))
Computes two hours from now, returning the result in the local time zone. For example, if now() returns 2007-10-02T21:26:43Z and the local date and time is 2007-10-02T14:26:43-07:00, then the above expression returns 2007-10-02T16:26:43-07:00
string adjust-dateTime-to-timezone(string)
This function adjusts a legal lexical xsd:dateTime received as the string parameter to the local time zone of the implementation, and returns the result. If the string argument contains no time zone, then the result is the string argument with the local time zone as the time zone component. If the implementation does not have access to time zone information, UTC is used. The result is empty string if the string argument is the empty sequence or not a legal lexical xsd:dateTime.
Examples:
adjust-dateTime-to-timezone('2007-10-07T02:22:00')returns 2007-10-07T02:22:00-07:00 in the Pacific time zone since daylight savings time applies.
adjust-dateTime-to-timezone('2007-10-02T21:26:43Z')returns 2007-10-02T14:26:43-07:00 in the Pacific time zone since daylight savings time applies.
adjust-dateTime-to-timezone(seconds-to-dateTime(seconds-from-dateTime(now()) + 7200))
Computes two hours from now, returning the result in the local time zone. For example, if now() returns 2007-10-02T21:26:43Z and the local date and time is 2007-10-02T14:26:43-07:00, then the above expression returns 2007-10-02T16:26:43-07:00
number seconds(string)
This function returns a possibly fractional number of seconds, according to the following rules:
If the string parameter represents a legal lexical xsd:duration, the return value is equal to the number specified in the seconds component plus 60 * the number specified in the minutes component, plus 60 * 60 * the number specified in the hours component, plus 60 * 60 * 24 * the number specified in the days component. The sign of the result will match the sign of the duration. Year and month components, if present, are ignored. Any other input parameter causes a return value of NaN.
xsd:duration, it is intended for use only with derived-from-xsd:duration datatypes, specifically xforms:dayTimeDuration. Examples:
seconds("P3DT10H30M1.5S")returns 297001.5 (3 days, 10 hours, 30 minutes, and 1.5 seconds)
seconds("P1Y2M")returns 0 because the year and month parts of the duration are ignored and the remaining portions are unspecified and default to 0
seconds("3")returns NaN because the parameter is not a lexically valid duration
number months(string)
This function returns a whole number of months, according to the following rules:
If the string parameter represents a legal lexical xsd:duration, the return value is equal to the number specified in the months component plus 12 * the number specified in the years component. The sign of the result will match the sign of the duration. Day, hour, minute, and second components, if present, are ignored. Any other input parameter causes a return value of NaN.
xsd:duration, it is intended for use only with derived-from-xsd:duration datatypes, specifically xforms:yearMonthDuration. Examples:
Examples:
months("P1Y2M")returns 14 (1 year and 2 months)
months("-P19M")returns -19 because the duration is negative and expresses 0 years and 19 months
string if(boolean, string, string)
Function if evaluates the first parameter as boolean, returning the second parameter when true, otherwise the third parameter.
Note:
This function is deprecated due to a conflict with [XPath 2.0], which contains an if construct that is incompatible with this function. Authors and design tools are encouraged to use the function choose() from Section 2.3.2 The choose() Function instead of this function.
object choose(boolean, object, object)
This function provides a conditional test that chooses an object to return based on the boolean parameter. If the boolean parameter is true, then the first object is returned, otherwise the second object is returned. Each of the object parameters can be of any XPath datatype as described in [XPath 1.0], and this function does no type conversion of the parameter it chooses to return.
Note:
All parameters of an XPath function are evaluated, so the parameter that is not returned by this function is still evaluated, and its result is discarded by this function.
Note:
Form authors and design tools are encouraged to use this function instead of the function if() described in Section 2.3.1 The if() Function, which has been deprecated. Because this function returns an object instead of a string, migrating from if() to choose() may occasionally require conversion of the return result using the string() function.
Example:
choose(count(x) > 0, x, y)
Returns the node-set of matching x if it is non-empty and the node-set matching y otherwise.
choose(@x, @x, 0)
If the context node of the function contains attribute x, then the nodeset containing that attribute is returned. Otherwise, the number 0 is returned.
number compare(string, string)
This function returns -1, 0, or 1, depending on whether the value of the first argument is respectively less than, equal to, or greater than the value of second argument based on lexicographic comparison using Unicode code point values [Unicode Collation Algorithm].
Example:
compare('apples', 'oranges')returns -1
number count-non-empty(node-set)
Function count-non-empty returns the number of non-empty nodes in argument node-set. A node is considered non-empty if it is convertible into a string with a greater-than zero length.
Example:
choose(count-non-empty(x) > 0, x, y)
Returns the node-set of matching x if it is has any non-empty nodes. Otherwise, the node-set matching y is returned.
boolean boolean-from-string(string)
Function boolean-from-string returns true if the required parameter string is "true" or "1", or false if parameter string is "false", or "0". This is useful when referencing a Schema xsd:boolean datatype in an XPath expression. If the parameter string matches none of the above strings, according to a case-insensitive comparison, the return value is false.
Example:
choose(boolean-from-string(isMinor), parentApplicant, primaryApplicant)
Returns the node-set matching parentApplicant if the content of node isMinor matches a truth value. Otherwise, the node-set matching primaryApplicant is returned. Note that a simple boolean cast of isMinor is a test for node existence, not a test on the node content, so if isMinor exists but contains false, then this function returns false whereas a boolean cast returns true.
node-set current()
Returns the context node used to initialize the evaluation of the containing XPath expression.
Example:
For the following XML data:
<data xmlns=""> <converter> <amount>100</amount> <currency>jpy</currency> </converter> <convTable date="20040212" currency="cdn"> <rate currency="eur">0.59376</rate> <rate currency="mxn">8.37597</rate> <rate currency="jpy">80.23451</rate> <rate currency="usd">0.76138</rate> </convTable> </data>
If the evaluation context node of the following XPath expression is data, then the result of the expression is the converted currency amount:
converter/amount * convTable/rate[@currency = current()/converter/currency]
number avg(node-set)
Function avg returns the arithmetic average of the result of converting the string-values of each node in the argument node-set to a number. The sum is computed with sum(), and divided with div by the value computed with count(). If the parameter is an empty node-set, or if any of the nodes evaluate to NaN, the return value is NaN.
number min(node-set)
Function min returns the minimum value of the result of converting the string-values of each node in argument node-set to a number. "Minimum" is determined with the < operator. If the parameter is an empty node-set, or if any of the nodes evaluate to NaN, the return value is NaN.
number max(node-set)
Function max returns the maximum value of the result of converting the string-values of each node in argument node-set to a number. "Maximum" is determined with the < operator. If the parameter is an empty node-set, or if any of the nodes evaluate to NaN, the return value is NaN.
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(2, 3)
returns 8
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).
number random(boolean?)
This function generates and returns a uniformly distributed random or pseudorandom number in the range from 0.0 up to but excluding 1.0. This function accepts an optional boolean parameter that is false by default. If true, the random number generator for this function is first seeded with a source of randomness before generating the return value. A typical implementation may seed the random number generator with the current system time in milliseconds when random(true) is invoked, and it may apply a linear congruential formula to generate return values on successive invocations of the function.
Example:
random()
could return 0.14159265358979
[Definition: A QName that is not an NCName. In 2006, the W3C named this a PrefixedName. ]
This document was produced with the participation of Forms Working Group participants. Current participants include:
This document was encoded in the XMLspec DTD v2.6. The XML sources were transformed using diffspec and xmlspec stylesheets, version 2.6. The XML Schema portion of the Appendix was rendered into HTML with the xmlverbatim XSLT stylesheet (used with permission). The primary tool used for editing was XMLSpy. The XML was transformed using the XSLT processor in Java 6. The editor(s) use the W3C CVS repository and the W3C IRC server for collaborative authoring.
Open the original version of this page.
Usablenet Assistive is a UsableNet product. Usablenet Assistive Main Page.