Rizel Tane
Rizel Tane

Reputation: 99

Nested complexTypes in XSD returning error: Element content is not allowed, because the type definition is simple

I'm building an xml schema for this xml:

<?xml version="1.0" encoding="UTF-8"?>

<groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="basic.xsd">
<!--Comienza la descripción de los grupos sencillos-->
    <!--modificador-->
    <group tagName="A">
      <name>modificador</name>
      <type>simple</type>
      <words>
        <word>el<word>
        <word>este<word>
      </words>
    </group>
    <!--prefijos-->
    <group tagName="C">
      <name>prefijos</name>
      <type>simple</type>
      <words>
        <word>pre</word>
        <word>ante</word>
        <word>anti</word>
        <word>pro</word>
        <word>tri</word>
      </words>
    </group>
    <!--Composiciones de intérvalos-->
</groups>

And this is my xsd file:

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

<!-- definition of simple elements -->
<xs:element name="name" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
<xs:element name="word" type="xs:string"/> 

<!-- definition of attributes -->
<xs:attribute name="tagName" type="xs:ID"/>

<!-- definition of complex elements -->
<xs:element name="words">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="word" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="group">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="name"/>
      <xs:element ref="type"/>
      <xs:element ref="words" />
    </xs:sequence>
    <xs:attribute ref="tagName" use="required"/>
  </xs:complexType>
</xs:element>

<xs:element name="groups">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="group" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

I'm using nokogiri to validate my xml, but I'm getting the following error:

Element 'word': Element content is not allowed, because the type definition is simple. XML not schema valid

Why should word be complex then? And how do I do that because to me it's just fine. Thanks in advanced.

Upvotes: 2

Views: 4519

Answers (1)

Burkart
Burkart

Reputation: 464

You accidentally used opening tags instead of closing tags at

<word>el<word>
<word>este<word>

in your input XML file. Since there are no closing tags, your XML is not well-formed. It's a good idea to always check well-formedness first.

You get the message "element content is not allowed" because it looks like there are word elements within word elements. This is only possible with complex types.

Change the affected lines in the input file to

<word>el</word>
<word>este</word>

and validation against your schema will succeed.

Upvotes: 1

Related Questions