Paul π
Paul π

Reputation: 519

xml schema, multiple elements with same name but with different types error

There are a ton of examples of this dilemma online but I still can't quite understand the best way to go about this. I'm trying to create a schema that will:


NOTE: The XML structure I'm working with uses 'True' rather than 'true' (even though a System.Boolean will accept mixed-case variations of "TruE") so I have to restrict using base="xs:string" and an enumeration rather than relying on xs:restriction base="xs:boolean".



I'm getting this error message when validating the xml below against the schema I have so far:

cos-element-consistent: Error for type '#AnonType_Application.UserOptionsconfig'. Multiple elements with name 'option', with different types, appear in the model group.



XML:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <Application.UserOptions>
        <option name="StartMaximized" type="System.Boolean">
            <val>False</val>
        </option>
        <option name="WindowSize" type="System.Drawing.Size">
            <val>1366, 768</val>
        </option>
    </Application.UserOptions>
</config>

XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="config">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Application.UserOptions">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="option">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="val">
                      <xs:simpleType>
                        <xs:restriction base="xs:string">
                          <xs:enumeration value="True"/>
                          <xs:enumeration value="False"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="name" type="xs:string" use="required" fixed="StartMaximized"/>
                  <xs:attribute name="type" type="xs:string" use="required" fixed="System.Boolean"/>
                </xs:complexType>
              </xs:element>
              <xs:element name="option">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="val">
                      <xs:simpleType>
                        <xs:restriction base="xs:string">
                          <xs:pattern value="\d+,\s\d+"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute name="name" type="xs:string" use="required" fixed="WindowSize"/>
                  <xs:attribute name="type" type="xs:string" use="required" fixed="System.Drawing.Size"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>





I'm prepared for receiving a stiff tone from the community as the topic has been seemingly covered, but after looking at responses to questions similar to mine, I still cannot determine how to incorporate the restrictions I've laid out.

Upvotes: 0

Views: 108

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Sample XSD 1.1 using xs:alternative based on type attribute of option:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="config">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Application.UserOptions">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="option" maxOccurs="unbounded">
                <xs:alternative test="@type = 'System.Boolean'">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="val">
                        <xs:simpleType>
                          <xs:restriction base="xs:string">
                            <xs:enumeration value="True"/>
                            <xs:enumeration value="False"/>
                          </xs:restriction>
                        </xs:simpleType>
                      </xs:element>
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string" use="required" fixed="StartMaximized"/>
                    <xs:attribute name="type" type="xs:string" use="required" fixed="System.Boolean"/>
                  </xs:complexType>                  
                </xs:alternative>
                <xs:alternative test="@type = 'System.Drawing.Size'">
                  <xs:complexType>
                    <xs:sequence>
                      <xs:element name="val">
                        <xs:simpleType>
                          <xs:restriction base="xs:string">
                            <xs:pattern value="\d+,\s\d+"/>
                          </xs:restriction>
                        </xs:simpleType>
                      </xs:element>
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string" use="required" fixed="WindowSize"/>
                    <xs:attribute name="type" type="xs:string" use="required" fixed="System.Drawing.Size"/>
                  </xs:complexType>
                </xs:alternative>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Upvotes: 0

Related Questions