so i have some noisy xml input data and i would like to cast it to xs:gYear since all of it is dates.
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>
First I thought: let's use cast as:
for $n in $dates/*
return if ($n castable as xs:gYear) then ($n cast as xs:gYear)
else ("boo")
which returns valid gYear ints as xs:gYear not quite what i wanted:
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")
works except for the year '0'. How do i get that to return "", since 0000 is also no valid year, and while the data contains historical dates, none of if is julian calendar or any other format containing a year 0.
was or was my first idea on track and cast as should actually convert e.g. 123 into 0123?