Xml &引用;不应使用此元素;错误:xs:sequence与xs:all

Xml &引用;不应使用此元素;错误:xs:sequence与xs:all,xml,xsd,xsd-validation,Xml,Xsd,Xsd Validation,我有一些XML,我正在编写一个验证工具,以便我们的客户可以测试他们的示例提要,但我对XSD非常陌生。我已经到了一个地步,根据cookie-cuter示例,所有内容都能正确验证,所以我想抛出一些其他场景。我想到的第一个方法是在基本节点中有不同顺序的元素 例如: 我使用以下complexType来验证每个事务节点 <xs:complexType name="transactionType"> <xs:sequence> <xs:element t

我有一些XML,我正在编写一个验证工具,以便我们的客户可以测试他们的示例提要,但我对XSD非常陌生。我已经到了一个地步,根据cookie-cuter示例,所有内容都能正确验证,所以我想抛出一些其他场景。我想到的第一个方法是在基本节点中有不同顺序的元素

例如: 我使用以下complexType来验证每个事务节点

<xs:complexType name="transactionType">
    <xs:sequence>
        <xs:element type="xs:string" name="id"/>
        <xs:element type="xs:string" name="type"/>
        <xs:element type="xs:float" name="amount"/>
        <xs:element type="xs:string" name="description"/>
        <xs:element type="xs:string" name="status"/>
        <xs:choice>
            <xs:element name="transacted_on" type="xs:date"/>
            <xs:element name="transacted_at" type="xs:date"/>
        </xs:choice>
        <xs:choice>
            <xs:element name="posted_on" type="xs:date"/>
            <xs:element name="posted_at" type="xs:date"/>
        </xs:choice>
    </xs:sequence>
</xs:complexType>
在进行此更改后运行验证时,仍会出现错误:

不需要此元素。预期为(id)


这里缺少什么?

XML模式不允许
xs:choice
成为
xs:all
的子项

解决办法:

  • xs:choice
    元素周围添加一个包装器元素
  • 放弃
    xs:all
    ,通过
    xs:sequence
    以固定顺序生活

  • 我建议在实践中,
    xs:sequence
    可以很好地工作,避免这个问题以及其他经常出现在
    xs:all
    中的问题,包括违反。

    我实际上怀疑这是一个原因,但是,如果我删除
    xs:choice
    标记,我仍然会因为元素顺序错误而得到相同的错误。我想我找到了另一篇具有相同问题的帖子
    <transaction>
        <id>20150617-123456</id>
        <type>DEBIT</type>
        <amount>17.44</amount>
        <description>Debit Card: CAFE  06/16/15</description>
        <status>POSTED</status>
        <transacted_on>2015-06-17</transacted_on>
        <posted_on>2015-06-16</posted_on>
    </transaction>
    
    <transactions>
        <transaction>
            <amount>17.44</amount>
            <id>20150617-123456</id>
            <type>DEBIT</type>
            <description>Debit Card: CAFE  06/16/15</description>
            <status>POSTED</status>
            <transacted_on>2015-06-17</transacted_on>
            <posted_on>2015-06-16</posted_on>
        </transaction>
        <transaction>
            <id>20150617-123456</id>
            <type>CREDIT</type>
            <amount>17.44</amount>
            <description>VISA Card: payment</description>
            <status>POSTED</status>
            <transacted_on>2015-06-17</transacted_on>
            <posted_on>2015-06-16</posted_on>
        </transaction>
    </transactions>
    
    <xs:complexType name="transactionType">
        <xs:all>
            <xs:element type="xs:string" name="id"/>
            <xs:element type="xs:string" name="type"/>
            <xs:element type="xs:float" name="amount"/>
            <xs:element type="xs:string" name="description"/>
            <xs:element type="xs:string" name="status"/>
            <xs:choice>
                <xs:element name="transacted_on" type="xs:date"/>
                <xs:element name="transacted_at" type="xs:date"/>
            </xs:choice>
            <xs:choice>
                <xs:element name="posted_on" type="xs:date"/>
                <xs:element name="posted_at" type="xs:date"/>
            </xs:choice>
        </xs:all>
    </xs:complexType>