date - 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
解决方案
除非您的查询是模式感知的,否则@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>
推荐阅读
- php - 在 chrome 中工作得很好,但在 IE(Internet Explorer)中却不行
- wcf - WCF 端口 8080 中的 WSHttpBinding 正被另一个应用程序使用
- scala - 满足特定条件后如何停止 Flink Job
- python - 如何读取目录的子文件夹并在 django 视图中写入文本文件
- android - 使用 Delphi XE 10.3 Rio 在 Android 上更改推送通知中的小图标
- c++ - 从 dll 导出信息并创建类的实例
- php - 如何使用 Laravel Socialite 链接社交媒体帐户
- python-3.x - 如何通过在 Windows 中选择 anaconda 或 Python 从命令提示符运行 Python 文件?
- json - 可以向 JSON Patch 文档添加额外的属性吗?
- reactjs - 如何在 react 中使用 graphene-django 和 axios 将图像上传到我的服务器?