xquery - 为什么数字文字永远不能是类型 xs :positiveInteger in XQuery?

标签 xquery xquery-3.1

我注意到 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 作为最顶层的类型会更有意义,但如果允许一个子类型,为什么不一直使用呢?

here is a live demo

最佳答案

我认为这方面的规范非常不幸,但很清楚:一个值是一个 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 xs:positiveInteger 的实例返回 false(因为 xs:integer 不是从 xs:positiveinteger)。

最后,当函数参数的预期类型为 xs:positiveInteger,并且函数调用提供值 3 时,§3.1.5.2 中的函数转换规则开始发挥作用。这些允许从提供的值到所需类型的各种转换,但从 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 - 为什么数字文字永远不能是类型 xs :positiveInteger in XQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60877203/

相关文章:

xml - 将纯文本 block 包裹在 P 中,同时跳过已经包裹在 P 中的 block

xslt-3.0 - tokenize($s) 与 tokenize($s, ' ' ) 相同吗?

sql - 在 SQL 中将 XML 解析为表(仅具有值的节点)

xml - 使用 Xquery 计算元素并返回值

regex - XSLT/XQuery 正则表达式输出的行为是否依赖于实现?

duplicates - 根据XQuery中的子节点选择不同值

php - 如何查询多个XML文件?

xml - 使用 XQuery/XPath 获取祖先节点的逆序