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

梦想与她 提交于 2020-01-15 08:26:12

问题


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.


回答1:


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>



回答2:


Change the 'group' definition in XSD to include mixed="true"

<xs:element name="group">
    <xs:complexType mixed="true">


来源:https://stackoverflow.com/questions/42010528/cvc-complex-type-2-3-element-group-cannot-have-character-children-because

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!