Skip to main content
 首页 » 编程设计

c#之如果 XSD 未指定 maxOccurs,则 XML 文档中的最大元素数是多少

2024年02月23日13zhwl

我有一个在文档中定义多个元素的 XSD 架构。我希望成为集合的两组元素。一组元素定义如下:

<xsd:element name="Prospects" minOccurs="0"> 
<xsd:complexType> 
  <xsd:sequence> 
    <xsd:element name="ROW" minOccurs="0" maxOccurs="unbounded"> 
      <xsd:complexType> 
        <xsd:sequence> 
          <xsd:element name="ID" type="xdv:guidKey" nillable="false" /> 
          <xsd:element name="Name" minOccurs="0"> 
            <xsd:complexType> 
              <xsd:simpleContent> 
                <xsd:extension base="xdv:stringLen50"> 
                  <xsd:attribute name="origVal" type="xdv:stringLen50" use="optional" /> 
                </xsd:extension> 
              </xsd:simpleContent> 
            </xsd:complexType> 
          </xsd:element> 
          ... more stuff... 
    </xsd:element> 
  </xsd:sequence> 
  <xsd:attribute name="name" type="xsd:string" use="required" fixed="Prospects" /> 
  <xsd:attribute name="alias" type="xsd:string" use="required" fixed="Prospects" /> 
  <xsd:attribute name="keys" type="xsd:string" use="required" fixed="ProposalID" /> 
  <xsd:attribute name="codeTableColVal" type="xdv:codeTableColVal" use="optional" /> 
</xsd:complexType> 

另一组元素如下所示:

<xsd:element name="Employees" minOccurs="0" maxOccurs="1"> 
<xsd:complexType> 
  <xsd:sequence> 
    <xsd:element name="ROW"> 
      <xsd:complexType> 
        <xsd:sequence> 
          <xsd:element name="ID" type="xdv:guidKey" nillable="false" /> 
          <xsd:element name="Seq" type="xdv:guidKey" nillable="false" /> 
          <xsd:element name="CompanyName" minOccurs="0"> 
            <xsd:complexType> 
              <xsd:simpleContent> 
                <xsd:extension base="xdv:stringLen32"> 
                  <xsd:attribute name="origVal" type="xdv:stringLen32" use="optional" /> 
                </xsd:extension> 
              </xsd:simpleContent> 
            </xsd:complexType> 
          </xsd:element> 
          ... more stuff... 
    </xsd:element> 
  </xsd:sequence> 
  <xsd:attribute name="name" type="xsd:string" use="required" fixed="Employees" /> 
  <xsd:attribute name="alias" type="xsd:string" use="required" fixed="Employees" /> 
  <xsd:attribute name="keys" type="xsd:string" use="required" fixed="OpportunityID,Seq" /> 
  <xsd:attribute name="codeTableColVal" type="xdv:codeTableColVal" use="optional" /> 
</xsd:complexType> 

主要区别在于前者为 Prospects 指定了 minOccurs="0"并且没有出现最大值,然后对于 ROW 它定义了 minOccurs=0 和 maxOccurs=unbounded。

对于后者,它为 Employees 定义了 minOccurs=0 和 maxOccurs=1,而对于 ROW,它没有定义 minOccurs 或 maxOccurs。

当我运行实用程序 Xsd2Code 并生成我的 C# 代码时,它会为 Prospects 创建一个带有 ROWs 集合的 Prospects 属性(作为 List()),但为 Employees 创建一个 Prospects 属性它创建一个具有 ROW 属性的 Employee 属性,而不是一个集合。

My question: whats is the schema rule for this? Since there is no maxOccurs defined on ROW for employee does the min and max for the parent apply or should it be a collection?

我正在尝试确定创建我的代码的实用程序是否有误,或者 .xsd 文件是否有误。

请您参考如下方法:

如果未指定 maxOccurs,则默认值与 maxOccurs = "1" 相同。

来自 XML Schema Indicators .

Occurrence indicators are used to define how often an element can occur.

Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.