1
votes

I'm writing a xml schema against to a xml file. I got this error for the following code but cannot figure out why. any suggestion?

cvc-type.3.1.1: Element 'employees' is a simple type, so it cannot have attributes, excepting those whose namespace name is identical to 'http://www.w3.org/2001/XMLSchema-instance' and whose [local name] is one of 'type', 'nil', 'schemaLocation' or 'noNamespaceSchemaLocation'. However, the attribute, 'essns' was found. A problem was found starting at: simpleType.

<xs:element name="employees" >
      <xs:simpleType>
        <xs:list itemType ="xs:integer"/>
      </xs:simpleType>
      </xs:element>
      <xs:element name= "projectsControlled">
      <xs:simpleType>
        <xs:list itemType ="xs:integer" />
      </xs:simpleType>
      </xs:element>

This following is xml code

<employees essns="888665555"/>
<projectsControlled pnos="20"/>
1
The error message looks pretty explicit. Type employees cannot have an essns attribute because the schema doesn't allow it to.Jim Garrison

1 Answers

2
votes

Elements may have simple types or complex types.

An element with a simple type is nothing much more than a wrapper around a valid of the appropriate type. It's not allowed to have other content, it's not allowed to have child elements, and it's not allowed to have attributes.

That is, only elements governed by complex type are allowed to have attributes. (An exception is made, as described in your error message, for xsi:nil, xsi:type, xsi:schemaLocation, and xsi:noNamespaceSchemaLocation.)

Your 'employees' element is declared as having a simple type: a list of integers. It is thus allowed to contain a list of integers, but you haven't declared any attributes for it. If you want to do that, you can declare it as having a "complex type with simple content" -- essentially, a complex type which extends a simple type by adding attributes.

<xs:simpleType name="list-of-integers">
  <xs:list itemType="xs:integer"/>
</xs:simpleType>

<xs:element name="employees">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="tns:list-of-integers">
        <xs:attribute name="essns" 
                      type="tns:list-of-integers"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

Or it's possible that what you intended to do was declare employees as an empty element with an attribute named essns whose value can be a list of integers. In that case, you don't want a complex type with simple content, because what you want is not simple content but no content.

<xs:element name="employees-sib">
  <xs:complexType mixed="false">
    <xs:sequence/>
    <xs:attribute name="essns" 
                  type="tns:list-of-integers"/>
  </xs:complexType>
</xs:element>