为什么 XQuery 中的数字文字永远不能是 xs:positiveInteger 类型?
Why can a numeric literal never be of type xs:positiveInteger in XQuery?
我注意到 XQuery 实现处理(子)类型的方式存在细微差别。特别是,处理文字数字作为声明了可接受输入类型的函数的输入。
我天真地认为任何可转换为该特定数字类型的数字文字都会被接受。
declare function local:any ($n as xs:anyAtomic) { $n };
declare function local:decimal ($n as xs:decimal) { $n };
declare function local:integer ($n as xs:integer) { $n };
declare function local:pos-int ($n as xs:positiveInteger) { $n };
local:any(1), (: works :)
local:decimal(1), (: works :)
local:integer(1), (: works :)
local:pos-int(1) (: throws in all tested implementations :)
exist-db 允许 xs:long
,xs:int
, ... Saxon 不允许。
我在 Xquery 规范中找不到该行为的任何原因 2.5.5 SequenceType Matching
也不是 Xpath 函数规范 1.6.3 Atomic Type Hierarchy
这里有人能解释一下为什么 Saxon 9.3.1 HE、BaseX 9.3.1 [Standalone] 和 eXist 5.3.0-SNAPSHOT 会这样吗?
我是不是错过了规范中定义将文字 1
强制转换为 xs:integer 的部分?
xs:decimal 作为最顶层的类型会更有意义,但如果允许一种子类型,为什么不一直使用呢?
您可以将数字文字 1
传递给 MarkLogic 中的 local:pos-int()
函数:
declare function local:any($n as xs:anyAtomicType ) { $n };
declare function local:decimal($n as xs:decimal) { $n };
declare function local:integer($n as xs:integer) { $n };
declare function local:pos-int($n as xs:positiveInteger) { $n };
local:any(1), (: works :)
local:decimal(1), (: works :)
local:integer(1), (: works :)
local:pos-int(1) (: works fine in MarkLogic :)
并且你可以使用xdmp:type()
来报告返回的值是类型positiveInteger
xquery version "1.0-ml";
declare function local:pos-int($n as xs:positiveInteger) { $n };
xdmp:type(local:pos-int(1))
https://www.w3.org/TR/xpath-31/#promotion 指定允许的促销类型:
Numeric type promotion:
A value of type xs:float (or any type derived by restriction from
xs:float) can be promoted to the type xs:double. The result is the
xs:double value that is the same as the original value.
A value of type xs:decimal (or any type derived by restriction from
xs:decimal) can be promoted to either of the types xs:float or
xs:double. The result of this promotion is created by casting the
original value to the required type. This kind of promotion may cause
loss of precision.
对于其他类型,您必须显式使用构造函数,例如local:int(xs:int(1))
.
我认为这方面的规范非常不幸,但很明显:一个值只有在被标记为 xs:positiveInteger
时才是 xs:positiveInteger
,而不仅仅是因为它是 (a) 整数和 ( b) 阳性。 XQuery 工作组对此进行了长时间的讨论,其中包括编程语言类型系统方面的一些著名专家(如 Phil Wadler),这就是做出的决定。我自己也不喜欢。
规范在哪里说的? XDM 规范中的定义是一个好的开始:
https://www.w3.org/TR/xpath-datamodel-31/#xs-types
[Definition: An atomic value is a value in the value space of an
atomic type and is labeled with the name of that atomic type.]
[Definition: An atomic type is a primitive simple type or a type
derived by restriction from another atomic type.] (Types derived by
list or union are not atomic.)
[Definition: The primitive simple types are the types defined in 2.1.1
Types adopted from XML Schema.]
然后 XQuery 规范中的 §3.1.1 讨论了数字文字:
The value of a numeric literal containing no "." and no e or E
character is an atomic value of type xs:integer.
§3.18.1 给出了 "instance of" 运算符的规则:
The boolean operator instance of
returns true if the value of its
first operand matches the SequenceType in its second operand,
according to the rules for SequenceType matching;
和§2.5.5.2给出了SequenceType匹配的相关规则:
An ItemType consisting simply of an EQName is interpreted as an
AtomicOrUnionType. The expected type AtomicOrUnionType matches an
atomic value whose actual type is AT if derives-from( AT,
AtomicOrUnionType ) is true.
综合起来,效果就是表达式3 instance of xs:positiveInteger
returns false(因为xs:integer
不是从xs:positiveinteger
推导出来的)。
最后,当函数参数的预期类型为 xs:positiveInteger
且函数调用提供值 3 时,§3.1.5.2 中的函数转换规则开始发挥作用。这些允许从提供的值到所需类型的各种转换,但是 "down-casting" 从 xs:integer 到 xs:positiveInteger 不是其中之一。所以这是一个错误:
If, after the above conversions, the resulting value does not match
the expected type according to the rules for SequenceType Matching, a
type error is raised [err:XPTY0004].
正如我所说,我不喜欢这些规则,并曾多次尝试改变它们。但是它们很明确,任何不遵循它们的产品都是不合格的。
我注意到 XQuery 实现处理(子)类型的方式存在细微差别。特别是,处理文字数字作为声明了可接受输入类型的函数的输入。 我天真地认为任何可转换为该特定数字类型的数字文字都会被接受。
declare function local:any ($n as xs:anyAtomic) { $n };
declare function local:decimal ($n as xs:decimal) { $n };
declare function local:integer ($n as xs:integer) { $n };
declare function local:pos-int ($n as xs:positiveInteger) { $n };
local:any(1), (: works :)
local:decimal(1), (: works :)
local:integer(1), (: works :)
local:pos-int(1) (: throws in all tested implementations :)
exist-db 允许 xs:long
,xs:int
, ... Saxon 不允许。
我在 Xquery 规范中找不到该行为的任何原因 2.5.5 SequenceType Matching 也不是 Xpath 函数规范 1.6.3 Atomic Type Hierarchy
这里有人能解释一下为什么 Saxon 9.3.1 HE、BaseX 9.3.1 [Standalone] 和 eXist 5.3.0-SNAPSHOT 会这样吗?
我是不是错过了规范中定义将文字 1
强制转换为 xs:integer 的部分?
xs:decimal 作为最顶层的类型会更有意义,但如果允许一种子类型,为什么不一直使用呢?
您可以将数字文字 1
传递给 MarkLogic 中的 local:pos-int()
函数:
declare function local:any($n as xs:anyAtomicType ) { $n };
declare function local:decimal($n as xs:decimal) { $n };
declare function local:integer($n as xs:integer) { $n };
declare function local:pos-int($n as xs:positiveInteger) { $n };
local:any(1), (: works :)
local:decimal(1), (: works :)
local:integer(1), (: works :)
local:pos-int(1) (: works fine in MarkLogic :)
并且你可以使用xdmp:type()
来报告返回的值是类型positiveInteger
xquery version "1.0-ml";
declare function local:pos-int($n as xs:positiveInteger) { $n };
xdmp:type(local:pos-int(1))
https://www.w3.org/TR/xpath-31/#promotion 指定允许的促销类型:
Numeric type promotion:
A value of type xs:float (or any type derived by restriction from xs:float) can be promoted to the type xs:double. The result is the xs:double value that is the same as the original value.
A value of type xs:decimal (or any type derived by restriction from xs:decimal) can be promoted to either of the types xs:float or xs:double. The result of this promotion is created by casting the original value to the required type. This kind of promotion may cause loss of precision.
对于其他类型,您必须显式使用构造函数,例如local:int(xs:int(1))
.
我认为这方面的规范非常不幸,但很明显:一个值只有在被标记为 xs:positiveInteger
时才是 xs:positiveInteger
,而不仅仅是因为它是 (a) 整数和 ( b) 阳性。 XQuery 工作组对此进行了长时间的讨论,其中包括编程语言类型系统方面的一些著名专家(如 Phil Wadler),这就是做出的决定。我自己也不喜欢。
规范在哪里说的? XDM 规范中的定义是一个好的开始:
https://www.w3.org/TR/xpath-datamodel-31/#xs-types
[Definition: An atomic value is a value in the value space of an atomic type and is labeled with the name of that atomic type.]
[Definition: An atomic type is a primitive simple type or a type derived by restriction from another atomic type.] (Types derived by list or union are not atomic.)
[Definition: The primitive simple types are the types defined in 2.1.1 Types adopted from XML Schema.]
然后 XQuery 规范中的 §3.1.1 讨论了数字文字:
The value of a numeric literal containing no "." and no e or E character is an atomic value of type xs:integer.
§3.18.1 给出了 "instance of" 运算符的规则:
The boolean operator
instance of
returns true if the value of its first operand matches the SequenceType in its second operand, according to the rules for SequenceType matching;
和§2.5.5.2给出了SequenceType匹配的相关规则:
An ItemType consisting simply of an EQName is interpreted as an AtomicOrUnionType. The expected type AtomicOrUnionType matches an atomic value whose actual type is AT if derives-from( AT, AtomicOrUnionType ) is true.
综合起来,效果就是表达式3 instance of xs:positiveInteger
returns false(因为xs:integer
不是从xs:positiveinteger
推导出来的)。
最后,当函数参数的预期类型为 xs:positiveInteger
且函数调用提供值 3 时,§3.1.5.2 中的函数转换规则开始发挥作用。这些允许从提供的值到所需类型的各种转换,但是 "down-casting" 从 xs:integer 到 xs:positiveInteger 不是其中之一。所以这是一个错误:
If, after the above conversions, the resulting value does not match the expected type according to the rules for SequenceType Matching, a type error is raised [err:XPTY0004].
正如我所说,我不喜欢这些规则,并曾多次尝试改变它们。但是它们很明确,任何不遵循它们的产品都是不合格的。