0
votes

According to the 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.

I have this function

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" />


};

which while I am typing returns the error:

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)

I may mistaken, but the type is actually cdm-base:ProductComponent and not untyped. I don't get where the issue is...

I am Using Oxygen 13.0 with Saxon EE 9.3.0.5

1
Looks like the type is looking for is not even there, because the error mentions {w3.org/2001/XMLSchema}untyped, while I have the w3.org/2001/XMLSchema-instance But then what attribute is expecting?? declare namespace xsi = "w3.org/2001/XMLSchema-instance"; declare namespace xs = "w3.org/2001/XMLSchema"; - AleIla

1 Answers

1
votes

Saxon is indeed correct here, all directly constructed ("inline") elements have the type xs:untyped (or xs:anyType if the construction mode is set to preserve).

The xsi:type element is meaningless until the element has been validated against your schemas. The easiest way to do this is to wrap the element in a validate expression:

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" /> }
};

Note that in XQuery 3.0, if you do not actually need the xsi:type attribute then you can validate the element as a particular 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 /> }
};