【问题标题】:function return type issue函数返回类型问题
【发布时间】:2012-01-06 09:44:13
【问题描述】:

根据 w3c

ElementTest 用于通过名称和/或类型注释来匹配元素节点。 ElementTest 可以采用以下任何一种形式。在这些形式中,ElementName 不需要出现在范围内的元素声明中,但 TypeName 必须出现在范围内的模式类型中 [err:XPST0008]。请注意,替换组不会影响 ElementTest 的语义。 ... element(*, TypeName) 匹配一个元素节点,不管它的名字是什么,如果deriveds-from(AT, TypeName )为真,其中AT为元素节点的类型注解,节点的nilled属性为假。

我有这个功能

import schema namespace cdm-base="http://cdm.basic.upc.com" at "file:///Workspace/peal/peal40/trunk/common/schema/cdm-basic.xsd";
declare function local:matchType(

                    $input as element()

                    ) as element(*,cdm-base:ProductComponent..) {

                    <cdm-base:product xsi:type="cdm-base:ProductComponent" />


};

在我输入时返回错误:

F [Saxon-EE XQuery 9.3.0.5] 函数 local:matchType() 的必需项类型为 element(*, ProductComponent);提供的值具有项目类型元素({http://cdm.basic.upc.com}product, {http://www.w3.org/2001/XMLSchema}untyped)

我可能弄错了,但类型实际上是 cdm-base:ProductComponent 而不是无类型的。 我不明白问题出在哪里......

我在 Saxon EE 9.3.0.5 中使用 Oxygen 13.0

【问题讨论】:

标签: xquery


【解决方案1】:

Saxon 在这里确实是正确的,所有直接构造的(“内联”)元素都具有 xs:untyped 类型(如果构造模式设置为保留,则为 xs:anyType)。

xsi:type 元素在根据您的模式验证元素之前是没有意义的。最简单的方法是将元素包装在验证表达式中:

import schema namespace cdm-base="http://cdm.basic.upc.com" at "file:///Workspace/peal/peal40/trunk/common/schema/cdm-basic.xsd";

declare function local:matchType(
                   $input as element())
                   as element(*,cdm-base:ProductComponent)
{
    validate { <cdm-base:product xsi:type="cdm-base:ProductComponent" /> }
};

请注意,在 XQuery 3.0 中,如果您实际上不需要 xsi:type 属性,那么您可以将元素验证为特定类型:

import schema namespace cdm-base="http://cdm.basic.upc.com" at "file:///Workspace/peal/peal40/trunk/common/schema/cdm-basic.xsd";

declare function local:matchType(
                   $input as element())
                   as element(*,cdm-base:ProductComponent)
{
    validate type cdm-base:ProductComponent { <cdm-base:product /> }
};

【讨论】:

    猜你喜欢
    • 2020-01-07
    • 2011-07-01
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    相关资源
    最近更新 更多