views:

231

answers:

1

I'm trying to write a schema that will validate the following XML:

<category type="rifles">
  <item>
 <name>
   AK47
     </name>
 <description>
   The USSR's chief export back in the day, and the guerrilla's best friend. On African flags than any other symbol.
  </description>
  <cost>
    4000
  </cost>
  <image>
    ak47.jpg
  </image>
  <provenance>
    The USSR.
  </provenance>
  <quantity>
    10000
  </quantity>
</item>

(Other item elements, surplus heading tags and closing tags ommited.)

And have developed the following schema:

<element name="store">
  <complexType>
 <sequence>
    <element name="category" maxOccurs="unbounded" >
      <complexType>
  <simpleContent>
     <extension base="string">
        <attribute name="type" type="string" />
     </extension>
   </simpleContent>
   <sequence>
     <element name="item" maxOccurs="unbounded" >
        <complexType>
    <sequence>
       <element name="name" type="string"/>
       <element name="description" type="string"/>
       <element name="image" type="string"/>
       <element name="cost" type="number"/>
       <element name="provenence" type="string"/>
       <element name="quantity" type="number"/>
     </sequence>
   </complexType>
        </element>
    </sequence>
  </complexType>
       </element>
     </sequence>
  </complexType>
</element>

Using the validator at http://www.xmlme.com/Validator.aspx?mid=83, I get:

Schema Error: System.Xml.Schema.XmlSchemaException: The content model of a complex type must consist of 'annotation' (if present); followed by zero or one of the following: 'simpleContent', 'complexContent', 'group', 'choice', 'sequence', or 'all'; followed by zero or more 'attribute' or 'attributeGroup'; followed by zero or one 'anyAttribute'.

I know this might sound silly, but can anyone point out my error?

+1  A: 

Try

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio - Developer Pro Edition 7.1.0.1135 (http://www.liquid-technologies.com)--&gt;
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  <xs:element name="category">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="item">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string" />
              <xs:element name="description" type="xs:string" />
              <xs:element name="cost" type="xs:unsignedShort" />
              <xs:element name="image" type="xs:string" />
              <xs:element name="provenance" type="xs:string" />
              <xs:element name="quantity" type="xs:unsignedShort" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="type" type="xs:string" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>
Colin
thanks colin! i'm guessing attributes have to be declared after sequences...
Beau Martínez
Yep, the XSD rules are all a bit fiddly, better to use an editor (give Liquid XML Studio a go!)
Colin
It's a terrible design, for XML to have attributes before content and XML Schema to have attribute definitions *after* content, because if you have nested elements in the content, the attribute definition can be a *long* way from its element.
13ren