首页 > 解决方案 > Xquery 比较数组中的日期值并获取最大日期值

问题描述

在我的 xquery 中,我有条件检查数组中是否 (SP_TYPE_CD!="") 和 (max of the EndDate) 并返回满足此条件的 EndDate

要求:

`<CMS xmlns="*******************">
     <CMSService>          
        <CMSDetails AccountID="123456" CR="1000">
           <SA_INFO_LIST>
              <SA_INFO_LISTRow SA_ID="3484598047" ServiceAgreementType="OOVRPAY" ServicePointType="" SP_TYPE_CD="" Status="60" StartDate="2018-09-27" EndDate="2018-09-27"/>
              <SA_INFO_LISTRow SA_ID="3486640145" ServiceAgreementType="OOVRPAY" ServicePointType="" SP_TYPE_CD="" Status="60" StartDate="2018-04-26" EndDate="2018-04-26"/>
              <SA_INFO_LISTRow SA_ID="3487463777" ServiceAgreementType="ERES" ServicePointType="3135182884" SP_TYPE_CD="RESE" Status="70" StartDate="2018-04-06" EndDate=""/>
              <SA_INFO_LISTRow SA_ID="3482685560" ServiceAgreementType="OOVRPAY" ServicePointType="" SP_TYPE_CD="" Status="60" 
           </SA_INFO_LIST>
        </CMSServiceDetails>
     </CMSService>
  </CMS>

我的 Xquery:

for $SA_INFO_LISTRow in $StartServiceAllowedResponse/ns2:CMSService/ns2:CMSServiceDetails/ns2:SA_INFO_LIST/ns2:SA_INFO_LISTRow
    return

        if (($SA_INFO_LISTRow/@SP_TYPE_CD)and fn:max($SA_INFO_LISTRow/@EndDate))
        then <ns1:date>{(fn:data($SA_INFO_LISTRow/@EndDate)}</ns1:date>
        else ()

我在 jdeveloper 中运行 xquery 时收到错误消息

FORG0001: "2018-09-27": invalid value for cast/constructor: {http://www.w3.org/2001/XMLSchema}double: error: double: Invalid double value: 2018-09-27

标签: datexsltcomparexquery

解决方案


除非您的查询是模式感知的,否则@endDate 属性(原子化之后)将是 xs:untypedAtomic,并且 max() 函数会尝试将 xs:untypedAtomic 值转换为日期。您需要告诉查询处理器将值视为日期,您可以通过使查询模式感知或(更简单地)通过显式转换来做到这一点:

fn:max($SA_INFO_LISTRow/@EndDate/xs:date(.))

但是,您的查询还有其他问题。这个条件:

if (($SA_INFO_LISTRow/@SP_TYPE_CD) and fn:max($SA_INFO_LISTRow/@EndDate))

(更正后)只是询问是否存在最大日期,如果有任何日期,那么就会有一个最大值,所以这是相当没有意义的。

此外,您说您正在寻找@SP_TYPE_CD 不等于“”的条目,但您的代码正在寻找该属性存在的所有条目,而不管其值如何。

我猜你实际上想要@SP_TYPE_CD 不等于“”的所有条目的最大结束日期,那就是(替换你的整个查询)

    <ns1:date>
      {max(//SA_INFO_LISTRow[@SP_TYPE_CD != '']/@EndDate/xs:date(.))}
    </ns1:date>

推荐阅读