So how to handle nulls? We now have a decent section in the user guide on handling nulls buts here’s a couple.Lets assume we have the following XML:
<AMOUNTS> <AMOUNT>100</AMOUNT> <AMOUNT></AMOUNT> <AMOUNT>200</AMOUNT> <AMOUNT>300</AMOUNT> <AMOUNT></AMOUNT> </AMOUNTS>
If we just do a simple summary to add up the amounts sum(.//AMOUNT) we would get the NaN output. We have two options to get around this:
1. Check for the Null first
We can use an XPATH expression to test the value before adding it:
<?sum(.//AMOUNT[.!=''])?>
yes, its effectively testing for an empty string but this will give us the correct answer i.e. 600
2. Use the BIP Sum function
A little less effort, we can remember to use the BIP built in function xdoxslt:sum. This will check for null values for you.
<?xdoxslt:sum(AMOUNT)?>
Both options work of course, of the two for simple summing either will work fine, if you have a more complex calculation then the second option is going to be better. Thats not to sat you could not use the XPATH but things start to get tedious checking all your values for nulls when the BIP sum function will do it for you. Before you ask, yes the Template Builder should give you the option and insert it for you.