Search code examples
xmlxsdxsd-validationxml-validation

cvc-complex-type.2.3: Element 'group' cannot have character [children], because the type's content type is element-only


I need to create XML from this XSD:

 <?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="group">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="person" minOccurs="5" maxOccurs="20" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="name" use="required" type="xs:string"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

Here is the XML I've tried:

<?xml version="1.0" ?>
<group name="abcd">
    xmlns="www.example.org"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="ex1.xsd">
    <person>Joao</person>
    <person>Andre</person>
    <person>Filipe</person>
    <person>Joaquim</person>
    <person>Rui</person>
</group>

I am getting this error:

Not valid. Error - Line 10, 9: org.xml.sax.SAXParseException; lineNumber: 10; columnNumber: 9; cvc-complex-type.2.3: Element 'group' cannot have character [children], because the type's content type is element-only.


Solution

  • Number of issues:

    • As Filburt mentioned, you've prematurely closed the opening group tag. This is the direct cause of your immediate error. It causes the parser to misinterpret what you intend to be attributes as text content to the group element.
    • schemaLocation must take namespace-XSD pairs.
    • elementFormDefault="qualified"
    • etc.

    Altogether, the following XSD will validate the following XML successfully.

    XSD

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               targetNamespace="http://www.example.org"
               elementFormDefault="qualified">
      <xs:element name="group">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="person" minOccurs="5" maxOccurs="20" type="xs:string"/>
          </xs:sequence>
          <xs:attribute name="name" use="required" type="xs:string"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    XML

    <?xml version="1.0" ?>
    <group name="abcd"
           xmlns="http://www.example.org"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.example.org ex1.xsd">
      <person>Joao</person>
      <person>Andre</person>
      <person>Filipe</person>
      <person>Joaquim</person>
      <person>Rui</person>
    </group>