【问题标题】:XML schema check errorXML 模式检查错误
【发布时间】:2009-04-16 15:02:14
【问题描述】:

我正在使用以下架构来检查以下 XML 文件。而且我发现当 People 元素中有多个 Information 元素时,模式检查将失败。为什么以及如何解决它(我想让 People 元素能够嵌套多个信息项)?

XML 架构文件:

  <xs:element name="People">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Information">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Name" type="xs:string"/>
            </xs:sequence>
            <xs:attribute name="Id" type="xs:string"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

XML 文件(模式检查将失败):

  <People>
    <Information Id="1">
      <Name>John</Name>
    </Information>
    <Information Id="2">
      <Name>Mike</Name>
    </Information>
  </People>

XML 文件(模式检查将成功):

  <People>
    <Information Id="1">
      <Name>John</Name>
    </Information>
  </People>

提前致谢, 乔治

【问题讨论】:

    标签: xml schema


    【解决方案1】:

    如果序列中没有指定 minOccurs 和 maxOccurs,则默认值为 1。

    <xs:element name="Information" minOccurs = "1" maxOccurs = "unbounded">
    

    【讨论】:

    • 感谢 Mork0075,您的修复工作有效。我想确认一下,默认情况下,如果不指定 min/max 发生,则允许元素存在一次且只存在一次?
    • 是的,min 和 maxoccurs 的默认值都是 1。这在 prosa 中“只存在一次且仅存在一次”。
    【解决方案2】:
    <xs:element name="People">
        <xs:complexType>
          <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="unbounded" name="Information">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="Name" type="xs:string"/>
                </xs:sequence>
                <xs:attribute name="Id" type="xs:string"/>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    

    试试这个,肯定有用

    【讨论】:

    • 感谢 Prashant,您的修复工作有效。我想确认一下,默认情况下,如果不指定 min/max 发生,则允许元素存在一次且只存在一次?
    猜你喜欢
    • 2018-04-18
    • 2015-12-10
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2020-03-03
    • 1970-01-01
    相关资源
    最近更新 更多