Xquery比较数组中的日期值并获取最大日期值

时间:2019-01-28 01:48:17

标签: date xslt compare xquery

在我的xquery中,我有条件检查数组中是否为(SP_TYPE_CD!=“”)和(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

1 个答案:

答案 0 :(得分:1)

除非您的查询支持架构,否则@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>