【问题标题】:cvc-complex-type.2.3: Element 'group' cannot have character [children], because the type's content type is element-onlycvc-complex-type.2.3:元素“组”不能有字符 [children],因为该类型的内容类型是仅元素
【发布时间】:2017-06-20 00:21:41
【问题描述】:

我需要从此 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>

我收到此错误:

无效。 错误 - 第 10、9 行:org.xml.sax.SAXParseException;行号:10;列号:9; cvc-complex-type.2.3:元素“组”不能有字符 [children],因为该类型的内容类型是仅元素。

【问题讨论】:

  • 您在命名空间声明之前关闭了&lt;group&gt;。您可以在此处通过代码着色很好地看到它。
  • 好吧,我之前在 ex1.xsd 的末尾关闭了它,它说它是无效的 xml 元素类型组必须后跟 > 或 />
  • 开启group将在第二个命名空间之后关闭。相信我,就像现在一样,如果没有任何架构验证它甚至不是有效的 Xml。
  • 什么意思?
  • 你的 Xml 中有一个悬空的&gt;...="ex1.xsd"&gt;

标签: xml xsd xsd-validation xml-validation


【解决方案1】:

问题数量:

  • 正如菲尔伯特所说,你过早地关闭了开口group 标签。这是您立即出错的直接原因。它导致 解析器将您打算将属性解释为文本的内容 group 元素的内容。
  • schemaLocation 必须采用命名空间-XSD pairs
  • 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>

【讨论】:

    【解决方案2】:

    将 XSD 中的“组”定义更改为包含混合 =“真”

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

    【讨论】:

    • 当带有随机缩进的 XML 对模式失败时,这对我有帮助。
    【解决方案3】:

    当我从 Skype 复制插入了奇怪编码的代码时,我遇到了这个错误。解决的办法是去掉粘贴的代码,手工重写所有错误的编码部分,不粘贴任何编码字符。

    【讨论】:

      猜你喜欢
      • 2021-08-29
      • 2013-05-13
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2018-03-04
      相关资源
      最近更新 更多