Elementary Examples
Adding Numbers
Simple expression that computes 1 + 1.
Note that <add> uses floating point arithmetic by default, giving "2.0" as the result. However, when all operands are long values, it'd be better to explicitly instruct <add> to calculate integer operations by specifying the "datatype" attribute: <add datatype="long"> .
<expression xmlns="http://xmlmath.org/1.0">
<add>
<long value="1"/>
<long value="1"/>
</add>
</expression>
|
String Manipulations
This example illustrates how xmlmath can manipulate strings. It shows the use of <strcat> to concatenate two or more strings and <string> to create a literal string.
<expression xmlns="http://xmlmath.org/1.0">
<strcat>
<string value="Hello"/>
<string value=" "/>
<string value="world."/>
</strcat>
</expression>
|
The expression below shows how
<substr>
is used to select substrings. It transforms the string
"unhappy" into "happy".
The operator
<substr>
takes three arguments: first a string and then two
numerical (long) indices. The first marks the beginning
index (inclusive), while the second marks the ending
index (exclusive).
<expression xmlns="http://xmlmath.org/1.0">
<substr>
<string value="unhappy"/>
<long value="2"/>
<strlen>
<string value="unhappy"/>
</strlen>
</substr>
</expression>
|
List Manipulations
The example shows the use of lists in xmlmath. First a list is created using the <list> operator. This list is then fed to <sort> which sorts the elements in the list in ascending order.
The expression yields the following result: [-0.0, 0.0, 2.718281828459045, 3.0, 3.141592653589793, Infinity]
<expression xmlns="http://xmlmath.org/1.0">
<sort>
<list>
<infinity/>
<double value="0"/>
<e/>
<double value="-0"/>
<pi/>
<double value="3"/>
</list>
</sort>
</expression>
|

