Now, I am sure the function works properly because I've been using for a while only specifying the element name, which doesn't require any schema import. Now I have a new requirement, such elements will always have same type but in turn they may also have different names and I want to enforce this check trough the function signature.

The strange thing is I do not have any error while typing, it pops up during transformation only.

It's not a syntax error, it's a runtime error(wrong item type in return), that's why you only see it at transformation time.

The error indicates the returned item type from common:getProduct() is of a type different from the one specified.Does common:getProduct return the result of common:resolveElement()?What item type does the common:resolveElement function return?

common:resolveElement() returns an indeterminate item type depending on the result of the if statement. Either:- common:getElementByIdRefKey($request,$anyElem/text())or- $anyElem

This result is then returned by common:getProduct() which must have the fixed item type: element(*,cdm-base:ProductComponent).My guess is that common:resolveElement() does not return an item of the appropriate type: element(*,cdm-base:ProductComponent).

Have you tried using the XQuery debugger(with Saxon-EE) to follow(XWatch) the results/types of these functions?