2012-01-06 88 views
0

根據W3C函數返回類型問題

An ElementTest is used to match an element node by its name and/or type annotation. An ElementTest may take any of the following forms. In these forms, ElementName need not be present in the in-scope element declarations, but TypeName must be present in the in-scope schema types [err:XPST0008]. Note that substitution groups do not affect the semantics of ElementTest. ... element(*, TypeName) matches an element node regardless of its name, if derives-from(AT, TypeName) is true, where AT is the type annotation of the element node, and the nilled property of the node is false.

我有這個功能

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] Required item type of result of function local:matchType() is element(*, ProductComponent); supplied value has item type element({http://cdm.basic.upc.com}product, {http://www.w3.org/2001/XMLSchema}untyped)

我可能錯了,但該類型實際上是cdm-base:ProductComponent而不是untyped。 我不明白其中的問題是...

我使用氧氣13.0撒克遜EE 9.3.0.5

+0

看起來類型正在尋找甚至沒有,因爲錯誤提到{http://www.w3.org/2001/XMLSchema}untyped,雖然我有http://www.w3。 org/2001/XMLSchema-instance 但是那麼期待什麼屬性? declare namespace xsi =「http://www.w3.org/2001/XMLSchema-instance」; 聲明命名空間xs =「http://www.w3.org/2001/XMLSchema」; – AleIla 2012-01-06 10:12:07

回答

1

撒克遜確實是正確的在這裏,所有直接構造(「內聯」)的元素有型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 /> } 
};