Element cannot have character [children], because the type's content type is element-only
Asked Answered
S

5

15

I tried validating my XML file with a XSD file but I get the following error message:

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

Which (if i am understanding it correctly) means a complex type cannot have a combination of simple and complex typed elements as children.

But how do I fix this? I'm still kinda new to this, so the solution could be simple?

My code is shown below:

XSD:

<?xml version="1.0" encoding="UTF-8"?>
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xs:element name="biography">
               <xs:complexType>
                     <xs:sequence>
                           <xs:element name="paragraph" maxOccurs="unbounded">
                                 <xs:complexType>
                                       <xs:sequence>
                                             <xs:element name="name">
                                                   <xs:complexType>
                                                         <xs:sequence>
                                                               <xs:element name="first_name" type="xs:string" minOccurs="0"></xs:element>
                                                               <xs:element name="last_name" type="xs:string"></xs:element>
                                                         </xs:sequence>
                                                   </xs:complexType>
                                             </xs:element>
                                             <xs:element name="emphasize" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
                                             <xs:element name="profession" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
                                             <xs:element name="date" minOccurs="0">
                                                   <xs:complexType>
                                                         <xs:sequence>
                                                               <xs:element name="month" type="xs:string"></xs:element>
                                                               <xs:element name="day" type="xs:int"></xs:element>
                                                               <xs:element name="year" type="xs:int"></xs:element>
                                                         </xs:sequence>
                                                   </xs:complexType>
                                             </xs:element>
                                       </xs:sequence>
                                 </xs:complexType>
                           </xs:element>
                           <xs:element name="definition" maxOccurs="unbounded">
                                 <xs:complexType>
                                       <xs:sequence>
                                             <xs:element name="term" type="xs:string"></xs:element>
                                       </xs:sequence>
                                 </xs:complexType>
                           </xs:element>
                     </xs:sequence>
               </xs:complexType>
         </xs:element>
   </xs:schema>

XML:

<biography>
 <paragraph>
  <name><first_name>Alex</first_name> <last_name>Turing</last_name></name>
  was one of the first people to truly deserve the name <emphasize>computer
  scientist</emphasize>. Although his contributions to the fields are too
  numerous to lst, his best-known are the famous <emphasize>Turing
  Test</emphasize> and <emphasize>Turing Machine</emphasize>.
 </paragraph>

 <definition>The <term>Turing Test</term> is to this day the standard test
  for determining whether a computer is truly intelligent. This test yet
  has to be passed.</definition>

 <definition>A <term>Turing Machine</term> is an abstract finite state
 automaton with infinite memory that can be proven equivalent to any other
 finite state automaton with arbitrarily large memory. Thus what is true
 for one Turing machine is true for all Turing machines no matter how
 implemented.</definition>

 <paragraph>
  <name><last_name>Turing</last_name></name> was also an accomplished
  <profession>mathematician</profession> and
  <profession>cryptographer</profession>. His assistance was crucial in
  helping the Allies decode the German Enigma cipher. He committed suicide
  on <date><month>June</month> <day>7</day>, <year>1954</year></date>
  after being convicted of homosexuality and forced to take female hormone
  injections.
  </paragraph>
</biography>
Spile answered 28/12, 2015 at 12:7 Comment(1)
Sorry, this is totally unrelated to the essence of the question, but I can't help noticing: it's Alan Turing (actually Alan Mathison Turing), not Alex.Melyndamem
E
12

This is precisely the purpose of mixed content:

    <xs:element name="paragraph" maxOccurs="unbounded">
      <xs:complexType mixed="true">

And:

    <xs:element name="definition" maxOccurs="unbounded">
      <xs:complexType mixed="true">

Note that you also probably want paragraph and definition to be in a xs:choice macOccurs="unbounded" given your XML.

Here's your XSD updated with all changes needed so that your XML will be valid:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="biography">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="paragraph">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="name">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="first_name" type="xs:string" minOccurs="0"></xs:element>
                    <xs:element name="last_name" type="xs:string"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="emphasize" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
              <xs:element name="profession" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
              <xs:element name="date" minOccurs="0">
                <xs:complexType mixed="true">
                  <xs:sequence>
                    <xs:element name="month" type="xs:string"></xs:element>
                    <xs:element name="day" type="xs:int"></xs:element>
                    <xs:element name="year" type="xs:int"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="definition">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="term" type="xs:string"></xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
Ezequiel answered 28/12, 2015 at 14:6 Comment(2)
Thanks a lot for your help! Using a choice type to enable a combination of "definition" and "paragraph" seems pretty obvious now ;) If i understand correctly mixed="true" lets you use a combination of simple and complex types? Does it have additional uses? In any case, thanks for your help. Much appreciated :)Spile
Right, mixed="true" lets you mix markup and text in the associated content model.Ezequiel
P
16

For me This error happen because of the strange character in the xml When I compared two files with similar data in "BeyondCompare" tool I found unknown character which is not visible. Open faulty file in "Visual Studio Code" it showed me some special characters.

When I copy pasted the string in online tools to find special characters, I found that is a special character  (&#65279).

Because of this character, I got above error. It took two weeks to find solution.

Phelgon answered 30/10, 2017 at 0:3 Comment(1)
Opening the file in XML editor helped me as well. Thanks for the adviceBellanca
E
12

This is precisely the purpose of mixed content:

    <xs:element name="paragraph" maxOccurs="unbounded">
      <xs:complexType mixed="true">

And:

    <xs:element name="definition" maxOccurs="unbounded">
      <xs:complexType mixed="true">

Note that you also probably want paragraph and definition to be in a xs:choice macOccurs="unbounded" given your XML.

Here's your XSD updated with all changes needed so that your XML will be valid:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="biography">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="paragraph">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="name">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="first_name" type="xs:string" minOccurs="0"></xs:element>
                    <xs:element name="last_name" type="xs:string"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="emphasize" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
              <xs:element name="profession" maxOccurs="unbounded" minOccurs="0" type="xs:string"></xs:element>
              <xs:element name="date" minOccurs="0">
                <xs:complexType mixed="true">
                  <xs:sequence>
                    <xs:element name="month" type="xs:string"></xs:element>
                    <xs:element name="day" type="xs:int"></xs:element>
                    <xs:element name="year" type="xs:int"></xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="definition">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element name="term" type="xs:string"></xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
Ezequiel answered 28/12, 2015 at 14:6 Comment(2)
Thanks a lot for your help! Using a choice type to enable a combination of "definition" and "paragraph" seems pretty obvious now ;) If i understand correctly mixed="true" lets you use a combination of simple and complex types? Does it have additional uses? In any case, thanks for your help. Much appreciated :)Spile
Right, mixed="true" lets you mix markup and text in the associated content model.Ezequiel
E
0

I have that error too but, in my case, the xml file didn't have mixed content (i think, I started learning about xsd in school like a month ago) like in your case. However, i tried fixing my issue in the same way, declaring the first element as mixed content and it worked!

I could be wrong but, all there was in my xml file apart from sequences of elements were comments that divided each of those elements, so I assume that comments also count as mixed content? idk TWT"

*here's a pic of the error and my code: https://i.sstatic.net/Ro7VD.png

Excitability answered 5/11, 2022 at 18:58 Comment(0)
S
0

I struggled with this error too. The weird characters were the problem, couldn't see them in the XML doc or any other test reader, all I did was removed the choice, and added a new one, then transferred everything into the new one, and it worked!

Here's some screenshots: enter image description here

Smalt answered 6/3, 2023 at 12:8 Comment(0)
K
-1

My problem was because my XML-file contained a double quote symbol between tags

<tag>content<tag>"
<anotherTag/>

However both IntelliJ and AltovaXML said that the XML was correct.

Konrad answered 7/7, 2023 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.