HukeLau_DABA
HukeLau_DABA

Reputation: 2528

Is this a good grammar to xml schema?

If I have these rules:

PIZZA-->doughSAUCECHEESE

SAUCE-->marinara | bbq | pesto

CHEESE-->mozarella | 

A pizza string is comprised of the literal 'dough' followed by 1 of 3 sauces. It may end there(no cheese) or end with the literal 'mozarella'. Here is my xml schema:

<xs:element name="pizza">
  <xs:complexType>
    <xs:element name="doughLiteral" type="xs:string" fixed="dough"/>
    <xs:element name="sauce">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="marinara"/>
          <xs:enumeration value="bbq"/>
          <xs:enumeration value="pesto"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="cheese">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="mozarella"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value=""/>
        </xs:restriction> 
      </xs:simpleType>
    </xs:element>
  </xs: complexType>
</xs: element>

I'm not sure if I specified the empty string correctly. Also, I read that attributes are by default optional, elements are by default required though right, i.e. the schema above states there MUST be 1 sauce right? Thank you.

Upvotes: 0

Views: 97

Answers (1)

MiMo
MiMo

Reputation: 11973

Elements by default are compulsory, to make them optional use minOccurs="0".

You are missing a xs:sequence and the double xs:simpleType used in the cheese element definition is not correct.

The correct schema should be something like this:

<xs:element name="pizza">
  <xs:complexType>
    <xs:sequence>
    <xs:element name="doughLiteral" type="xs:string" fixed="dough"/>
    <xs:element name="sauce">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="marinara"/>
          <xs:enumeration value="bbq"/>
          <xs:enumeration value="pesto"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="cheese" minOccurs="0">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="mozarella"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Having said that, as @seki correctly points out this schema defines an XML structure like:

<pizza>
  <doughLiteral>dough</doughLiteral>
  <sauce>marinara</sauce>
  <cheese>mozzarella</cheese>
</pizza>

not the plain text grammar you specify at the beginning of the question.

Upvotes: 1

Related Questions