XSD错误:由于内容类型为空,因此不允许字符内容

问题描述 投票:9回答:2

我从以下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.

我想念什么?

xml xsd
2个回答
15
投票

是说“人”不能包含字符串。对于使用该xsd验证的xml,请使用::>

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

0
投票

如果XML是正确的,并且您希望XSD支持字符串内容(带有或不带有其他子元素,只需在complexType上添加mixed=true属性:

© www.soinside.com 2019 - 2024. All rights reserved.