Basix XML Expression Evaluation

Some XML Elements like Set, Switch, If, Cond make use of attribute 'expr' to permit to make decisions based on expression evaluation.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
<IVR>
  <GetDigits numDigits="1" validDigits="0123456789">
    <Speak voice="en-US-Standard-C">Please press one key</Speak>
  </GetDigits>
  <If expr="Digits == '7'">
    <Then>
      <Speak voice="en-US-Standard-C">You pressed the lucky number 7</Speak>
    </Then>
    <Else>
      <Speak voice="en-US-Standard-C">You pressed {{Digits}} which multiplied by 3 gives {{int(Digits) * 3}}</Speak>
    </Else>
  </If>
</IVR>

Also, as shown above the result of expression evaluation can be interpolated into strings using handlebars.

There are several operators and functions that can be used in expression evaluations. They are listed here.

Operators

+ (adds two things):

"1 + 1" -> 2

- (subtract two things):

"100 - 1" -> 99

/ (divide one thing by another):

"100/10" -> 10

* (multiple one thing by another):

"10 * 10" -> 100

** (power):

"2 ** 10" -> 1024

% (modulus):

"15 % 4" -> 3

== (equals):

"15 == 4" -> False

in (is something contained within something else?):

"'one' in ['two', 'three']" -> false

GT, GE, LT, LE (DEPRECATED)

Since XML syntax uses '<' and '>' to indicate tags, we cannot freely use expressions like this:

"myvar < 10"

and we would need to escape them like this:

"myvar &lt; 10"

which is confusing.

So we provide the following operators:

GT : > (greater than)

GE : >= (greater than or equal to)

LT : < (less than)

LE : <= (less than or equal to)

and so they can be used like this:

"myvar < 10"

DEPRECATED: the support for GT/GE/LT/LE will be eventually removed.

Instead use the reverse comparison like this:

"10 > myvar"

The above will work because only '<' is not freely allowed in XML.

or use a negated expression:

"not (myvar > 10)"

Functions

str(x)

converts something to a string:

"str(123)" => '123'

int(s)

converts a numeric string into an integer:

"int('123')" => 123

float(s)

converts a numeric string into a float:

"float('123.4')" => 123.4

len(x)

returns the length of a string or list

"len('abc')" => 3

now()

gets current date/time as a string:

"dt = now()" => '2021-03-29 10:32:45'

year(dt)

extracts the year from a date/time string:

"year('2021-03-29 10:32:45')" => 2021

month(dt)

extracts the month from a date/time string:

"month('2021-03-29 10:32:45')" => 3

day(dt)

extracts the day of the month from a date/time string:

"day('2021-03-29 10:32:45')" => 29

hour(dt)

extracts the hour from a date/time string:

"hour('2021-03-29 10:32:45')" => 10

minute(dt)

extracts the minute from a date/time string:

"minute('2021-03-29 10:32:45')" => 32

second(dt)

extracts the second from a date/time string:

"second('2021-03-29 10:32:45')" => 45

month_day(dt)

generates a list [month, day] from a date/time string:

`"month_day('2021-03-29 10:32:45')" => [3,29]

month_name(dt)

returns the name of the month from a date/time string:

"month_name('2021-03-29 10:32:45')" => 'March' ('January' | 'February' | ... | 'December')

day_of_week(dt)

returns the day of the week from a date/time string:

"day_of_week('2021-03-29 10:32:45')" => 'MON' ('MON' | 'TUE' | 'WED' | 'THU' | 'FRI' | 'SAT' | 'SUN')

day_of_year(dt)

returns the day of the year from a date/time string:

"day_of_year('2021-02-01 10:32:45')" => 32

date_time_diff(dt1, dt2)

returns the difference in seconds between two date/time strings:

"date_time_diff('2021-02-01 10:32:55', '2021-02-01 10:32:45)" => 10

date_time_format(dt, format)

returns a string with a date/time formated as specified:

"date_time_format('2021-02-01 10:32:55', '%m/%D/%Y')" => '02/01/2021'

time_in_range(dt, start, end)

returns true if the provided date/time string is within the specified range:

"time_in_range('2021-02-01 10:32:55', '10:00:00', '12:00:00')" => true

Obs: the start is inclusive and the end is exclusive so:

"time_in_range('2021-02-01 12:59:59', '10:00:00', '13:00:00')" => true

but:

"time_in_range('2021-02-01 13:00:00', '10:00:00', '13:00:00')" => false

match_time_in_range(dt, start, end)

returns data associated with a time range if the provided date/time string is within its specified range:

"match_time_in_range('2021-02-01 22:32:55', [['09:00:00','22:00:00','data1'], ['22:00:00','17:00:00','data2'], ['17:00:00', '09:00:00','data3']])" => 'data2'

get_entry(d, key, default=null)

get entry from a JSON object:

"get_entry({'a': 1, 'b': 2}, 'b')" => 2

"get_entry({'a': 1, 'b': 2}, 'z', 'NOT_FOUND')" => 'NOT_FOUND'

contains(s, subs)

checks if a string contains a substring:

"contains('aabbcc', 'bb')" => true

contains_any_of(s, substring_list)

checks if a string contains any substring within a list:

"contains_any_of('aabbcc', ['bb', 'cc'])" => true

iif(expr, true_part, false_part)

returns one of two parts, depending on the evaluation of an expression:

"iif(5 > 7, 'yes', 'no')" => 'no'

shuffle(l)

shuffles the elements in a list:

"shuffle([1,2,3])" => [3,1,2] (but it would be random)

sort(l)

sort the elements in a list:

"sort([3,1,2])" => [1,2,3]

split(s, separator)

splits a string on a separator:

"split('a,b,c', ',')" => ['a', 'b', 'c']

join(l, separator)

joins a list into a string:

"join(['a','b','c'], ',')" => 'a,b,c'

jq_first(o, query)

gets the first element of an object that matches the query:

"jq_first({'a': {'b': {'c': 10}}, '.a.b.c')" => 10

"jq_first({'a': {'b': {'c': 10}}, '.a.b.ccc')" => null

(see jq-tutorial)

jq_all(o, query)

gets all element of an object that matches the query:

"jq_all({'a': {'b': {'c': 10}}, '.a.b.c')" => [10]

"jq_all({'a': {'b': {'c': 10}}, '.a.b.ccc')" => []

(see jq-tutorial)

shift(l)

removes the first element of a list (it changes the input parameter) and returns it:

"shift([1,2,3])" => 1

unshift(l, element)

add an element to the beginning of a list (it changes the input parameter):

"unshift([1,2,3], 0)" => [0,1,2,3]

pop(l)

removes the last element of a list (it changes the input parameter) and returns it:

"pop([1,2,3])" => 3

push(l, element)

add an element to the end of a list (it changes the input parameter):

"push([1,2,3], 4)" => [1,2,3,4]

lowercase(s)

converts a string to lowercase:

"lowercase('ABC')" => 'abc'

uppercase(s)

converts a string to uppercase:

"uppercase('abc')" => 'ABC'

hiraganize(s)

converts a japanese string to hiragana:

"hiraganize('横浜')" => 'よこはま'

katakanize(s)

converts a japanese string to katakana:

"katakanize('横浜')" => 'ヨコハマ'