0

I'm designing an XSD schema for all my XML I made after the fact. I know I should of done this first but I'm kinda new to it. So far all my XML works wonders for my app, I just want some good validation for it too.

Here is a quick example of what I'm going for.

<fields>
  <field id="name" type="text">
   <display>Somethings Name</display>
   <description>The name of the something.</description>
  </field>
</fields>
<forms>
  <form id="custom_form">
    <widgets>
      <widget type="text-input">
        <field id="name"/>
      </widget>
    </widgets>
  </form>
</forms>

Here is a simplified example of the XSD I have:

<xs:sequence>
  <xs:element name="fields" minOccurs="1" maxOccurs="1">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="field" type="fieldType"
          minOccurs="1" maxOccurs="unbounded">
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="forms" maxOccurs="1" minOccurs="1">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="form" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="widgets" minOccurs="1" maxOccurs="1">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="widget" minOccurs="1" maxOccurs="unbounded">
                      <!-- What do I put here to reference a field? -->
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:sequence>

My main question is how do I tell the XSD schema that the element within the widget node is a reference to one of the fields defined in the fields node above it? Right now I simply use code for this validation.

How should I define the field node in this scenario? Should I simply define the field node twice, one for the main definition and one for referencing a field within a widget node?

I also saw there is the option to redefine nodes in an XSD as well. Should I maybe define the main field definition then redefine it as a referenced style node for the widgets? How would I even do this?

Any suggestions for what is common or popular for this scenario?

mr haven
  • 1,494
  • 1
  • 17
  • 28

1 Answers1

1

You could make use of the XML Schema key/keyref construct to constrain the possible values of an attribute to those used in a set of attributes elsewhere in your document. See XML Schema key/keyref - how to use them? and the links in the answers for usage examples, ignoring the JAXB specifics.

imhotap
  • 2,275
  • 1
  • 8
  • 16
  • Oh dang, just what I was looking for. Strange thing is W3Schools nor eclipse XSD tools have any mention of key/keyref so I never came across it on my own. Looks like there is quite a few XSD features like that. – mr haven Dec 29 '17 at 21:57