尝试转换$date
为实际日期/时间:
let $date:= xs:dateTime($item/pubDate/text())
如果日期/时间的格式一致,您可以实现自己的转换器。在您评论的示例中,会给您带来最多问题的两个部分是月份和时区。月份必须是数字,您需要将时区转换为等效的 UTC 偏移量。
这是一个例子:
declare variable $months := ('jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec');
declare variable $timezoneMap :=
<map>
<tz>
<numeric>-05:00</numeric>
<alpha>est</alpha>
</tz>
</map>;
declare function local:formatDate($origdate as xs:string) as xs:dateTime {
let $dateTokens := tokenize($origdate,' ')
let $timezone := $timezoneMap/tz[lower-case($dateTokens[6])=alpha]/numeric/text()
let $month := string(index-of($months,lower-case($dateTokens[3])))
let $newDate := concat($dateTokens[4],'-',if (string-length($month)=1) then concat('0',$month) else $month,'-',$dateTokens[2],
'T',$dateTokens[5],$timezone)
return
xs:dateTime($newDate)
};
然后,您可以像这样使用该函数:
let $date:= local:formatDate($item/pubDate)
此外,如果您使用的是 XQuery 3.0,您可以使用地图作为时区和月份:
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare variable $months := map{"jan":="01","feb":="02","mar":="03","apr":="04","may":="05","jun":="06",
"jul":="07","aug":="08","sep":="09","oct":="10","nov":="11","dec":="12"};
declare variable $timezoneMap := map{"est":="-05:00"};
declare function local:formatDate($origdate as xs:string) as xs:dateTime {
let $dateTokens := tokenize($origdate,' ')
let $newDate := concat($dateTokens[4],'-',$months(lower-case($dateTokens[3])),'-',$dateTokens[2],
'T',$dateTokens[5],$timezoneMap(lower-case($dateTokens[6])))
return
xs:dateTime($newDate)
};