0

Given the following type:

<xsd:complexType name="Options">
    <xsd:sequence>
        <xsd:element name="option" maxOccurs="unbounded">
            <xsd:complexType>
                <xsd:all>
                    <xsd:element name="id" type="xsd:integer" />
                    <xsd:element name="label" type="xsd:string" />
                    <xsd:element name="value" type="xsd:string" minOccurs="0" />
                    <xsd:element name="dependency" type="xsd:integer" minOccurs="0" />
                </xsd:all>
            </xsd:complexType>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

How can I express that a dependency of one option must exist as an id of a different option?

I think I'm supposed to be using xsd:key and xsd:keyref, but having trouble understanding how to use them.

Svish
  • 152,914
  • 173
  • 462
  • 620
  • By `different option` you mean that you also want to enforce that you can't have self-referenced options? – Petru Gardea Oct 18 '12 at 16:35
  • @PetruGardea That would indeed be nice, but first and foremost I'm just after the reference part in this question :) – Svish Oct 18 '12 at 17:19

2 Answers2

0

XSD 1.0 doesn't support this. XSD 1.1 supports Conditional Types and assertions which might allow you to do this. For xsd 1.0 you can use Schematron to achieve this.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
0

Constraints can only be expressed in the context of an element.

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="Options">
        <xsd:sequence>
            <xsd:element name="option" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:all>
                        <xsd:element name="id" type="xsd:integer"/>
                        <xsd:element name="label" type="xsd:string"/>
                        <xsd:element name="value" type="xsd:string" minOccurs="0"/>
                        <xsd:element name="dependency" type="xsd:integer" minOccurs="0"/>
                    </xsd:all>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="Options" type="Options">
        <xsd:key name="PK">
            <xsd:selector xpath="option"/>
            <xsd:field xpath="id"/>
        </xsd:key>
        <xsd:keyref name="FK" refer="PK">
            <xsd:selector xpath="option"/>
            <xsd:field xpath="dependency"/>
        </xsd:keyref>
    </xsd:element>
</xsd:schema>

enter image description here

If you wish to have this type referenced in multiple instances, along with the constraints you've indicated, then you will have to always define a global element, and then ref that element in your specific contexts.

I would mention that it is very important to understand of your use of the "different" word in your question. If you really mean it, then the above doesn't give you that.

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • So given elements `Option` and `Options` the `key` and `keyref` has to be defined for `Options`, the parent, and not for `Option`, the child? – Svish Oct 18 '12 at 17:21
  • @Svish, yes, you have to be at the Options level. This is from the spec: `{selector} specifies a restricted XPath ([XPath]) expression relative to instances of the element being declared. This must identify a node set of subordinate elements (i.e. contained within the declared element) to which the constraint applies.` You're basically trying to restrict `option` elements, which means you have to move one level up. – Petru Gardea Oct 18 '12 at 18:00