XSD validation for unsorted and only one unbounded element

I have an xml file like this:

<customer>
  <field1 />
  <field2 />
  <field3>
    <item1 />
  </field3>
  <field3>
    <item1 />
  </field3>
</customer>

      

Field

* can appear in any order, and only field3 can appear more than once.

How do I create an XSD file to test this?

Thank!

0


source to share


2 answers


Try

I'm not a guru, but it works.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="customer" type="customerType"/>
  <xs:complexType name="customerType">
    <xs:sequence>
      <xs:element name="field1" minOccurs="1" maxOccurs="1">
      </xs:element>
      <xs:element name="field2" minOccurs="1" maxOccurs="1">
      </xs:element>
      <xs:element name="field3" type="field3Type"
                  minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="field3Type">
    <xs:sequence>
      <xs:element name="item1">
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

      

instruments



I've used XML Copy Editor , but there are many editors out there that will validate XML.

links

You may also be interested in this article on creating XSD from XML file.

+3


source


Hum, this is the kind of work that xsd is really not comfortable with. Anyway, this should do the trick, if I'm not mistaken:



<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/NewXMLSchema"
xmlns:tns="http://www.example.org/NewXMLSchema" elementFormDefault="qualified">

  <element name="customer" type="tns:customerType"/>
  <complexType name="customerType">
    <sequence>
      <element>
        <complexType>
          <all>
            <element>
              <complexType>
                <sequence>
                  <element ref="tns:field3" maxOccurs="unbounded"/>
                  <element ref="tns:field1" maxOccurs="1"/>
                </sequence>
              </complexType>
            </element>
            <element>
              <complexType>
                <sequence>
                  <element ref="tns:field3" maxOccurs="unbounded"/>
                  <element ref="tns:field2" maxOccurs="1"/>
                </sequence>
              </complexType>
            </element>
            <element>
              <complexType>
                <sequence>
                  <element ref="tns:field3" maxOccurs="unbounded"/>
                  <element ref="tns:field4" maxOccurs="1"/>
                </sequence>
              </complexType>
            </element>
          </all>
        </complexType>
      </element>
      <element ref="tns:field3" maxOccurs="unbounded" />
    </sequence>
  </complexType>
  <complexType name="field1Container"/>
  <complexType name="field2Container"/>
  <complexType name="field3Type">
    <sequence>
      <element name="item1"/>
    </sequence>
  </complexType>
  <complexType name="field4Container"/>
  <element name="field3" type="tns:field3Type"/>
  <element name="field1"/>
  <element name="field2"/>
  <element name="field4"/>
</schema>

      

0


source







All Articles