Standard XPath functions

Build 1501 on 14/Nov/2017  This topic last edited on: 21/Mar/2016, at 18:52

GNQuery implements the following standard XPath functions:

boolean(expr)

Converts expr to a Boolean value.

true()

Returns the ‘true’ Boolean value.

false()

Returns the ‘false’ Boolean value.

not(expr)

Converts expr to a Boolean value and negates it.

number(expr)

Converts expr to a number. If it is not a valid number returns the special ‘NaN’ (not a number) value.

ceiling(expr)

Converts expr to a number and computes its ceiling – i.e. the smallest integer value that is greater or equal the expression.

floor(expr)

Converts expr to a number and computes its floor – i.e. the greatest integer value that is smaller or equal the expression.

sum(expr)

Converts each node in the expr node-set to a number and adds them all.

round(expr)

Converts expr to a number and rounds it to the nearest integer value.

concat(expr1, expr2, . . .)

Converts all the expression to strings and concatenates them.

contains(expr1, expr2)

Returns true if expr2 is contained as a sub-string in expr1.

In standard XPath expr1 and expr2 are first converted into strings and then the test takes place; GNQuery on the other hand uses the same logic of the relational operators: if one of the expression is a node-set executes the test for each node, and returns true if any of the test is satisfied. This means that in GNQuery the expression

  contains(gn4:keywords/gn4:item,'bridges')

is true if any of the keywords contains ‘bridges’, whereas in standard XPath is true only if the first keyword contains ‘bridges’.

starts-with(expr1, expr2)

Returns true if expr1 (as a string) starts with expr2.

GNQuery uses the same non-standard evaluation logic as for the ‘contains()’ function above.

string(expr)

Converts expr to a string.

string-length(expr)

Converts expr to a string and returns its length.

substring(expr1, expr2)

Converts expr1 to a string, expr2 to a number and returns all the characters in the expr1 string starting at the expr2 position. Positions start from 1.

substring(exp1, expr2, expr3)

Converts expr1 to a string, expr2 and expr3 to numbers and returns expr3 characters in the expr1 string starting at the expr2 position. Positions start from 1.

count(expr)

Returns the number of nodes in the node-set expr. For example the query

  gn4:story[count(gn4:keywords/gn4:item)>=2]

returns all the stories with at least two keywords.

position()

Returns the position, or index number, of the current node relative to all the matching nodes in the node list. It is used to limit the number of results returned by a query, for example

  gn4:story[position()<=5] order by @modifiedDate descending

returns the five most recently modified stories: search all the stories, order them by modification date with the most recent first, get only the first five. Another example:

  gn4:story[contains(gn4:title,’test’)][5<=position() and position()<=10]

returns from all the stories whose title contains ‘test’  those from position 5 to position 10 (both included). When – as in this case – there is no specified ordering the system uses ordering by object id.

The ‘position()’ function can be used only in comparisons with numeric constants; these comparison cannot use the ‘!=’ (not equal to) operator; these comparisons can be combined only using ‘and’; comparisons using ‘position()’ cannot be mixed with other conditions. These limitations mean that all the following queries are illegal

  gn4:story[position()<$p0]    -- Comparison with a variable instead than a constant

  gn4:story[position()<@nWords] -- Comparison with a path instead than a constant

  gn4:story[position()<10 and contains(gn4:title,’test’)]    -- Mixed conditions

  gn4:story[position()<10 or position()>20]    -- Comparisons not combined with ‘and’

  gn4:story[position()!=10]    -- Comparison using the ‘!=’ operator