【问题标题】:error validating xml file against xsd针对 xsd 验证 xml 文件时出错
【发布时间】:2014-03-08 11:59:48
【问题描述】:

我正在尝试根据 xsd 文件验证 xml 文件 但我得到一个错误,我不知道为什么 有人有想法吗?

我的 xsd 文件:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
      <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                  <xs:element ref="call"/>
            </xs:choice>
      </xs:complexType>
  </xs:element>

  <xs:element name="cl">
      <xs:complexType>
           <xs:choice minOccurs="1" maxOccurs="1">
                   <xs:element ref="arguments"/>
                   <xs:element ref="outputs"/>
           </xs:choice>
           <xs:attribute name="name" type="xs:string" use="required"/>
           <xs:attribute name="module" type="xs:string" use="required"/>
           <xs:attribute name="title" type="xs:string" use="optional"/>
      </xs:complexType> 
  </xs:element>

  <xs:element name="">
       <xs:complexType>
          <xs:choice minOccurs="0" maxOccurs="unbounded">
                 <xs:element ref=""/>
          </xs:choice>
      </xs:complexType> 
  </xs:element>

  <xs:element name="">
   <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
          xs:element ref=""/>
    </xs:choice>
   </xs:complexType>    
  </xs:element>


  <xs:element name="">
    <xs:complexType>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="value" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="">
    <xs:complexType>
        <xs:attribute name="name" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>

</xs:schema> 

我收到“无效文件”,但我看不出问题出在哪里,谁能帮帮我? 谢谢你

【问题讨论】:

  • 我相信 Python 代码不是回答您的问题所必需的(因此您实际上可以删除它,以及 python 标签。)验证错误是:cvc-complex-type.2.4.d: Invalid content was found starting with element 'outputs'. No child element is expected at this point. 您可以更改您的打印解析器错误的代码,而不是 invalid file,它不会提供有关错误的信息。
  • 如何更改我的代码以打印我不明白的解析器错误你能解释更多吗? @helderdarocha
  • 我使用的 PHP 不多,但我相信您必须以某种方式访问​​ LibXMLError 才能打印消息。我找到了this article,如果您有兴趣,它可能会有所帮助(它有一个关于解析器错误报告的部分)
  • 您的架构文档似乎无效(除了由 holderdarocha 确定的问题之外):您有四个名称为空的元素声明。

标签: xml xsd


【解决方案1】:

您的实例无效,因为根据您的架构,您的call 元素中只能有argumentsoptions 之一的choice

<xs:choice minOccurs="1" maxOccurs="1">
     <xs:element ref="arguments"/>
     <xs:element ref="outputs"/>
</xs:choice>

所以,这是有效的:

<call name="name" module="module" title="title">
    <outputs>
        <output name="name"/>
    </outputs>
</call>

这是有效的:

<call name="name" module="module" title="title">
    <arguments>
        <argument name="NAME" value="value"/>
    </arguments>
</call>

但这不是,因为有一个元素的最大值和最小值的限制:

<call name="name" module="module" title="title">
    <arguments>
        <argument name="NAME" value="value"/>
    </arguments>
    <outputs>
        <output name="name"/>
    </outputs>
</call>

您可能希望将 choice 替换为 sequence 或增加 maxOccurs/minOccurs 值(取决于您想要的结构)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多