xml - XSD 错误 : Character content is not allowed, 因为内容类型为空

标签 xml xsd

我从以下 XSD 收到验证错误:

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="People">
      <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="Person" maxOccurs="unbounded">
                 <xsd:complexType>
                    <xsd:attribute name="name" type="xsd:string" use="required" />
                 </xsd:complexType>
             </xsd:element>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>

使用以下 XML 进行验证时:

<?xml version="1.0" ?>
<People>
    <Person name='john'>
        a nice person
    </Person>
    <Person name='sarah'>
        a very nice person
    </Person>
    <Person name='chris'>
        the nicest person in the world
   </Person>
</People>

返回以下错误:

lxml.etree.XMLSyntaxError: Element 'Person': Character content is not allowed, because the content type is empty.

我错过了什么?

最佳答案

意思是“Person”不能包含字符串。对于要使用该 xsd 验证的 xml,请使用:

<?xml version="1.0" ?>
<People>
    <Person name='john'>
    </Person>
    <Person name='sarah'>
    </Person>
    <Person name='chris'>
   </Person>
</People>

尝试使用 xsd 进行验证:

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="People">
      <xsd:complexType>
         <xsd:sequence>
         <xsd:element name="Person" type="Person" maxOccurs="unbounded"/>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>

   <xsd:complexType name="Person">
     <xsd:simpleContent>
        <xsd:extension base="xsd:string">
           <xsd:attribute name="name" type="xsd:string" use="required" />
        </xsd:extension>
    </xsd:simpleContent>
   </xsd:complexType>
</xsd:schema>

关于xml - XSD 错误 : Character content is not allowed, 因为内容类型为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7761589/

相关文章:

c# - .NET C# Dataset WriteXml 包含/排除列并添加命名空间

xsd - 在 xsd 模式中定义元素列表

xml - cvc-复杂类型.2.4.b : The content of element 'tns:patient' is not complete

xml - XSD:一个元素应允许采用不同的形式

sql-server - 查询多个 XML 值

xml - 获取根节点属性

xml - XPath 拉出不止一场比赛

xml - 使用 XSLT 删除第一个 <p> 标签

java - 如何在 XSD 中指定属性 namespace 以便 JAXB 正确解释它?

java - 使用 maven-jaxb2-plugin 获取 boolean 变量的 Getter