I have an XPath 2.0 formula using XPath variables. The formula is in the "if-then-else" construct.
The if condition itself has a few conditions inside it. I put the conditions in the order I want them to be evaluated.
Here is my formula:
if (empty($NetCashFlowFromInvestingActivities) and exists($NetCashFlowFromOperatingActivities) and exists($NetCashFlowFromFinancingActivities) and exists($NetCashFlow) and not($NetCashFlow - ($NetCashFlowFromOperatingActivities + $NetCashFlowFromFinancingActivities + (if (exists($ExchangeGainsLosses)) then $ExchangeGainsLosses else 0)) = 0)) then ($NetCashFlow - ($NetCashFlowFromOperatingActivities + $NetCashFlowFromFinancingActivities + (if (exists($ExchangeGainsLosses)) then $ExchangeGainsLosses else 0))) else ()
The data supplied to elementpath in Python dict format is:
{
'NetCashFlow': None,
'NetCashFlowFromFinancingActivities': 369100000,
'NetCashFlowFromFinancingActivitiesContinuing': 369100000,
'NetCashFlowFromInvestingActivities': -460300000,
'NetCashFlowFromOperatingActivities': 207400000
}
Here is the source xml:
<root>
<formula>
<variable name="Equity" select="if (empty($Equity) and exists(($EquityAttributableToParent, $EquityAttributableToNoncontrollingInterest))) then sum(($EquityAttributableToParent, $EquityAttributableToNoncontrollingInterest)) else ()"/>
<variable name="OperatingMargin" select="if ((not(exists($OperatingMargin))) and (exists($Revenues) and exists($OperatingExpense))) then (sum(($Revenues, -$OperatingExpense))) else $OperatingMargin"/>
<variable name="NetCashFlowFromInvestingActivities" select="if (empty($NetCashFlowFromInvestingActivities) and exists($NetCashFlowFromOperatingActivities) and exists($NetCashFlowFromFinancingActivities) and exists($NetCashFlow) and not($NetCashFlow - ($NetCashFlowFromOperatingActivities + $NetCashFlowFromFinancingActivities + (if (exists($ExchangeGainsLosses)) then $ExchangeGainsLosses else 0)) = 0)) then ($NetCashFlow - ($NetCashFlowFromOperatingActivities + $NetCashFlowFromFinancingActivities + (if (exists($ExchangeGainsLosses)) then $ExchangeGainsLosses else 0))) else ()"/>
</formula>
</root>
I am using elementpath package in Python 3.6 to evaluate the XPath 2.0, as that was the only way I could find that evaluates XPath 2.0 in Python.
In my formula:
I check for exists($NetCashFlow)
first and then try to check:
and not($NetCashFlow - ($NetCashFlowFromOperatingActivities + $NetCashFlowFromFinancingActivities + (if (exists($ExchangeGainsLosses)) then $ExchangeGainsLosses else 0)) = 0)
I assumed that if $NetCashFlow is nil, the next expression will not be evaluated. But, I am getting an error which says that I am trying an operation with a non-existent value, i.e., $NetCashFlow - ...
How can I write this formula so that exists($NetCashFlow)
is checked first and if that is false, the rest of the expression is not evaluated.
Is there an order precedence based on parentheses that I should be using?