【问题标题】:Xquery to compare the date values in an array and get the max date valueXquery 比较数组中的日期值并获取最大日期值
【发布时间】:2019-01-28 01:48:17
【问题描述】:

在我的 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

【问题讨论】:

    标签: date xslt compare xquery


    【解决方案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>
    

    【讨论】:

    • 谢谢迈克尔。我完全删除了 if 条件,并尝试了您提到的语句 .ie..e {max($SA_INFO_LISTrow[@SP_TYPE_CD != '']/@EndDate/xs:date(.))} ns1:日期>。但我收到错误消息第 21 行,第 74 列:{w3.org/2005/xqt-errors}FORG0001: "": invalid value for cast/constructor: {w3.org/2001/XMLSchema}date: error: date: Invalid date value: wrong type:
    • 如果我更正 if 语句和 xquery:对于 $StartServiceAllowedResponse/ns2:CMServiceAgreementByAccountSearchService/ns2:CMServiceAgreementByAccountSearchServiceDetails/ns2:SA_INFO_LIST/ns2:SA_INFO_LISTRow 中的 $SA_INFO_LISTrow 返回 if ((($SA_INFO_LISTRow/@SP_TYPE_CD) !='') 和 fn:max(xs:string($SA_INFO_LISTrow/@EndDate))) 然后 {max($SA_INFO_LISTrow[@SP_TYPE_CD != '']/@EndDate/xs:date(. ))} else () 。它会运行,但结果会返回 SP_TYPE_CD != '' 的所有日期。它没有得到最大日期。我正在使用 xquery1.0
    • 我没有注意到您的某些 @EndDate 值实际上并不是日期。除非您提供更精确的规范,否则我认为我不能再给您任何代码了。
    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2023-03-14
    相关资源
    最近更新 更多