【问题标题】:XSD to validate element containing sequence and attributesXSD 验证包含序列和属性的元素
【发布时间】:2018-10-10 13:48:51
【问题描述】:

为什么我的 XSD 不适用于以下 XML 结构? 它说“发现以元素“权利”开头的无效内容。此时不需要子元素。'

XML:

<Root>...
   <Rights Status="xxx" Date="2009-02-03T00:00:00">
      <Right>
         <Amount>9999</Amount>
         <AmountCovered>888</AmountCovered>
      </Right>
      <Right>
         <Amount>8888</Amount>
         <AmountCovered>777</AmountCovered>
      </Right>
      ...
   </Rights>
   ...

XSD:

<xs:element name="Rights" minOccurs="0">
   <xs:complexType>
      <xs:sequence>
         <xs:element name="Right" maxOccurs="unbounded">
            <xs:complexType>
               <xs:all>
                  <xs:element name="Amount" type="xs:string" minOccurs="0" />
                  <xs:element name="AmountCovered" type="xs:string" minOccurs="0" />
               </xs:all>
            </xs:complexType>
         </xs:element>
      </xs:sequence>
      <xs:attribute name="Status" use="optional" type="xs:string" />
      <xs:attribute name="Date" use="optional" type="xs:dateTime" />
    </xs:complexType>
 </xs:element>

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    问题是根元素&lt;Rights&gt;不能用minOccurs="0"指定
    删除 &lt;xs:element name="Rights"&gt; 中的 minOccurs 或在 &lt;Rights&gt; 之外包装另一个根元素

    XSD:
    <?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root"> <xs:complexType> <xs:sequence> <xs:element name="Rights" minOccurs="0"> ... </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

    【讨论】:

    • 您好,我刚刚编辑了我的代码, 不是根元素,所以问题一定出在其他地方
    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    相关资源
    最近更新 更多