XPath 2.0公式中条件的评估顺序

时间:2019-02-12 20:00:19

标签: xpath-2.0

我有一个使用XPath变量的XPath 2.0公式。该公式位于“ if-then-else”构造中。

if条件本身内部包含一些条件。我将条件按顺序进行评估。

这是我的公式:

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 ()

以Python dict格式提供给elementpath的数据是:

{
 'NetCashFlow': None,
 'NetCashFlowFromFinancingActivities': 369100000,
 'NetCashFlowFromFinancingActivitiesContinuing': 369100000,
 'NetCashFlowFromInvestingActivities': -460300000,
 'NetCashFlowFromOperatingActivities': 207400000
}

这是源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>

我正在Python 3.6中使用elementpath包来评估XPath 2.0,因为这是我发现在Python中评估XPath 2.0的唯一方法。

在我的公式中:

我先检查exists($NetCashFlow),然后尝试检查: and not($NetCashFlow - ($NetCashFlowFromOperatingActivities + $NetCashFlowFromFinancingActivities + (if (exists($ExchangeGainsLosses)) then $ExchangeGainsLosses else 0)) = 0)

我假设如果$ NetCashFlow为nil,则不会计算下一个表达式。但是,我收到一条错误消息,提示我正在尝试使用不存在的值(即$ NetCashFlow-...

)进行操作

如何编写此公式,以便首先检查exists($NetCashFlow),如果该值为假,则不对表达式的其余部分求值。

基于我应该使用的括号是否存在顺序优先级?

0 个答案:

没有答案