0

所以我有一些嘈杂的 xml 输入数据,我想将其转换为 xs:gYear,因为所有这些都是日期。

let $dates := 
<date>
    <a>-1234</a>
    <b/>    
    <c>1911</c> 
    <d>786</d>
    <e>-90</e>
    <f>0</f>
    <g>0302</g>
    <h>-0987</h>
</date>

首先我想:让我们使用 cast 为:

for $n in $dates/*
return if ($n castable as xs:gYear) then ($n cast as xs:gYear) 
else ("boo")

它返回有效的 gYear 整数作为 xs:gYear 不是我想要的:

declare function local:isodate ($string as xs:string)  as xs:string* {
        if (empty($string)) then ()
        else if (starts-with($string, "-")) then (concat('-',(concat (string-join((for $i in (string-length(substring($string,2)) to 3) return '0'),'') , substring($string,2)))))
        else (concat (string-join((for $i in (string-length($string) to 3) return '0'),'') , $string))
    };
   return local:isodate("-1234 ,'', 1911, 786, -90, 0, 0302, -0987")

除了“0”年之外的作品。我如何让它返回“”,因为 0000 也不是有效的年份,虽然数据包含历史日期,但都不是儒略历或任何其他包含年份 0 的格式。

我的第一个想法是或者是我的第一个想法,实际上应该将例如 123 转换为 0123?

4

2 回答 2

2

XSD 1.0 says that there is no year zero; XSD 1.1 falls into line with ISO 8601 and says that there is. This follows the convention used by astronomers rather than the convention used by historians: see https://en.wikipedia.org/wiki/0_(year) for background.

For XQuery it's implemenetation-defined whether the XSD 1.0 or XSD 1.1 rules are used. I don't know which one eXist-DB follows.

于 2014-09-15T22:02:00.983 回答
1

这样的事情怎么样?

declare function local:as-year($year as xs:string) as xs:gYear? {
    let $y := number($year)
    return
        if($y lt 0)then
            concat("-", substring(string(10000 + $y * -1), 2)) cast as xs:gYear
        else if($y gt 0)then
            substring(string(10000 + $y), 2) cast as xs:gYear
        else()
};


let $dates := 
    <date>
        <a>-1234</a>
        <b/>    
        <c>1911</c> 
        <d>786</d>
        <e>-90</e>
        <f>0</f>
        <g>0302</g>
        <h>-0987</h>
    </date>
return
    for $n in $dates/*
    return   
        local:as-year($n)
于 2014-09-15T16:51:03.467 回答