首页 > 解决方案 > 一个 XSD 复杂元素中可以有多个指标吗?

问题描述

我正在尝试构建一个新模式来验证 XML 是否适合我的工作。但是我很难回答这个问题:我可以以及如何创建一个复杂的元素,它有一些元素需要在一个集合序列中,而其他子元素不需要?最终我认为我应该能够在两组元素周围打开和关闭“序列”标签以及打开和关闭“所有”标签,但 xsd 似乎不喜欢那样。这是我所拥有的:

<xsd:complexType name="Original">
        <xsd:sequence>
            <xsd:element maxOccurs="1" minOccurs="1" name="AssetIdentifier" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation>Definition: The Asset Identifier element is intended to
                        reflect the root of all following digital filenames.</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element maxOccurs="1" minOccurs="0" name="ArchiveID" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation>Definition: The Filename element in this section is
                        intended to reflect the root of all the following derivative digital
                        filenames.</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element maxOccurs="1" minOccurs="1" name="Title" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation>Definition: The known title of the asset. If no title is
                        known, one can be assigned; a number or letter sequence, whichever is
                        the most logical. Using the value "unknown" is also
                        acceptable.</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element maxOccurs="1" minOccurs="1" name="RecordDate" type="xsd:date">
                <xsd:annotation>
                    <xsd:documentation>Definition: The actual recording date of the asset.
                        Estimates, partial dates, and date ranges (i.e. 19XX, Feb. 19-24,
                        1934-1935, etc.) are allowable, as is 'unknown'. Best practice, when
                        applicable, is to use the YYYY-MM-DD format in accordance with ISO 8601.
                        Even partial dates, i.e. 1990-05 should adhere to this
                        format.</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element maxOccurs="1" minOccurs="1" name="FormatType" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation>Definition: The format of the analog asset, i.e. Open
                        Reel, Grooved Disc, DAT, Cassette, VHS, 16mm film, EIAJ,
                        etc.</xsd:documentation>
                    <xsd:documentation>Best Practice: The MediaPreserve maintains a list of
                        controlled vocabularies organized by media type at: www.dontknowyet.com.
                        However, MP opted to meake this an unrestricted element in the event
                        that other ogranizations have their own controlled vocabularies in
                        place.</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
         </xsd:sequence>
        <xsd:all>
            <xsd:element maxOccurs="1" minOccurs="0" name="StockBrand" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation>If known definitively</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element maxOccurs="1" minOccurs="0" name="TapeModel" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation>If applicable. Usually applies to DAT tapes, open reels,
                        and wire recordings.</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
            <xsd:element maxOccurs="1" minOccurs="0" name="TapeWidth" type="xsd:string">
                <xsd:annotation>
                    <xsd:documentation>Typically only applicable for open reel
                        audio</xsd:documentation>
                </xsd:annotation>
            </xsd:element>
        </xsd:all>

标签: xsdxsd-validationvalidationrules

解决方案


不幸的是,XSD 不允许您尝试做的事情(组合<sequence/><all />在单个复杂类型或元素内)。您可能可以使用嵌套内容模型实现类似的效果,但请注意您不能嵌套<all>在 another 下<all />,否则您必须在另一个元素中定义它。但是,您可以嵌套<sequence>或嵌套<choice>在彼此之下。

根据我对 XSD 的理解,您有 3 个可行的选择。

第一个是嵌套你想要<all />包含在它们自己的子元素中的所有元素:

<xs:complexType name="Original">
  <xs:sequence>
    <!-- AssetIdentifier to FormatType left out for brevity -->
    <xs:element name="Misc">
      <xs:complexType>
        <xs:all>
          <xs:element maxOccurs="1" minOccurs="0" name="StockBrand" type="xs:string" />
          <xs:element maxOccurs="1" minOccurs="0" name="TapeModel" type="xs:string" />
          <xs:element maxOccurs="1" minOccurs="0" name="TapeWidth" type="xs:string" />
        </xs:all>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>
<!-- For the above, valid XML would be: -->
<Original>
  <AssetIdentifier>AssetIdentifier0</AssetIdentifier>
  <Title>Title0</Title>
  <RecordDate>2006-05-04</RecordDate>
  <FormatType>FormatType0</FormatType>
  <Misc>
    <!-- Optional & order doesn't matter -->
    <StockBrand>what</StockBrand>
    <TapeWidth>1290</TapeWidth>
    <TapeModel>Hey</TapeModel>
  </Misc>
</Original>

其次是将这些元素嵌套在 another 下<sequence />,这允许您放弃指定另一个子元素,但现在要求元素按照架构中指定的顺序出现。请注意,嵌套序列本身可以是可选的。

<xs:complexType name="Original">
  <xs:sequence>
    <!-- AssetIdentifier to FormatType left out for brevity -->
    <xs:sequence minOccurs="0">
      <xs:element maxOccurs="1" minOccurs="0" name="StockBrand" type="xs:string" />
      <xs:element maxOccurs="1" minOccurs="0" name="TapeModel" type="xs:string" />
      <xs:element maxOccurs="1" minOccurs="0" name="TapeWidth" type="xs:string" />
    </xs:sequence>
  </xs:sequence>
</xs:complexType>

<!-- For the above, valid XML would be: -->
<Original>
  <AssetIdentifier>AssetIdentifier0</AssetIdentifier>
  <Title>Title0</Title>
  <RecordDate>2006-05-04</RecordDate>
  <FormatType>FormatType0</FormatType>
  <!-- Optional below, but must be ordered -->
  <StockBrand>what</StockBrand>
  <TapeModel>Hey</TapeModel>
  <TapeWidth>1290</TapeWidth>
</Original>

还有第三个选项有点“hack”,但仍然允许指定元素无序,仍然是可选的,但仍然与其他强制性的有序元素相邻。这在父序列(序列 > 序列 > 选择)内的序列下嵌套了一个选择(使用maxOccurs="3"):

<xs:complexType name="Original">
  <xs:sequence>
    <!-- AssetIdentifier to FormatType left out for brevity -->
    <xs:sequence>
      <xs:choice maxOccurs="3" minOccurs="0">
        <xs:element name="StockBrand" type="xs:string"/>
        <xs:element name="TapeModel" type="xs:string"/>
        <xs:element name="TapeWidth" type="xs:string"/>
      </xs:choice>
    </xs:sequence>
  </xs:sequence>
</xs:complexType>
<!-- For the above, valid XML would be: -->
<Original>
  <AssetIdentifier>AssetIdentifier0</AssetIdentifier>
  <Title>Title0</Title>
  <RecordDate>2006-05-04</RecordDate>
  <FormatType>FormatType0</FormatType>
  <!-- Optional, unordered, but there's a catch: -->
  <TapeWidth>1290</TapeWidth>
  <StockBrand>what</StockBrand>
  <TapeModel>Hey</TapeModel>
</Original>

然而,这第三个选项有一个问题,元素上的maxOccurs="3"and使子元素(和)上的 and无意义;这意味着这些元素虽然仍然是可选的,但现在可以出现多次,只要元素的累积总数仍然是 3 或更少:<choice />minOccursmaxOccursStockBrandTapeModelTapeWidth

这变得有效(2 个相同元素 + 1 个以上):

  <TapeWidth>1290</TapeWidth>
  <TapeWidth>1291</TapeWidth>
  <TapeModel>Hey</TapeModel>

这仍然有效(3个相同):

  <TapeWidth>1290</TapeWidth>
  <TapeWidth>1291</TapeWidth>
  <TapeWidth>1292</TapeWidth>

还有这个(只有 1 个元素出现 1 次):

  <StockBrand>1290</StockBrand>

您可能会尝试通过摆弄序列和选择嵌套的组合来找到另一个选项,但最好的做法是让您的模式保持简单。就个人而言,我会推荐前两个选项而不是第三个选项,纯粹是为了让您的架构简单。


推荐阅读