在今天的工作,我们一起扔了这样的尝试:如何将8位整数转换为xquery中的二进制字符串?
xquery version "1.0";
declare option saxon:output "omit-xml-declaration=yes";
declare variable $x := 99;
string-join(
for $b in (128,64,32,16,8,4,2,1)
let $xm := $x mod ($b*2)
return
if ($xm >= $b) then "1" else "0"
, "")
你有更好的办法?
以奥利弗的答案,我做了相反的功能。
declare function local:bin-byte($x as xs:string) as xs:unsignedByte
{
let $binary-nibbles := ("0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111")
return xs:unsignedByte(
(index-of($binary-nibbles, substring($x,1,4))-1) * 16
+ (index-of($binary-nibbles, substring($x,5,4))-1)
)
};
我们考虑过这个问题,但没有尝试。我怀疑(这对我们来说可能很重要),简单而快速。 我们的应用程序的输出将是XML或文本。在这种情况下,它可能是XML。 – philcolbourn 2010-04-22 11:55:05
应该xs:byte是xs:unsignedByte? – philcolbourn 2010-04-22 12:30:22
你是对的;我做了这个改变 – 2010-04-22 14:38:09