1

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...

Xstian
  • 8,184
  • 10
  • 42
  • 72
gentooise
  • 416
  • 3
  • 8

1 Answers1

0

I hope I understood your problem correctly. In your schema, you need to enclose <sequence> in <complexType>, and you need to close the field pointing to "subSubElem/@type":

<xs:element name="myElement">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="subElem">
                <xs:complexType>
                    <xs:attribute name="name" type="xs:string"/>
                    <xs:attribute name="type" type="xs:string"/>
                </xs:complexType>
            </xs:element>
            <xs:element name="subElem2">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="subSubElem">
                            <xs:complexType>
                                <xs:attribute name="type" type="xs:string"/>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string" use="required"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:key name="uniqueSubElem">
        <xs:selector xpath="subElem"/>
        <xs:field xpath="@name"/>
        <xs:field xpath="@type"/>
    </xs:key>
    <xs:keyref name="uniqueSubElemRef" refer="uniqueSubElem">
        <xs:selector xpath="subElem2"/>
        <xs:field xpath="@name"/>
        <xs:field xpath="subSubElem/@type"/>
    </xs:keyref>
</xs:element>

This XML should validate without specifying a subSubElem:

<myElement>
    <subElem name="foo" type="bar"/>
    <subElem2 name="foo2"/>
</myElement>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
friedemann_bach
  • 1,418
  • 14
  • 29
  • I have fixed the schema. Anyway, this validates because the attribute 'name' is also missing, but I need it. I added 'use="required"' for name attribute. – gentooise Nov 21 '15 at 18:22
  • Should work anyway if you add the use="required" in the schema. Still no need to specify subSubElem. I updated the solution. – friedemann_bach Nov 21 '15 at 18:49
  • If there are still problems just give us an XML example you expect to validate. And please, next time, include all your requirements in the question, and do not update your question in a way that the answers become obsolete. – friedemann_bach Nov 21 '15 at 18:52
  • 1
    I'm sorry, seems it was a bug in the validator: https://issues.apache.org/jira/browse/XERCESJ-682. The XML I expect to validate is exactly the one you provided. – gentooise Nov 21 '15 at 19:28