1
votes

I have a more or less simple task to build a XSD schema of but I'm not sure if my idea here is right. Especially for the element comment.

Customers can make purchase orders. A purchase order includes at least one order position (product name, quantity, and price are obligatory; comment and shipping date are optional).

A purchase order has a date (order date) and an optional comment. Customers can specify different addresses (billing and shipping). Only the shipping address is required.

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
    <xs:element name="purchase-order">
        <xs:element name="order-position" type="order-position-type" minOccurs="1">
            <xs:complexType name="order-position-type">
                <xs:sequence>
                    <xs:element name="product-name" type="xs:string"></xs:element>
                    <xs:element name="quantity" type="xs:integer"></xs:element>
                    <xs:element name="price" type="xs:decimal"></xs:element>
                    <xs:element name="comment" type="xs:string" minOccurs="0" maxOccurs="2"></xs:element>
                    <xs:element name="shipping-date" type="xs:date" minOccurs="0"></xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="order-date" type="xs:date" minOccurs="0"></xs:element>
        <xs:element name="billing-address" type="xs:string"></xs:element>
        <xs:element name="shipping-address" type="xs:string" minOccurs="1"></xs:element>
    </xs:element>
</xs:schema>

So does the same element, here comment, appear multiple times? Now I have min and maxOccurs for comment but in the sequence so it's probably wrong.

Where else are errors you maybe see? Or can I make it even easier? The point at least one order position let me create an element before the complexType to tell that the minOccurs value is 1.

1

1 Answers

1
votes

It's ok to have minOccurs and maxOccurs on an element in a sequence.

Here are some of the issues that you do have to address, however:

  1. xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" should be xmlns:xs="http://www.w3.org/2001/XMLSchema". (Fixes title message error, but there will be more to fix after that...)

  2. <xs:element name="purchase-order"> cannot have an xs:element child. Use a xs:complexType child and xs:sequence grandchild.

  3. Declaration of the order-position element cannot have both a type attribute and a xs:complexType child. Use one or the other.

Others remain (meeting your cardinality requirements, for example), but here is an XSD that has the above syntax issues fixed, at least, to help get you unstuck:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="purchase-order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="order-position" type="order-position-type" minOccurs="1"/>
        <xs:element name="order-date" type="xs:date" minOccurs="0"/>
        <xs:element name="billing-address" type="xs:string"/>
        <xs:element name="shipping-address" type="xs:string" minOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="order-position-type">
    <xs:sequence>
      <xs:element name="product-name" type="xs:string"/>
      <xs:element name="quantity" type="xs:integer"/>
      <xs:element name="price" type="xs:decimal"/>
      <xs:element name="comment" type="xs:string" minOccurs="0" maxOccurs="2"/>
      <xs:element name="shipping-date" type="xs:date" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

You'll want to use an XML editor or validating parser to check that your XSD is well-formed and valid.