Xpath 以在首选值为 null 时使用备用元素值



我对如何根据 XPATH 条件返回字符串值的"通常"问题很复杂。

我以前成功使用过以下解决方案:返回基于 xpath 条件和 xpath 中的嵌套条件 if else 语句的字符串值

但现在我的 XML 是:

<Events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <EventDetails>
    <AccountingDate>2017-01-01</AccountingDate>
    <PaymentDate>2017-01-31</PaymentDate>
  </EventDetails>
  <EventDetails>
    <AccountingDate>2017-01-01</AccountingDate>
    <PaymentDate xsi:nil="true"/>
  </EventDetails>
</Events>

如果付款日期有值,我需要提取付款日期,否则将会计日期替换为第一个事件,即 2017-01-31 但第二个事件为 2017-01-01。

问题是我被迫使用的解析器是 XPath 1.0,并且无法识别 nilled 函数,如果付款日期为 nil,则布尔值(付款日期(为真。

我希望有人有一个好主意,因为我完全被难住了。

大约 4 小时后终于解决了它......

concat(substring(PaymentDate,   1 div    (boolean(PaymentDate) and PaymentDate!='')),
       substring(AccountingDate,1 div not(boolean(PaymentDate) and PaymentDate!='')))

(它还迎合了付款日期根本不存在的可能性,尽管在我的情况下它总是存在。

现在我只需要让它在三个可能的日期下工作!

您也可以尝试此过滤

EventDetails[PaymentDate != '']/PaymentDate | EventDetails[PaymentDate = '']/AccountingDate

或在实际水平上

PaymentDate[. != ''] | AccountingDate[../PaymentDate = '']

或者,如果当付款日期丢失且 xml 数据顺序固定时工作很重要

PaymentDate[. != ''] | AccountingDate[not(following-sibling::PaymentDate) or following-sibling::PaymentDate = '']

最新更新