Manisha Singh Sanoo
Manisha Singh Sanoo

Reputation: 929

Getting errors for XSD attribute

Hello guys i'm learning how to validate XML files but i'm getting errors for the attribute part. So this is my XML file:

<?xml version="1.0" encoding="ISO88591"?>

<feedback xs:noNamespaceSchemaLocation = "Num1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">

<studentemail>[email protected]</studentemail>

<modulename code="CSE2041">Web Tech II</modulename>

<moduleyear>2010</moduleyear>

<classsize>Adequate</classsize>

</feedback>

And this is the XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="feedback">
<xs:complexType>
<xs:sequence>

<xs:element name="studentemail" type="xs:string"/>

<xs:element name="modulename" type="xs:string">
<xs:complexType>
<xs:sequence>

<xs:element name="moduleyear" type="xs:integer"/>
<xs:element name="classsize" type="xs:string"/>

</xs:sequence>
<xs:attribute name="code" type="xs:string"/> //errors
</xs:complexType>
</xs:element>

</xs:sequence>
</xs:complexType>
</xs:element>

</xs:schema>

The error messages are:

Errors in the XML document:
4: 52 cvc-complex-type.2.3: Element 'modulename' cannot have character [children], because the type's content type is element-only.
4: 52 cvc-complex-type.2.4.b: The content of element 'modulename' is not complete. One of '{"":moduleyear}' is expected.
5: 13 cvc-complex-type.2.4.d: Invalid content was found starting with element 'moduleyear'. No child element is expected at this point.

Errors in file xml-schema:
10: 48 src-element.3: Element 'modulename' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.

Please help me out. Thanks.

Upvotes: 0

Views: 1460

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122364

If you want an element to have character content and attributes but not child elements, then you need to give it a complexType with simpleContent that extends the simple type of the character content:

<xs:element name="modulename">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute ....

If you wanted to apply facets to the string value (e.g. to disallow an empty string value) then you have to do it in two stages, first declare a named simpleType with the facets

<xs:simpleType name="nonEmptyString">
  <xs:restriction base="xs:string">
    <xs:minLength value="1" />
  </xs:restriction>
</xs:simpleType>

and then give the element a complexType that extends the simple one

<xs:element name="modulename">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="nonEmptyString">
        <!-- attribute declarations go here -->

Upvotes: 0

kjhughes
kjhughes

Reputation: 111491

You will be able to successfully validate your XML if you make one change to it:

  1. Change encoding="ISO88591" to encoding="ISO-8859-1".

And two changes to your XSD:

  1. Move moduleyear and classsize to siblings rather than children of modulename.
  2. Fixup the content model of modulename to use the string-content-with-attributes pattern as shown below:

XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="feedback">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="studentemail" type="xs:string"/>
        <xs:element name="modulename">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="code" type="xs:string"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="moduleyear" type="xs:integer"/>
        <xs:element name="classsize" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Upvotes: 1

Related Questions