I wonder if it's possible to use keyref element with optional fields.
e.g. Given the following schema:
<element name="myElement" type="myType">
    <complexType>
        <sequence>
            <element name="subElem">
                <complexType>
                    <attribute name="name" type="string"/>
                    <attribute name="type" type="string"/>
                </complexType>
            </element>
            <element name="subElem2">
                <complexType>
                    <sequence>
                        <element name="subSubElem" minOccurs="0">
                            <complexType>
                                <attribute name="type" type="string"/>
                            </complexType>
                        </element>
                    </sequence>
                    <attribute name="name" type="string" use="required"/>
                </complexType>
            </element>
        </sequence>
    </complexType>
    <key name="uniqueSubElem">
        <selector xpath="subElem"/>
        <field xpath="@name"/>
        <field xpath="@type"/>
    </key>
    <keyref name="uniqueSubElemRef" refer="uniqueSubElem">
        <selector xpath="subElem2"/>
        <field xpath="@name"/>
        <field xpath="subSubElem/@type"/>
    </keyref>
</element>
In this example if I don't specify a subSubElem in the XML the validator will complain:
Not enough values specified for <keyref name="uniqueSubElem"> identity constraint specified for element "myElement".
I need both the minOccurs="0" and the keyref constraint, and if the field is not present simply don't want any check to be performed (as for NULL FOREIGN KEYs).
EDIT: Ignore namespaces for this example, I used default namespace for everything...