【问题标题】:XSD Error: Character content is not allowed, because the content type is emptyXSD 错误:不允许字符内容,因为内容类型为空
【发布时间】:2011-10-13 23:28:38
【问题描述】:

我收到来自以下 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


    【解决方案1】:

    意思是“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>
    

    【讨论】:

    • 我需要向 XSD 添加什么以在 XML 中添加字符串(在 Person 元素下)?
    • 编辑了我的帖子。请检查。
    【解决方案2】:

    如果 XML 是正确的并且您希望 XSD 支持字符串内容(有或没有其他子元素),您可以简单地在 complexType 上添加 mixed=true 属性:

    <?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 mixed="true">
                        <xsd:attribute name="name" type="xsd:string" use="required" />
                     </xsd:complexType>
                 </xsd:element>
             </xsd:sequence>
          </xsd:complexType>
       </xsd:element>
    </xsd:schema>
    

    mixed 属性:

    指定是否允许字符数据出现在此 complexType 元素的子元素之间。默认为假。如果 simpleContent 元素是子元素,则不允许使用混合属性!

    【讨论】:

      【解决方案3】:

      对我来说,当我将 value 元素添加到之前只有属性的复杂类型时,错误就解决了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-28
        • 2021-04-19
        • 2015-12-15
        • 1970-01-01
        • 2018-10-24
        • 2019-04-23
        • 1970-01-01
        • 2016-09-28
        相关资源
        最近更新 更多