XPath Functions

XPath expressions consist of (1) path expressions for selecting and filtering nodes, (2) XPath functions and (3) XPath operators. About XPath expressions and how Docentric utilizes them learn more in the overview tutorial.

Boolean functions

Sample XML fragment

That is used to demonstrate how the Boolean functions work - in the Example column in the below table.

Syntax Return Type Description Example
 boolean(object) boolean Converts the argument to a Boolean. boolean(1) returns True.
boolean(0) returns False.
 false() boolean Returns False. false() returns False.
 lang(string) boolean Returns true if the xml:lang attribute of the context node is the same as the argument string. count(lang("en-US")) returns 1 if the context node has an xml:lang attribute of "en-US".
 not(boolean) boolean Returns True if the argument is false, otherwise, False. not("true") returns False.
 to-boolean(object) boolean Converts the argument to a Boolean. If the argument is a node-set, then it takes the text of the first node and converts it to a Boolean value. It returns True if the text is ‘true’ or ‘1’; Otherwise, it returns False. to-boolean("true") returns True.
 true() boolean Returns True. true() returns True.

Conditional functions

Syntax Return Type Description Example
 iif(object, object, object) object Returns one of two values (the second and the third parameter), depending on whether the Boolean expression (the first parameter) evaluates to true or false. iif(1 > 0, "red", "blue") returns the string "red".
 coalesce(object*) object Accepts a list of parameters and returns the first value from the list that is not an empty string nor empty node set. coalesce("", 1, "bar") returns the string "1".

Context functions

Sample XML fragment

That is used to demonstrate how the Context functions work - in the Example column in the below table.

Syntax Return Type Description Example
 current() node-set Returns the current XML node that is the current data context item. current() returns the current <SalesInvoiceLines> node.
 current-collection() node-set Returns the node-set which is the resulting item collection of the current directly or indirectly wrapping List or Group tagging element. current-collection() returns the current <SalesInvoiceLines> node.
 current-index() node-set Returns the 1-based index of the current data context item in the resulting item collection. The resulting item collection is the product of a List or Group tagging element. current-index() returns 1 or 2.
 data-source(string?) node-set Returns the root XML element of the specified data source. data-source('GeneralData') returns <GeneralData> root element.
 last-processed-group-element-result() node-set Returns all groups created by the last processed Group tagging element. last-processed-group-element-result(SalesInvoiceLines)[1]/@Name returns "Low-Range Speaker".
 var-element-value() - Returns the value of the specified Var tagging element. The argument must be the name of an existing Var tagging element. We create Var-Tagging element with name Counter and value 0. Then, when we use var-element-value("Counter"), it returns 0.

Date-Time functions

Syntax Return Type Description Example
 date-time-add-days(object, object) date-time Returns a date-time value that adds the specified number of days (second parameter) to the given date-time value (first parameter). date-time-add-days("2023-08-20T15:20:00", 3) returns the date-time value "2023-08-23T15:20:00".
 date-time-add-months(object, object) date-time Returns a date-time value that adds the specified number of months (second parameter) to the given date-time value (first parameter). date-time-add-months("2023-08-20T15:20:00", 1) returns the date-time value "2023-09-20T15:20:00".
 date-time-add-years(object, object) date-time Returns a date-time value that adds the specified number of years (second parameter) to the given date-time value (first parameter). date-time-add-years("2023-08-20T15:20:00", 1) returns the date-time value "2024-08-20T15:20:00".
 day(date-time) number Returns the day component of the given date-time. day("2023-08-20T15:20:00") returns the number 20.
 hour(date-time) number Returns the hour component of the given date-time. hour("2023-08-20T15:20:00") returns the number 15.
 millisecond(date-time) number Returns the millisecond component of the given date-time. millisecond("2023-08-20T15:20:00.123") returns the number 123.
 minute(date-time) number Returns the minute component of the given date-time. minute("2023-08-20T15:20:00") returns the number 20.
 month(date-time) number Returns the month component of the given date-time. month("2023-08-20T15:20:00") returns the number 8.
 now() date-time Returns a date-time that is the current date and time on this computer, expressed as the local time. now() returns the current date and time in the local time zone.
 second(date-time) number Returns the second component of the given date-time. second("2023-08-20T15:20:00") returns the number 0.
 to-date-time(date-time) date-time Converts the given date-time formatted string to a date-time value. to-date-time("2023-08-20T15:20:35") returns the date-time value "2023-08-20T15:20:35".
 year(date-time) number Returns the year component of the given date-time. year("2023-08-20T15:20:00") returns the number 2023.

Misc functions

Syntax Return Type Description Example
 label(object) string Returns a label translation for the provided label code. label("@SYS80991") returns Name.
 load-binary(string, string?) string Loads the resource from the specified URL and returns the binary data encoded as Base 64 string. The first parameter accepts the resource URL. The second (optional) parameter accepts a boolean value indicating whether an error due to a failed operation should be suppressed ('false' by default). load-binary("https://ax.docentric.com/image.jpg") returns Base 64 string of the image.jpg.
 load-text(string, string?) string Loads the text file/resource from the specified URL and returns the data as string. The first parameter accepts the resource URL. The second (optional) parameter accepts a boolean value indicating whether an error due to a failed operation should be suppressed ('false' by default). load-text("https://ax.docentric.com/text.txt") returns text data from text.txt as a string.

Node-Set functions

Sample XML fragment

That is used to demonstrate how the Node-Set functions work - in the Example column in the below table.

Syntax Return Type Description Example
 count(node-set) number Returns the number of nodes in the node-set argument. count(SalesInvoiceLines) returns 2.
 id(object) node-set Selects elements by their unique ID. count(SalesInvoiceLines[id(1)]) returns 1.
 index-of(node-set, node-set) number Reports the one-based index of the first occurrence of the specified reference node (second parameter) in the source node set (first parameter). index-of(SalesInvoiceLines, PackingSlip) returns -1.
 last() number Returns a number equal to context size of the expression evaluation context. last() returns 2.
 local-name(node-set?) string Returns the local part of the expanded name of the node in the node-set argument that is first in document order. local-name(SalesInvoiceLines) returns SalesInvoiceLines.
 name(node-set?) string Returns a string containing a QName representing the expanded name of the node in the node-set argument that is first in document order. name(SalesInvoiceLines) returns SalesInvoiceLines.
 names(node-set) node-set Returns a string collection containing a QName representing the expanded name of all the nodes in the given node-set argument. names(SalesInvoiceLines) returns SalesInvoiceLinesSalesInvoiceLines.
 namespace-uri(node-set?) string Returns the namespace Uniform Resource Identifier (URI) of the expanded name of the node in the node-set argument that is first in document order. namespace-uri(ReportData) returns https://ax.docentric.com
 position() number Returns the index number of the node within the parent. position() returns 1.
 skip(node-set, number) node-set Skips the specified number (second parameter) of XML nodes in the given node set (first parameter) and then returns the remaining nodes. skip(SalesInvoiceLines, 1) returns <SalesInvoiceLines LineAmount="80.0000000000" Name="Low-Range Speaker"></SalesInvoiceLines>.
 take(node-set, number) node-set Returns only the first specified number (second parameter) of XML nodes in the given node set (first parameter). take(SalesInvoiceLines, 1) returns <SalesInvoiceLines LineAmount="1920.0000000000" Name="Mid-Range Speaker"></SalesInvoiceLines>.

Number functions

That is used to demonstrate how the Number functions work - in the Example column in the below table.

Syntax Return Type Description Example
 abs(number) number Returns the absolute value of a specified number. abs(-10) returns 10.
 ceiling(number) number Returns the smallest integer that is not less than the argument. ceiling(3.14) returns 4.
 floor(number) number Returns the largest integer that is not greater than the argument. floor(3.14) returns 3.
 number (Object?) number Converts the argument to a number. number("123") returns the number 123.
 round(number) number Returns an integer closest in value to the argument. round(3.14) returns 3.
 round-number(number, number) number Rounds the given decimal value to the specified number of fractional digits. round-number(3.14159, 2) returns 3.14.
 sum(node-set) number Returns the sum of all nodes in the node-set. Each node is first converted to a number value before summing. sum(SalesInvoiceLines/@LineAmount) returns 600.0000000000.

NOTE: To display the resulting number in a particular format (e.g. 600.00), it should be formatted by applying Format String (e.g. "n2").

String functions

Sample XML fragment

That is used to demonstrate how the String functions work - in the Example column in the below table.

Syntax Return Type Description Example
 $cr string Returns a carriage return character. $cr.
 $lf string Returns a new line feed. $lf.
 $new-line string Returns a new line string. $new-line.
 $tab string Returns a tab character. $tab.
 char(number) string Returns the character from the provided ASCII number. char(65) returns the character A.
 concat(string, string, string*) string Returns the concatenation of the arguments. concat("Hello", " ", "World") returns the string Hello World.
 contains(str1, str2) boolean Returns true if the first argument string contains the second argument string; otherwise returns false. contains("Hello World", "World") returns true.
 format(node-set, node-set, node-set?) string Converts the numeric or date-time value to its equivalent string representation, using the specified format. This function accepts 3 parameters. The first one is a number or date-time value to format. The second parameter is a format string. The third parameter is optional and accepts a value representing a culture name (e.g., 'en-US') to be used for the format operation instead of the currently set culture. format (1234567890, "###,###,###,###") returns the string "1,234,567,890".
 get-string-binary(node-set) string Encodes the given string by using the UTF-8 encoding and returns the binary result in the form of a Base 64 string. get-string-binary("Hello World") returns the Base 64 string SGVsbG8gV29ybGQ=.
 lower-case(string) string Returns the given string converted to lowercase. lower-case("Hello World") returns the string "hello world".
 normalize-space(string) string Returns the argument string with the white space stripped. normalize-space(" Hello World ") returns the string "Hello World".
 replace(string, string, string) string Replaces parts of the input string (the first parameter) with the replacement string (the third parameter) that match regular expression pattern. replace("Hello World", "World", "Universe") returns the string "Hello Universe".
 starts-with(string, string) boolean Returns true if the first argument string starts with the second argument string; otherwise returns false. starts-with("Hello", "He") returns True.
 string(object?) string Converts an object to a string. string(123) returns "123".
 string-find(node-set, node-set) number Searches the input string (first parameter) for an occurrence of the search string (second parameter). It returns the character index of the first occurrence of the search string. string-find("Hello World", "e") returns 2.
 string-join(node-set, string, string?) string Returns the string containing the concatenated strings of the given nodes delimited by the string specified by the second parameter. The third parameter is optional and specifies the string which acts as the last delimiter. string-join(SalesInvoiceLines/@Name, " - ") returns "Mid-Range Speaker - Low-Range Speaker".
 string-length(string) number Returns the number of characters in the string. string-length("Hello World") returns 11.
 string-split(string, boolean, string) node-set Returns a node set of strings that contains the substrings of the input string (first parameter) delimited by the specified string (third parameter). If the Remove Empty Entries flag (second parameter) is set to True, then all empty strings will be removed from the result. string-split("Contoso Entertainment System USA", true(), " ") returns "ContosoEntertainmentSystemUSA".
 substring(string, number, number?) string Returns the substring of the first argument starting at the position specified in the second argument and the length specified in the third argument. substring("Hello World", 1, 5) returns "Hello".
 substring-after(string, string) string Returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string. substring-after("Hello World", " ") returns "World".
 substring-before(string, string) string Returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string. substring-before("Hello World", " ") returns "Hello".
 translate(string, string, string) string Returns the first argument string with occurrences of characters in the second argument string replaced by the character at the corresponding position in the third argument string. translate("Hello World","o","M") returns "HellM WMrld".
 trim(node-set) string Removes whitespaces on the left and the right side of the input string and returns the remaining string. If the input string is not provided, the current XML node is used as the input. trim(" Hello World ") returns "Hello World".
 upper-case(string) string Returns the given string converted to uppercase. upper-case("Hello World") returns "HELLO WORLD".

See also

XPath Expressions Overview >>
XPath Selecting and Filtering Nodes >>
XPath Operators >>

IN THIS ARTICLE