<?xml version="1.0" encoding="UTF-8"?>
<library>
<books>
<book>
<title>The Story of my Life </title>
<accessionNumber val="1"/>
</book>
<book>
<title>XML Schema </title>
<accessionNumber val="2"/>
</book>
<book>
<title>C++ for Image processing</title>
<accessionNumber val="3"/>
</book>
</books>
<loans>
<loan book="1" user="35" dueDate="25-April-2010"/>
</loans>
<loans>
<loan book="3" user="400" dueDate="05-Jan-2010"/>
</loans>
<loans>
<loan book="2" user="02" dueDate="25-Feb-2010"/>
</loans>
<users>
<user>
<name>Fred Jame</name>
<id val="35"/>
</user>
<user>
<name>Ashley</name>
<id val="400"/>
</user>
<user>
<name>Patrick</name>
<id val="02"/>
</user>
</users>
</library>
我的任务是编写XQuery Expression,列出贷款逾期的所有用户。
我必须使用fn:current-date()
函数与dueDate
进行比较才能找出所有逾期用户,但此处的问题是fn:current-date()
格式,而且XML文档中的dueDate
格式完全不同
如何格式化以获得我想要的结果?
for $x in doc("library.xml")/library
where $x/@dueDate > fn:current-date()
return $x/@name
我在上面尝试过但fn:current-date()
将日期输出设为2014-03-30Z
但如果dueDate高于Current-Date,我需要将dueDate与当前日期进行比较,然后显示所有用户。
答案 0 :(得分:0)
由于这显然是一项练习,我不会提供完整的答案,而是草拟解决方案。
tokenize(...)
,将月份解析为数字并将其连接为YYYY-mm-dd
xs:date($string)
将其转换为日期对象。<loan/>
元素,并在where
子句中过滤它们以获取到期贷款,调用您编写的函数或将代码放在此处。<loan/>
个对象解析为相应的用户。这应该可以帮助您编写代码。如果您有任何进一步的具体问题或遇到问题,请随时发布后续问题(与您尝试过的一样)!
更新:这是我的代码,使用index-of
查找月份。 resolveDate
函数在代码中被注释,剩下的查询很简单。
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
答案 1 :(得分:0)
您可以创建如下函数:
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
它应该适合你。