tags:

views:

353

answers:

4

I have a 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 can I create a XSD file to validate this?

Thank you!

+3  A: 

Try this

I'm not a guru, but this appears to work.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <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>

tools

I used XML Copy Editor, but there are loads of editors which will validate XML.

links

You might also be interested in this article about generating an XSD from an XML file.

AJ
A: 

AJ,

Thanks for your answer. But the problem is that field* may appear in any order - xs:all would be perfect if I could use maxOccurs="unbounded".

wmasm
A: 

Hum, this is the kind of work xsd is really not handy for. Anyway, this should do the trick if I did not make a mistake:

<?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>
gizmo
A: 

gizmo,

Thanks for your answer. Unfortunatelly, it didn't work - VS2008 gives me this error:

"If ref is present, all of complexType, simpleType, key, keyref, unique, nillable, default, fixed, form, block and type must be absent"

My problem is that I have a list of unordered fields that must have minOccurs=0/maxOccurs=1 and just one of them must have minOccurs=0/maxOccurs=unbounded. I think this validation cannot be done using XSD or DTD.

wmasm