XPath 2.0 Functions
Smartsite 7.1 - ...
As of Smartsite 7.1, the XForms Engine also includes a number of XPath 2.0 functions.
The (recommended) default prefix for the function namespace is fn:
The URI of the function namespace is: http://www.w3.org/2005/xpath-functions
Available XPath 2.0 functions
The table below shows which functions are available.
Function | Description |
---|---|
Accessors | |
fn:node-name(node) | Returns the node-name of the argument node. |
fn:nilled(node) | Returns a Boolean value indicating whether the argument node is nilled. |
fn:string(arg) | Returns the string value of the argument. The argument could be a number, boolean, or node-set. |
fn:base-uri(node) | Returns the value of the base-uri property of the specified node. |
Functions on numeric values | |
fn:abs(num) | Returns the absolute value of the argument. |
fn:ceiling(num) | Returns the smallest integer that is greater than the number argument. |
fn:floor(num) | Returns the largest integer that is not greater than the number argument. |
fn:round(num) | Rounds the number argument to the nearest integer. |
fn:round-half-to-even(num) | Rounds the number argument to the nearest even integer. |
Functions on strings | |
fn:compare(arg1,arg2) | Returns -1 if arg1 is less than arg2, 0 if arg1 is equal to arg2, or 1 if arg1 is greater than arg2. |
fn:codepoint-equal(arg1,arg2) | Returns true if the value of arg1 is equal to the value of arg2, according to the Unicode code point collation, otherwise it returns false. |
fn:concat(string,string,...) | Returns the concatenation of the strings. |
fn:string-join(nodeset,sep) | Returns a string created by concatenating the string arguments and using the sep argument as the separator. |
fn:substring(string,start,length) | Returns the substring from the start position to the specified length. Index of the first character is 1. If length is omitted it returns the substring from the start position to the end. |
fn:string-length(string) | Returns the length of the specified string. |
fn:normalize-space(string) | Removes leading and trailing spaces from the specified string, and replaces all internal sequences of white space with one and returns the result. |
fn:normalize-unicode(string,normalizationForm) | Returns a new string whose binary representation is in the specified Unicode normalization form ("NFC", "NFD", "NFKC" or "NFKD"). |
fn:upper-case(string) | Converts the string argument to upper-case. |
fn:lower-case(string) | Converts the string argument to lower-case. |
fn:encode-for-uri(string) | Encodes reserved characters within the string argument, using escaping rules as defined in section 2 of RFC 3986. |
fn:contains(string1,string2) | Returns true if string1 contains string2, otherwise it returns false. |
fn:starts-with(string1,string2) | Returns true if string1 starts with string2, otherwise it returns false. |
fn:ends-with(string1,string2) | Returns true if string1 ends with string2, otherwise it returns false. |
fn:substring-before(string1,string2) | Returns the start of string1 before string2 occurs in it. |
fn:substring-after(string1,string2) | Returns the remainder of string1 after string2 occurs in it. |
fn:matches(string,pattern) | Returns true if the string argument matches the pattern, otherwise, it returns false. |
fn:replace(string,pattern,replace) | Returns a string that is created by replacing the given pattern with the replace argument. |
Functions on anyURI | |
fn:resolve-uri(relative,base) | This function enables a relative URI reference to be resolved against an absolute URI (base). When base is omitted, the base URI of the host is used. |
Functions on Boolean values | |
fn:true() | Returns the boolean value true. |
fn:false() | Returns the boolean value false. |
fn:not(arg) | The argument is first reduced to a boolean value by applying the boolean() function. Returns true if the boolean value is false, and false if the boolean value is true. |
Functions related to QNames | |
fn:resolve-QName(string,node) | Returns an xs:QName with the lexical form given in the first argument. The prefix is resolved using the in-scope namespaces for a given element. |
fn:QName(string1,string2) | Returns an xs:QName with the namespace URI given in the first argument and the local name and prefix in the second argument. |
fn:prefix-from-QName(qname) | Returns an xs:NCName representing the prefix of the xs:QName argument. |
fn:local-name-from-QName(qname) | Returns an xs:NCName representing the local name of the xs:QName argument. |
fn:namespace-uri-from-QName(qname) | Returns the namespace URI for the xs:QName argument. If the xs:QName is in no namespace, the zero-length string is returned. |
fn:namespace-uri-for-prefix(prefix) | Returns the namespace URI of one of the in-scope namespaces for the given element, identified by its namespace prefix. |
Functions on nodes | |
fn:name(nodeset) | Returns the name of the first node in the specified node set. |
fn:local-name(nodeset) | Returns the name of the first node in the specified node set - without the namespace prefix. |
fn:namespace-uri(nodeset) | Returns the namespace URI of the first node in the specified node set. |
Functions on sequences | |
fn:boolean(arg) | Returns a boolean value for a number, string, or node-set. |
References
- Official w3 specification: XPath 2.0 functions
- w3schools.com reference library: XPath, XQuery and XSLT Functions
- XPath Introduction and Resources
Example
XML | Copy Code |
---|---|
<?xml version="1.0" encoding="UTF-8"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:fn="http://www.w3.org/2005/xpath-functions"> <head> <title>XPath 2.0 functions test</title> <xf:model> <xf:instance> <data xmlns=""> <string>tattoo</string> <alphabet>abcdefghi</alphabet> <empty /> </data> </xf:instance> </xf:model> </head> <body> <xf:group class="contains"> <xf:output value="fn:contains(string, 't')" /> <!-- result: true --> <xf:output value="fn:contains(string, 'tt')" /> <!-- result: true --> <xf:output value="fn:contains(string, 'ttt')" /> <!-- result: false --> <xf:output value="fn:contains('', empty)" /> <!-- result: true --> </xf:group> <xf:group class="starts-with"> <xf:output value="fn:starts-with(string, 'tat')" /> <!-- result: true --> <xf:output value="fn:starts-with(string, 'att')" /> <!-- result: false --> <xf:output value="fn:starts-with(empty, empty)" /> <!-- result: true --> </xf:group> <xf:group class="ends-with"> <xf:output value="fn:ends-with(string, 'too')" /> <!-- result: true --> <xf:output value="fn:ends-with(string, 'tattoo')" /> <!-- result: true --> <xf:output value="fn:ends-with(string, 'att')" /> <!-- result: false --> <xf:output value="fn:ends-with(empty, empty)" /> <!-- result: true --> </xf:group> <xf:group class="substring-before"> <xf:output value="fn:substring-before(string, 'too')" /> <!-- result: 'tat' --> <xf:output value="fn:substring-before(string, 'tat')" /> <!-- result: '' --> <xf:output value="fn:substring-before(empty, empty)" /> <!-- result: '' --> <xf:output value="fn:substring-before(alphabet, 'def')" /> <!-- result: 'abc' --> </xf:group> <xf:group class="substring-after"> <xf:output value="fn:substring-after(string, 'too')" /> <!-- result: '' --> <xf:output value="fn:substring-after(string, 'tat')" /> <!-- result: 'too' --> <xf:output value="fn:substring-after(empty, empty)" /> <!-- result: '' --> <xf:output value="fn:substring-after(alphabet, 'def')" /> <!-- result: 'ghi' --> </xf:group> </body> </html> |