cvc-complex-type.2.3: 元素 'group' 不能有字符 [children],因为类型的内容类型是仅元素

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

我需要从这个 XSD 创建 XML:

 <?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>

这是我试过的XML:

<?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>

我收到这个错误:

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.

问题数量:

  • 正如 Filburt 提到的,您过早地关闭了开口 group 标签。这是您立即出错的直接原因。它导致 解析器将您打算成为属性的内容误解为文本 group 元素的内容。
  • schemaLocation必须取namespace-XSD.
  • elementFormDefault="qualified"
  • 等等

总而言之,下面的XSD将成功验证下面的XML。

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>

更改 XSD 中的 'group' 定义以包含 mixed="true"

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

当我从插入了奇怪编码的 Skype 复制代码时出现此错误。解决方案是去掉粘贴的代码,手工重写所有错误的编码部分,没有任何粘贴的编码字符。