Xml xquery fn:current-date()格式

Xml xquery fn:current-date()格式,xml,xquery,Xml,Xquery,我在上面尝试过,但是fn:current-date()将日期输出为2014-03-30Z 但是,如果dueDate高于当前日期,我需要将dueDate与当前日期进行比较,然后显示所有用户。由于这显然是一个练习,我将不提供完整答案,而是提供解决方案的草图 XML文档中使用的日期格式很难比较。创建一个执行以下步骤的函数可能是合理的(但当然也可以不执行这些步骤): 您必须标记(…)它,将月份解析为数字,并将其连接为YYYY-mm-dd 现在可以使用xs:date($string)将其转换为日期对象

我在上面尝试过,但是
fn:current-date()
将日期输出为
2014-03-30Z


但是,如果dueDate高于当前日期,我需要将dueDate与当前日期进行比较,然后显示所有用户。

由于这显然是一个练习,我将不提供完整答案,而是提供解决方案的草图

  • XML文档中使用的日期格式很难比较。创建一个执行以下步骤的函数可能是合理的(但当然也可以不执行这些步骤):
    • 您必须
      标记(…)
      它,将月份解析为数字,并将其连接为
      YYYY-mm-dd
    • 现在可以使用
      xs:date($string)
      将其转换为日期对象
  • 现在循环所有
    元素,并在那里
    where
    子句中过滤它们,调用您编写的函数或将代码放在这里
  • 作为最后一步,您必须将
    对象解析给相应的用户
  • 这将有助于您编写代码。如果您有任何进一步的具体问题或遇到问题,请随时发布后续问题(包括您尝试过的问题)


    更新:这是我的代码,使用的索引查找月份。
    resolveDate
    函数在代码中被注释,剩下的查询很简单

    for $x in doc("library.xml")/library
    where  $x/@dueDate > fn:current-date()
    return $x/@name
    
    将函数local:resolveDate($date作为xs:string)声明为xs:date{
    (:月索引,以便我们可以查找:)
    let$months:=(“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”)
    (:拆分日期:)
    让$date:=标记化($date,'-'))
    (:将月份解析为其两位数:)
    让$month:=将($months,$date[2])的索引转换为xs:string
    设$month:=if(字符串长度($month)<2)
    然后concat('0',$month)
    每月其他$
    (:从重新排序的日期构造xs:date对象:)
    让$date:=字符串联接($date[3],$month,$date[1]),“-”)
    返回xs:date($date)
    };
    对于//user中的$user
    对于//loan[@user=$user/id/@val]中的$loan
    其中本地:resolveDate($loan/@dueDate)<当前日期()
    返回$user
    
    您可以创建如下函数:

    declare function local:resolveDate($date as xs:string) as xs:date {
      (: Month index, so we can look it up :)
      let $months := ("Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
      (: Split up the date :)
      let $date := tokenize($date, '-')
      (: Resolve the month to its two digit number :)
      let $month := index-of($months, $date[2]) cast as xs:string
      let $month := if (string-length($month) < 2)
                    then concat('0', $month)
                    else $month
      (: Construct an xs:date object from the reordered date :)
      let $date := string-join(($date[3], $month , $date[1]), '-')
      return xs:date($date)
    };
    
    for $user in //user
    for $loan in //loan[@user=$user/id/@val]
    where local:resolveDate($loan/@dueDate) < current-date()
    return $user
    

    它应该适合您。

    该函数称为
    tokenize
    ,而不是
    tokenise
    。另外,我会使用标记化破折号,而不是空格:
    tokenize(“2010年4月25日),“-”)
    。我正在尝试使用标记化()将dueDate分解为数字,但会出现错误。请阅读如何发布破折号,以便有人能够真正回答您的问题。由于您的问题是一个新的、独立的问题,最好是实际发布一个,而不是发表评论。您可以这样做,与我在展示自己的努力后添加的代码版本进行比较。再看一看我在回答中描述的解决方案草图。不能直接将XML中提供的日期与当前日期进行比较。
    declare function local:resolveDate($date as xs:string) as xs:date {
      (: Month index, so we can look it up :)
      let $months := ("Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec")
      (: Split up the date :)
      let $date := tokenize($date, '-')
      (: Resolve the month to its two digit number :)
      let $month := index-of($months, $date[2]) cast as xs:string
      let $month := if (string-length($month) < 2)
                    then concat('0', $month)
                    else $month
      (: Construct an xs:date object from the reordered date :)
      let $date := string-join(($date[3], $month , $date[1]), '-')
      return xs:date($date)
    };
    
    for $user in //user
    for $loan in //loan[@user=$user/id/@val]
    where local:resolveDate($loan/@dueDate) < current-date()
    return $user
    
    declare function local:xml_month
    
    ($monthId as xs:string) as xs:string
    {
    if (tokenize($monthId,"-")[2] = 'January')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'January','01'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'February')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'February','02'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'March')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'March','03'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'April')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'April','04'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'May')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'May','05'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'June')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'June','06'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'July')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'July','07'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'August')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'August','08'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'September')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'Sempter','09'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'October')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'October','10'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'November')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'November','11'),"-",
    tokenize($monthId,"-")[3])
    else if (tokenize($monthId,"-")[2] = 'December')
    then  
    concat(  tokenize($monthId,"-")[1],"-",
    replace(tokenize($monthId,"-")[2],'December','12'),"-",
    tokenize($monthId,"-")[3])    
    else "NA"
    };
    
    for $u in doc("ques2010.xml")//user
    for $l in doc("ques2010.xml")//loan    
    where fn:current-date() > xs:date(local:xml_month($l/data(@dueDate)))
    and $u/id/@val = $l/@user
    return $u/name